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