browserDocument.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const NodeJSDocument = require('./document');
  6. const EventEmitter = require('events').EventEmitter;
  7. const MongooseError = require('./error/index');
  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 isObject = require('./helpers/isObject');
  13. /**
  14. * Document constructor.
  15. *
  16. * @param {Object} obj the values to set
  17. * @param {Object} schema
  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 https://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. function Document(obj, schema, fields, skipId, skipInit) {
  26. if (!(this instanceof Document)) {
  27. return new Document(obj, schema, fields, skipId, skipInit);
  28. }
  29. if (isObject(schema) && !schema.instanceOfSchema) {
  30. schema = new Schema(schema);
  31. }
  32. // When creating EmbeddedDocument, it already has the schema and he doesn't need the _id
  33. schema = this.schema || schema;
  34. // Generate ObjectId if it is missing, but it requires a scheme
  35. if (!this.schema && schema.options._id) {
  36. obj = obj || {};
  37. if (obj._id === undefined) {
  38. obj._id = new ObjectId();
  39. }
  40. }
  41. if (!schema) {
  42. throw new MongooseError.MissingSchemaError();
  43. }
  44. this.$__setSchema(schema);
  45. NodeJSDocument.call(this, obj, fields, skipId, skipInit);
  46. applyHooks(this, schema, { decorateDoc: true });
  47. // apply methods
  48. for (const m in schema.methods) {
  49. this[m] = schema.methods[m];
  50. }
  51. // apply statics
  52. for (const s in schema.statics) {
  53. this[s] = schema.statics[s];
  54. }
  55. }
  56. /*!
  57. * Inherit from the NodeJS document
  58. */
  59. Document.prototype = Object.create(NodeJSDocument.prototype);
  60. Document.prototype.constructor = Document;
  61. /*!
  62. * ignore
  63. */
  64. Document.events = new EventEmitter();
  65. /*!
  66. * Browser doc exposes the event emitter API
  67. */
  68. Document.$emitter = new EventEmitter();
  69. ['on', 'once', 'emit', 'listeners', 'removeListener', 'setMaxListeners',
  70. 'removeAllListeners', 'addListener'].forEach(function(emitterFn) {
  71. Document[emitterFn] = function() {
  72. return Document.$emitter[emitterFn].apply(Document.$emitter, arguments);
  73. };
  74. });
  75. /*!
  76. * Module exports.
  77. */
  78. Document.ValidationError = ValidationError;
  79. module.exports = exports = Document;