thoughts/node_modules/mongoose/lib/browserDocument.js
node_modules/mongoose/lib/browserDocument.jsRaw
1/*!
2 * Module dependencies.
3 */
4
5var NodeJSDocument = require('./document'),
6 EventEmitter = require('events').EventEmitter,
7 MongooseError = require('./error'),
8 Schema = require('./schema'),
9 ObjectId = require('./types/objectid'),
10 utils = require('./utils'),
11 ValidationError = MongooseError.ValidationError,
12 InternalCache = require('./internal');
13
14/**
15 * Document constructor.
16 *
17 * @param {Object} obj the values to set
18 * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
19 * @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
20 * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
21 * @event `init`: Emitted on a document after it has was retrieved from the db and fully hydrated by Mongoose.
22 * @event `save`: Emitted when the document is successfully saved
23 * @api private
24 */
25
26function Document(obj, schema, fields, skipId, skipInit) {
27 if (!(this instanceof Document)) {
28 return new Document(obj, schema, fields, skipId, skipInit);
29 }
30
31
32 if (utils.isObject(schema) && !schema.instanceOfSchema) {
33 schema = new Schema(schema);
34 }
35
36 // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
37 schema = this.schema || schema;
38
39 // Generate ObjectId if it is missing, but it requires a scheme
40 if (!this.schema && schema.options._id) {
41 obj = obj || {};
42
43 if (obj._id === undefined) {
44 obj._id = new ObjectId();
45 }
46 }
47
48 if (!schema) {
49 throw new MongooseError.MissingSchemaError();
50 }
51
52 this.$__setSchema(schema);
53
54 this.$__ = new InternalCache;
55 this.$__.emitter = new EventEmitter();
56 this.isNew = true;
57 this.errors = undefined;
58
59 // var schema = this.schema;
60
61 if (typeof fields === 'boolean') {
62 this.$__.strictMode = fields;
63 fields = undefined;
64 } else {
65 this.$__.strictMode = this.schema.options && this.schema.options.strict;
66 this.$__.selected = fields;
67 }
68
69 var required = this.schema.requiredPaths();
70 for (var i = 0; i < required.length; ++i) {
71 this.$__.activePaths.require(required[i]);
72 }
73
74 this.$__.emitter.setMaxListeners(0);
75 this._doc = this.$__buildDoc(obj, fields, skipId);
76
77 if (!skipInit && obj) {
78 this.init(obj);
79 }
80
81 this.$__registerHooksFromSchema();
82
83 // apply methods
84 for (var m in schema.methods) {
85 this[m] = schema.methods[m];
86 }
87 // apply statics
88 for (var s in schema.statics) {
89 this[s] = schema.statics[s];
90 }
91}
92
93/*!
94 * Inherit from the NodeJS document
95 */
96Document.prototype = Object.create(NodeJSDocument.prototype);
97Document.prototype.constructor = Document;
98
99/*!
100 * Module exports.
101 */
102Document.ValidationError = ValidationError;
103module.exports = exports = Document;