(schema, options, baseClass)
| 136 | */ |
| 137 | |
| 138 | function _createConstructor(schema, options, baseClass) { |
| 139 | Subdocument || (Subdocument = require('../types/arraySubdocument')); |
| 140 | |
| 141 | // compile an embedded document for this schema |
| 142 | function EmbeddedDocument() { |
| 143 | Subdocument.apply(this, arguments); |
| 144 | // `ArraySubdocument` already looked up the parent array's parent doc, so |
| 145 | // reuse it rather than going through the document array proxy again. |
| 146 | const parent = this[documentArrayParent]; |
| 147 | if (parent == null) { |
| 148 | return; |
| 149 | } |
| 150 | this.$session(parent.$session()); |
| 151 | } |
| 152 | |
| 153 | // Must run before `$__setSchema()`, which compiles virtuals onto the prototype |
| 154 | schema.plugin(idGetter, { deduplicate: true }); |
| 155 | |
| 156 | const proto = baseClass?.prototype ?? Subdocument.prototype; |
| 157 | EmbeddedDocument.prototype = Object.create(proto); |
| 158 | EmbeddedDocument.prototype.$__setSchema(schema); |
| 159 | EmbeddedDocument.schema = schema; |
| 160 | EmbeddedDocument.prototype.constructor = EmbeddedDocument; |
| 161 | EmbeddedDocument.$isArraySubdocument = true; |
| 162 | EmbeddedDocument.events = new EventEmitter(); |
| 163 | EmbeddedDocument.base = schema.base; |
| 164 | |
| 165 | // apply methods |
| 166 | for (const i in schema.methods) { |
| 167 | EmbeddedDocument.prototype[i] = schema.methods[i]; |
| 168 | } |
| 169 | |
| 170 | // apply statics |
| 171 | for (const i in schema.statics) { |
| 172 | EmbeddedDocument[i] = schema.statics[i]; |
| 173 | } |
| 174 | |
| 175 | for (const i in EventEmitter.prototype) { |
| 176 | EmbeddedDocument[i] = EventEmitter.prototype[i]; |
| 177 | } |
| 178 | |
| 179 | EmbeddedDocument.options = options; |
| 180 | |
| 181 | return EmbeddedDocument; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Adds a discriminator to this document array. |
no test coverage detected