browserDocument.js 3.2 KB

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