schemaSubdocumentOptions.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const SchemaTypeOptions = require('./schemaTypeOptions');
  3. /**
  4. * The options defined on a single nested schematype.
  5. *
  6. * #### Example:
  7. *
  8. * const schema = Schema({ child: Schema({ name: String }) });
  9. * schema.path('child').options; // SchemaSubdocumentOptions instance
  10. *
  11. * @api public
  12. * @inherits SchemaTypeOptions
  13. * @constructor SchemaSubdocumentOptions
  14. */
  15. class SchemaSubdocumentOptions extends SchemaTypeOptions {}
  16. const opts = require('./propertyOptions');
  17. /**
  18. * If set, overwrites the child schema's `_id` option.
  19. *
  20. * #### Example:
  21. *
  22. * const childSchema = Schema({ name: String });
  23. * const parentSchema = Schema({
  24. * child: { type: childSchema, _id: false }
  25. * });
  26. * parentSchema.path('child').schema.options._id; // false
  27. *
  28. * @api public
  29. * @property _id
  30. * @memberOf SchemaSubdocumentOptions
  31. * @type {Function|string}
  32. * @instance
  33. */
  34. Object.defineProperty(SchemaSubdocumentOptions.prototype, '_id', opts);
  35. /**
  36. * If set, overwrites the child schema's `minimize` option. In addition, configures whether the entire
  37. * subdocument can be minimized out.
  38. *
  39. * #### Example:
  40. *
  41. * const childSchema = Schema({ name: String });
  42. * const parentSchema = Schema({
  43. * child: { type: childSchema, minimize: false }
  44. * });
  45. * const ParentModel = mongoose.model('Parent', parentSchema);
  46. * // Saves `{ child: {} }` to the db. Without `minimize: false`, Mongoose would remove the empty
  47. * // object and save `{}` to the db.
  48. * await ParentModel.create({ child: {} });
  49. *
  50. * @api public
  51. * @property minimize
  52. * @memberOf SchemaSubdocumentOptions
  53. * @type {Function|string}
  54. * @instance
  55. */
  56. Object.defineProperty(SchemaSubdocumentOptions.prototype, 'minimize', opts);
  57. module.exports = SchemaSubdocumentOptions;