SchemaTypeOptions.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. 'use strict';
  2. const clone = require('../helpers/clone');
  3. /**
  4. * The options defined on a schematype.
  5. *
  6. * #### Example:
  7. *
  8. * const schema = new Schema({ name: String });
  9. * schema.path('name').options instanceof mongoose.SchemaTypeOptions; // true
  10. *
  11. * @api public
  12. * @constructor SchemaTypeOptions
  13. */
  14. class SchemaTypeOptions {
  15. constructor(obj) {
  16. if (obj == null) {
  17. return this;
  18. }
  19. Object.assign(this, clone(obj));
  20. }
  21. }
  22. const opts = require('./propertyOptions');
  23. /**
  24. * The type to cast this path to.
  25. *
  26. * @api public
  27. * @property type
  28. * @memberOf SchemaTypeOptions
  29. * @type Function|String|Object
  30. * @instance
  31. */
  32. Object.defineProperty(SchemaTypeOptions.prototype, 'type', opts);
  33. /**
  34. * Function or object describing how to validate this schematype.
  35. *
  36. * @api public
  37. * @property validate
  38. * @memberOf SchemaTypeOptions
  39. * @type Function|Object
  40. * @instance
  41. */
  42. Object.defineProperty(SchemaTypeOptions.prototype, 'validate', opts);
  43. /**
  44. * Allows overriding casting logic for this individual path. If a string, the
  45. * given string overwrites Mongoose's default cast error message.
  46. *
  47. * #### Example:
  48. *
  49. * const schema = new Schema({
  50. * num: {
  51. * type: Number,
  52. * cast: '{VALUE} is not a valid number'
  53. * }
  54. * });
  55. *
  56. * // Throws 'CastError: "bad" is not a valid number'
  57. * schema.path('num').cast('bad');
  58. *
  59. * const Model = mongoose.model('Test', schema);
  60. * const doc = new Model({ num: 'fail' });
  61. * const err = doc.validateSync();
  62. *
  63. * err.errors['num']; // 'CastError: "fail" is not a valid number'
  64. *
  65. * @api public
  66. * @property cast
  67. * @memberOf SchemaTypeOptions
  68. * @type String
  69. * @instance
  70. */
  71. Object.defineProperty(SchemaTypeOptions.prototype, 'cast', opts);
  72. /**
  73. * If true, attach a required validator to this path, which ensures this path
  74. * cannot be set to a nullish value. If a function, Mongoose calls the
  75. * function and only checks for nullish values if the function returns a truthy value.
  76. *
  77. * @api public
  78. * @property required
  79. * @memberOf SchemaTypeOptions
  80. * @type Function|Boolean
  81. * @instance
  82. */
  83. Object.defineProperty(SchemaTypeOptions.prototype, 'required', opts);
  84. /**
  85. * The default value for this path. If a function, Mongoose executes the function
  86. * and uses the return value as the default.
  87. *
  88. * @api public
  89. * @property default
  90. * @memberOf SchemaTypeOptions
  91. * @type Function|Any
  92. * @instance
  93. */
  94. Object.defineProperty(SchemaTypeOptions.prototype, 'default', opts);
  95. /**
  96. * The model that `populate()` should use if populating this path.
  97. *
  98. * @api public
  99. * @property ref
  100. * @memberOf SchemaTypeOptions
  101. * @type Function|String
  102. * @instance
  103. */
  104. Object.defineProperty(SchemaTypeOptions.prototype, 'ref', opts);
  105. /**
  106. * The path in the document that `populate()` should use to find the model
  107. * to use.
  108. *
  109. * @api public
  110. * @property ref
  111. * @memberOf SchemaTypeOptions
  112. * @type Function|String
  113. * @instance
  114. */
  115. Object.defineProperty(SchemaTypeOptions.prototype, 'refPath', opts);
  116. /**
  117. * Whether to include or exclude this path by default when loading documents
  118. * using `find()`, `findOne()`, etc.
  119. *
  120. * @api public
  121. * @property select
  122. * @memberOf SchemaTypeOptions
  123. * @type Boolean|Number
  124. * @instance
  125. */
  126. Object.defineProperty(SchemaTypeOptions.prototype, 'select', opts);
  127. /**
  128. * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
  129. * build an index on this path when the model is compiled.
  130. *
  131. * @api public
  132. * @property index
  133. * @memberOf SchemaTypeOptions
  134. * @type Boolean|Number|Object
  135. * @instance
  136. */
  137. Object.defineProperty(SchemaTypeOptions.prototype, 'index', opts);
  138. /**
  139. * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
  140. * will build a unique index on this path when the
  141. * model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
  142. *
  143. * @api public
  144. * @property unique
  145. * @memberOf SchemaTypeOptions
  146. * @type Boolean|Number
  147. * @instance
  148. */
  149. Object.defineProperty(SchemaTypeOptions.prototype, 'unique', opts);
  150. /**
  151. * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
  152. * disallow changes to this path once the document
  153. * is saved to the database for the first time. Read more about [immutability in Mongoose here](https://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
  154. *
  155. * @api public
  156. * @property immutable
  157. * @memberOf SchemaTypeOptions
  158. * @type Function|Boolean
  159. * @instance
  160. */
  161. Object.defineProperty(SchemaTypeOptions.prototype, 'immutable', opts);
  162. /**
  163. * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
  164. * build a sparse index on this path.
  165. *
  166. * @api public
  167. * @property sparse
  168. * @memberOf SchemaTypeOptions
  169. * @type Boolean|Number
  170. * @instance
  171. */
  172. Object.defineProperty(SchemaTypeOptions.prototype, 'sparse', opts);
  173. /**
  174. * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
  175. * will build a text index on this path.
  176. *
  177. * @api public
  178. * @property text
  179. * @memberOf SchemaTypeOptions
  180. * @type Boolean|Number|Object
  181. * @instance
  182. */
  183. Object.defineProperty(SchemaTypeOptions.prototype, 'text', opts);
  184. /**
  185. * Define a transform function for this individual schema type.
  186. * Only called when calling `toJSON()` or `toObject()`.
  187. *
  188. * #### Example:
  189. *
  190. * const schema = Schema({
  191. * myDate: {
  192. * type: Date,
  193. * transform: v => v.getFullYear()
  194. * }
  195. * });
  196. * const Model = mongoose.model('Test', schema);
  197. *
  198. * const doc = new Model({ myDate: new Date('2019/06/01') });
  199. * doc.myDate instanceof Date; // true
  200. *
  201. * const res = doc.toObject({ transform: true });
  202. * res.myDate; // 2019
  203. *
  204. * @api public
  205. * @property transform
  206. * @memberOf SchemaTypeOptions
  207. * @type Function
  208. * @instance
  209. */
  210. Object.defineProperty(SchemaTypeOptions.prototype, 'transform', opts);
  211. module.exports = SchemaTypeOptions;