UNPKG

8.61 kBJavaScriptView Raw
1'use strict';
2
3/*!
4 * Module dependencies.
5 */
6
7const CastError = require('../error/cast');
8const EventEmitter = require('events').EventEmitter;
9const ObjectExpectedError = require('../error/objectExpected');
10const SchemaSingleNestedOptions = require('../options/SchemaSingleNestedOptions');
11const SchemaType = require('../schematype');
12const $exists = require('./operators/exists');
13const castToNumber = require('./operators/helpers').castToNumber;
14const discriminator = require('../helpers/model/discriminator');
15const geospatial = require('./operators/geospatial');
16const get = require('../helpers/get');
17const getConstructor = require('../helpers/discriminator/getConstructor');
18const handleIdOption = require('../helpers/schema/handleIdOption');
19const internalToObjectOptions = require('../options').internalToObjectOptions;
20
21let Subdocument;
22
23module.exports = SingleNestedPath;
24
25/**
26 * Single nested subdocument SchemaType constructor.
27 *
28 * @param {Schema} schema
29 * @param {String} key
30 * @param {Object} options
31 * @inherits SchemaType
32 * @api public
33 */
34
35function SingleNestedPath(schema, path, options) {
36 schema = handleIdOption(schema, options);
37
38 this.caster = _createConstructor(schema);
39 this.caster.path = path;
40 this.caster.prototype.$basePath = path;
41 this.schema = schema;
42 this.$isSingleNested = true;
43 SchemaType.call(this, path, options, 'Embedded');
44}
45
46/*!
47 * ignore
48 */
49
50SingleNestedPath.prototype = Object.create(SchemaType.prototype);
51SingleNestedPath.prototype.constructor = SingleNestedPath;
52SingleNestedPath.prototype.OptionsConstructor = SchemaSingleNestedOptions;
53
54/*!
55 * ignore
56 */
57
58function _createConstructor(schema, baseClass) {
59 // lazy load
60 Subdocument || (Subdocument = require('../types/subdocument'));
61
62 const _embedded = function SingleNested(value, path, parent) {
63 const _this = this;
64
65 this.$parent = parent;
66 Subdocument.apply(this, arguments);
67
68 this.$session(this.ownerDocument().$session());
69
70 if (parent) {
71 parent.on('save', function() {
72 _this.emit('save', _this);
73 _this.constructor.emit('save', _this);
74 });
75
76 parent.on('isNew', function(val) {
77 _this.isNew = val;
78 _this.emit('isNew', val);
79 _this.constructor.emit('isNew', val);
80 });
81 }
82 };
83
84 const proto = baseClass != null ? baseClass.prototype : Subdocument.prototype;
85 _embedded.prototype = Object.create(proto);
86 _embedded.prototype.$__setSchema(schema);
87 _embedded.prototype.constructor = _embedded;
88 _embedded.schema = schema;
89 _embedded.$isSingleNested = true;
90 _embedded.events = new EventEmitter();
91 _embedded.prototype.toBSON = function() {
92 return this.toObject(internalToObjectOptions);
93 };
94
95 // apply methods
96 for (const i in schema.methods) {
97 _embedded.prototype[i] = schema.methods[i];
98 }
99
100 // apply statics
101 for (const i in schema.statics) {
102 _embedded[i] = schema.statics[i];
103 }
104
105 for (const i in EventEmitter.prototype) {
106 _embedded[i] = EventEmitter.prototype[i];
107 }
108
109 return _embedded;
110}
111
112/*!
113 * Special case for when users use a common location schema to represent
114 * locations for use with $geoWithin.
115 * https://docs.mongodb.org/manual/reference/operator/query/geoWithin/
116 *
117 * @param {Object} val
118 * @api private
119 */
120
121SingleNestedPath.prototype.$conditionalHandlers.$geoWithin = function handle$geoWithin(val) {
122 return { $geometry: this.castForQuery(val.$geometry) };
123};
124
125/*!
126 * ignore
127 */
128
129SingleNestedPath.prototype.$conditionalHandlers.$near =
130SingleNestedPath.prototype.$conditionalHandlers.$nearSphere = geospatial.cast$near;
131
132SingleNestedPath.prototype.$conditionalHandlers.$within =
133SingleNestedPath.prototype.$conditionalHandlers.$geoWithin = geospatial.cast$within;
134
135SingleNestedPath.prototype.$conditionalHandlers.$geoIntersects =
136 geospatial.cast$geoIntersects;
137
138SingleNestedPath.prototype.$conditionalHandlers.$minDistance = castToNumber;
139SingleNestedPath.prototype.$conditionalHandlers.$maxDistance = castToNumber;
140
141SingleNestedPath.prototype.$conditionalHandlers.$exists = $exists;
142
143/**
144 * Casts contents
145 *
146 * @param {Object} value
147 * @api private
148 */
149
150SingleNestedPath.prototype.cast = function(val, doc, init, priorVal) {
151 if (val && val.$isSingleNested && val.parent === doc) {
152 return val;
153 }
154
155 if (val != null && (typeof val !== 'object' || Array.isArray(val))) {
156 throw new ObjectExpectedError(this.path, val);
157 }
158
159 const Constructor = getConstructor(this.caster, val);
160
161 let subdoc;
162
163 // Only pull relevant selected paths and pull out the base path
164 const parentSelected = get(doc, '$__.selected', {});
165 const path = this.path;
166 const selected = Object.keys(parentSelected).reduce((obj, key) => {
167 if (key.startsWith(path + '.')) {
168 obj[key.substr(path.length + 1)] = parentSelected[key];
169 }
170 return obj;
171 }, {});
172
173 if (init) {
174 subdoc = new Constructor(void 0, selected, doc);
175 subdoc.init(val);
176 } else {
177 if (Object.keys(val).length === 0) {
178 return new Constructor({}, selected, doc);
179 }
180
181 return new Constructor(val, selected, doc, undefined, { priorDoc: priorVal });
182 }
183
184 return subdoc;
185};
186
187/**
188 * Casts contents for query
189 *
190 * @param {string} [$conditional] optional query operator (like `$eq` or `$in`)
191 * @param {any} value
192 * @api private
193 */
194
195SingleNestedPath.prototype.castForQuery = function($conditional, val, options) {
196 let handler;
197 if (arguments.length === 2) {
198 handler = this.$conditionalHandlers[$conditional];
199 if (!handler) {
200 throw new Error('Can\'t use ' + $conditional);
201 }
202 return handler.call(this, val);
203 }
204 val = $conditional;
205 if (val == null) {
206 return val;
207 }
208
209 if (this.options.runSetters) {
210 val = this._applySetters(val);
211 }
212
213 const Constructor = getConstructor(this.caster, val);
214 const overrideStrict = options != null && options.strict != null ?
215 options.strict :
216 void 0;
217
218 try {
219 val = new Constructor(val, overrideStrict);
220 } catch (error) {
221 // Make sure we always wrap in a CastError (gh-6803)
222 if (!(error instanceof CastError)) {
223 throw new CastError('Embedded', val, this.path, error, this);
224 }
225 throw error;
226 }
227 return val;
228};
229
230/**
231 * Async validation on this single nested doc.
232 *
233 * @api private
234 */
235
236SingleNestedPath.prototype.doValidate = function(value, fn, scope, options) {
237 const Constructor = getConstructor(this.caster, value);
238
239 if (options && options.skipSchemaValidators) {
240 if (!(value instanceof Constructor)) {
241 value = new Constructor(value, null, scope);
242 }
243 return value.validate(fn);
244 }
245
246 SchemaType.prototype.doValidate.call(this, value, function(error) {
247 if (error) {
248 return fn(error);
249 }
250 if (!value) {
251 return fn(null);
252 }
253
254 value.validate(fn);
255 }, scope, options);
256};
257
258/**
259 * Synchronously validate this single nested doc
260 *
261 * @api private
262 */
263
264SingleNestedPath.prototype.doValidateSync = function(value, scope, options) {
265 if (!options || !options.skipSchemaValidators) {
266 const schemaTypeError = SchemaType.prototype.doValidateSync.call(this, value, scope);
267 if (schemaTypeError) {
268 return schemaTypeError;
269 }
270 }
271 if (!value) {
272 return;
273 }
274 return value.validateSync();
275};
276
277/**
278 * Adds a discriminator to this single nested subdocument.
279 *
280 * ####Example:
281 * const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
282 * const schema = Schema({ shape: shapeSchema });
283 *
284 * const singleNestedPath = parentSchema.path('shape');
285 * singleNestedPath.discriminator('Circle', Schema({ radius: Number }));
286 *
287 * @param {String} name
288 * @param {Schema} schema fields to add to the schema for instances of this sub-class
289 * @param {String} [value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
290 * @return {Function} the constructor Mongoose will use for creating instances of this discriminator model
291 * @see discriminators /docs/discriminators.html
292 * @api public
293 */
294
295SingleNestedPath.prototype.discriminator = function(name, schema, value) {
296 schema = discriminator(this.caster, name, schema, value);
297
298 this.caster.discriminators[name] = _createConstructor(schema, this.caster);
299
300 return this.caster.discriminators[name];
301};
302
303/*!
304 * ignore
305 */
306
307SingleNestedPath.prototype.clone = function() {
308 const options = Object.assign({}, this.options);
309 const schematype = new this.constructor(this.schema, this.path, options);
310 schematype.validators = this.validators.slice();
311 schematype.caster.discriminators = Object.assign({}, this.caster.discriminators);
312 return schematype;
313};