MCPcopy Create free account
hub / github.com/Automattic/mongoose / _createConstructor

Function _createConstructor

lib/schema/documentArray.js:137–177  ·  view source on GitHub ↗
(schema, options, baseClass)

Source from the content-addressed store, hash-verified

135 */
136
137function _createConstructor(schema, options, baseClass) {
138 Subdocument || (Subdocument = require('../types/arraySubdocument'));
139
140 // compile an embedded document for this schema
141 function EmbeddedDocument() {
142 Subdocument.apply(this, arguments);
143 if (this.__parentArray == null || this.__parentArray.getArrayParent() == null) {
144 return;
145 }
146 this.$session(this.__parentArray.getArrayParent().$session());
147 }
148
149 schema._preCompile();
150
151 const proto = baseClass?.prototype ?? Subdocument.prototype;
152 EmbeddedDocument.prototype = Object.create(proto);
153 EmbeddedDocument.prototype.$__setSchema(schema);
154 EmbeddedDocument.schema = schema;
155 EmbeddedDocument.prototype.constructor = EmbeddedDocument;
156 EmbeddedDocument.$isArraySubdocument = true;
157 EmbeddedDocument.events = new EventEmitter();
158 EmbeddedDocument.base = schema.base;
159
160 // apply methods
161 for (const i in schema.methods) {
162 EmbeddedDocument.prototype[i] = schema.methods[i];
163 }
164
165 // apply statics
166 for (const i in schema.statics) {
167 EmbeddedDocument[i] = schema.statics[i];
168 }
169
170 for (const i in EventEmitter.prototype) {
171 EmbeddedDocument[i] = EventEmitter.prototype[i];
172 }
173
174 EmbeddedDocument.options = options;
175
176 return EmbeddedDocument;
177}
178
179/**
180 * Adds a discriminator to this document array.

Callers 2

SchemaDocumentArrayFunction · 0.70
documentArray.jsFile · 0.70

Calls 1

createMethod · 0.80

Tested by

no test coverage detected