123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 'use strict';
- const NodeJSDocument = require('./document');
- const EventEmitter = require('events').EventEmitter;
- const MongooseError = require('./error/index');
- const Schema = require('./schema');
- const ObjectId = require('./types/objectid');
- const ValidationError = MongooseError.ValidationError;
- const applyHooks = require('./helpers/model/applyHooks');
- const isObject = require('./helpers/isObject');
- function Document(obj, schema, fields, skipId, skipInit) {
- if (!(this instanceof Document)) {
- return new Document(obj, schema, fields, skipId, skipInit);
- }
- if (isObject(schema) && !schema.instanceOfSchema) {
- schema = new Schema(schema);
- }
-
- schema = this.schema || schema;
-
- if (!this.schema && schema.options._id) {
- obj = obj || {};
- if (obj._id === undefined) {
- obj._id = new ObjectId();
- }
- }
- if (!schema) {
- throw new MongooseError.MissingSchemaError();
- }
- this.$__setSchema(schema);
- NodeJSDocument.call(this, obj, fields, skipId, skipInit);
- applyHooks(this, schema, { decorateDoc: true });
-
- for (const m in schema.methods) {
- this[m] = schema.methods[m];
- }
-
- for (const s in schema.statics) {
- this[s] = schema.statics[s];
- }
- }
- Document.prototype = Object.create(NodeJSDocument.prototype);
- Document.prototype.constructor = Document;
- Document.events = new EventEmitter();
- Document.$emitter = new EventEmitter();
- ['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
- 'removeAllListeners', 'addListener'].forEach(function(emitterFn) {
- Document[emitterFn] = function() {
- return Document.$emitter[emitterFn].apply(Document.$emitter, arguments);
- };
- });
- Document.ValidationError = ValidationError;
- module.exports = exports = Document;
|