* Document constructor. * * @param {Object} obj the values to set * @param {Object} schema * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data * @param {Boolean} [skipId] bool, should we auto crea
(obj, schema, fields, skipId, skipInit)
| 27 | */ |
| 28 | |
| 29 | function Document(obj, schema, fields, skipId, skipInit) { |
| 30 | if (!(this instanceof Document)) { |
| 31 | return new Document(obj, schema, fields, skipId, skipInit); |
| 32 | } |
| 33 | |
| 34 | if (isObject(schema) && !schema.instanceOfSchema) { |
| 35 | schema = new Schema(schema); |
| 36 | } |
| 37 | |
| 38 | // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id |
| 39 | schema = this.schema || schema; |
| 40 | |
| 41 | // Generate ObjectId if it is missing, but it requires a scheme |
| 42 | if (!this.schema && schema.options._id) { |
| 43 | obj = obj || {}; |
| 44 | |
| 45 | if (obj._id === undefined) { |
| 46 | obj._id = new ObjectId(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (!schema) { |
| 51 | throw new MongooseError.MissingSchemaError(); |
| 52 | } |
| 53 | |
| 54 | this.$__setSchema(schema); |
| 55 | |
| 56 | NodeJSDocument.call(this, obj, fields, skipId, skipInit); |
| 57 | |
| 58 | applyHooks(this, schema, { decorateDoc: true }); |
| 59 | |
| 60 | // apply methods |
| 61 | for (const m in schema.methods) { |
| 62 | this[m] = schema.methods[m]; |
| 63 | } |
| 64 | // apply statics |
| 65 | for (const s in schema.statics) { |
| 66 | this[s] = schema.statics[s]; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /*! |
| 71 | * Inherit from the NodeJS document |
nothing calls this directly
no test coverage detected