browserDocument.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const NodeJSDocument = require('./document');
  6. const EventEmitter = require('events').EventEmitter;
  7. const MongooseError = require('./error');
  8. const Schema = require('./schema');
  9. const ObjectId = require('./types/objectid');
  10. const ValidationError = MongooseError.ValidationError;
  11. const applyHooks = require('./helpers/model/applyHooks');
  12. const utils = require('./utils');
  13. /**
  14. * Document constructor.
  15. *
  16. * @param {Object} obj the values to set
  17. * @param {Object} [fields] optional object containing the fields which were selected in the query returning this document and any populated paths data
  18. * @param {Boolean} [skipId] bool, should we auto create an ObjectId _id
  19. * @inherits NodeJS EventEmitter http://nodejs.org/api/events.html#events_class_events_eventemitter
  20. * @event `init`: Emitted on a document after it has was retrieved from the db and fully hydrated by Mongoose.
  21. * @event `save`: Emitted when the document is successfully saved
  22. * @api private
  23. */
  24. function Document(obj, schema, fields, skipId, skipInit) {
  25. if (!(this instanceof Document)) {
  26. return new Document(obj, schema, fields, skipId, skipInit);
  27. }
  28. if (utils.isObject(schema) && !schema.instanceOfSchema) {
  29. schema = new Schema(schema);
  30. }
  31. // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
  32. schema = this.schema || schema;
  33. // Generate ObjectId if it is missing, but it requires a scheme
  34. if (!this.schema && schema.options._id) {
  35. obj = obj || {};
  36. if (obj._id === undefined) {
  37. obj._id = new ObjectId();
  38. }
  39. }
  40. if (!schema) {
  41. throw new MongooseError.MissingSchemaError();
  42. }
  43. this.$__setSchema(schema);
  44. NodeJSDocument.call(this, obj, fields, skipId, skipInit);
  45. applyHooks(this, schema, { decorateDoc: true });
  46. // apply methods
  47. for (const m in schema.methods) {
  48. this[m] = schema.methods[m];
  49. }
  50. // apply statics
  51. for (const s in schema.statics) {
  52. this[s] = schema.statics[s];
  53. }
  54. }
  55. /*!
  56. * Inherit from the NodeJS document
  57. */
  58. Document.prototype = Object.create(NodeJSDocument.prototype);
  59. Document.prototype.constructor = Document;
  60. /*!
  61. * ignore
  62. */
  63. Document.events = new EventEmitter();
  64. /*!
  65. * Browser doc exposes the event emitter API
  66. */
  67. Document.$emitter = new EventEmitter();
  68. utils.each(
  69. ['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
  70. 'removeAllListeners', 'addListener'],
  71. function(emitterFn) {
  72. Document[emitterFn] = function() {
  73. return Document.$emitter[emitterFn].apply(Document.$emitter, arguments);
  74. };
  75. });
  76. /*!
  77. * Module exports.
  78. */
  79. Document.ValidationError = ValidationError;
  80. module.exports = exports = Document;