123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- 'use strict';
- const CastError = require('../error/cast');
- const EventEmitter = require('events').EventEmitter;
- const ObjectExpectedError = require('../error/objectExpected');
- const SchemaSingleNestedOptions = require('../options/SchemaSingleNestedOptions');
- const SchemaType = require('../schematype');
- const $exists = require('./operators/exists');
- const castToNumber = require('./operators/helpers').castToNumber;
- const discriminator = require('../helpers/model/discriminator');
- const geospatial = require('./operators/geospatial');
- const get = require('../helpers/get');
- const getConstructor = require('../helpers/discriminator/getConstructor');
- const handleIdOption = require('../helpers/schema/handleIdOption');
- const internalToObjectOptions = require('../options').internalToObjectOptions;
- let Subdocument;
- module.exports = SingleNestedPath;
- function SingleNestedPath(schema, path, options) {
- schema = handleIdOption(schema, options);
- this.caster = _createConstructor(schema);
- this.caster.path = path;
- this.caster.prototype.$basePath = path;
- this.schema = schema;
- this.$isSingleNested = true;
- SchemaType.call(this, path, options, 'Embedded');
- }
- SingleNestedPath.prototype = Object.create(SchemaType.prototype);
- SingleNestedPath.prototype.constructor = SingleNestedPath;
- SingleNestedPath.prototype.OptionsConstructor = SchemaSingleNestedOptions;
- function _createConstructor(schema, baseClass) {
-
- Subdocument || (Subdocument = require('../types/subdocument'));
- const _embedded = function SingleNested(value, path, parent) {
- const _this = this;
- this.$__parent = parent;
- Subdocument.apply(this, arguments);
- this.$session(this.ownerDocument().$session());
- if (parent) {
- parent.on('save', function() {
- _this.emit('save', _this);
- _this.constructor.emit('save', _this);
- });
- parent.on('isNew', function(val) {
- _this.isNew = val;
- _this.emit('isNew', val);
- _this.constructor.emit('isNew', val);
- });
- }
- };
- const proto = baseClass != null ? baseClass.prototype : Subdocument.prototype;
- _embedded.prototype = Object.create(proto);
- _embedded.prototype.$__setSchema(schema);
- _embedded.prototype.constructor = _embedded;
- _embedded.schema = schema;
- _embedded.$isSingleNested = true;
- _embedded.events = new EventEmitter();
- _embedded.prototype.toBSON = function() {
- return this.toObject(internalToObjectOptions);
- };
-
- for (const i in schema.methods) {
- _embedded.prototype[i] = schema.methods[i];
- }
-
- for (const i in schema.statics) {
- _embedded[i] = schema.statics[i];
- }
- for (const i in EventEmitter.prototype) {
- _embedded[i] = EventEmitter.prototype[i];
- }
- return _embedded;
- }
- SingleNestedPath.prototype.$conditionalHandlers.$geoWithin = function handle$geoWithin(val) {
- return { $geometry: this.castForQuery(val.$geometry) };
- };
- SingleNestedPath.prototype.$conditionalHandlers.$near =
- SingleNestedPath.prototype.$conditionalHandlers.$nearSphere = geospatial.cast$near;
- SingleNestedPath.prototype.$conditionalHandlers.$within =
- SingleNestedPath.prototype.$conditionalHandlers.$geoWithin = geospatial.cast$within;
- SingleNestedPath.prototype.$conditionalHandlers.$geoIntersects =
- geospatial.cast$geoIntersects;
- SingleNestedPath.prototype.$conditionalHandlers.$minDistance = castToNumber;
- SingleNestedPath.prototype.$conditionalHandlers.$maxDistance = castToNumber;
- SingleNestedPath.prototype.$conditionalHandlers.$exists = $exists;
- SingleNestedPath.prototype.cast = function(val, doc, init, priorVal, options) {
- if (val && val.$isSingleNested && val.parent === doc) {
- return val;
- }
- if (val != null && (typeof val !== 'object' || Array.isArray(val))) {
- throw new ObjectExpectedError(this.path, val);
- }
- const Constructor = getConstructor(this.caster, val);
- let subdoc;
-
- const parentSelected = get(doc, '$__.selected', {});
- const path = this.path;
- const selected = Object.keys(parentSelected).reduce((obj, key) => {
- if (key.startsWith(path + '.')) {
- obj[key.substr(path.length + 1)] = parentSelected[key];
- }
- return obj;
- }, {});
- options = Object.assign({}, options, { priorDoc: priorVal });
- if (init) {
- subdoc = new Constructor(void 0, selected, doc);
- subdoc.init(val);
- } else {
- if (Object.keys(val).length === 0) {
- return new Constructor({}, selected, doc, undefined, options);
- }
- return new Constructor(val, selected, doc, undefined, options);
- }
- return subdoc;
- };
- SingleNestedPath.prototype.castForQuery = function($conditional, val, options) {
- let handler;
- if (arguments.length === 2) {
- handler = this.$conditionalHandlers[$conditional];
- if (!handler) {
- throw new Error('Can\'t use ' + $conditional);
- }
- return handler.call(this, val);
- }
- val = $conditional;
- if (val == null) {
- return val;
- }
- if (this.options.runSetters) {
- val = this._applySetters(val);
- }
- const Constructor = getConstructor(this.caster, val);
- const overrideStrict = options != null && options.strict != null ?
- options.strict :
- void 0;
- try {
- val = new Constructor(val, overrideStrict);
- } catch (error) {
-
- if (!(error instanceof CastError)) {
- throw new CastError('Embedded', val, this.path, error, this);
- }
- throw error;
- }
- return val;
- };
- SingleNestedPath.prototype.doValidate = function(value, fn, scope, options) {
- const Constructor = getConstructor(this.caster, value);
- if (options && options.skipSchemaValidators) {
- if (!(value instanceof Constructor)) {
- value = new Constructor(value, null, scope);
- }
- return value.validate(fn);
- }
- SchemaType.prototype.doValidate.call(this, value, function(error) {
- if (error) {
- return fn(error);
- }
- if (!value) {
- return fn(null);
- }
- value.validate(fn);
- }, scope, options);
- };
- SingleNestedPath.prototype.doValidateSync = function(value, scope, options) {
- if (!options || !options.skipSchemaValidators) {
- const schemaTypeError = SchemaType.prototype.doValidateSync.call(this, value, scope);
- if (schemaTypeError) {
- return schemaTypeError;
- }
- }
- if (!value) {
- return;
- }
- return value.validateSync();
- };
- SingleNestedPath.prototype.discriminator = function(name, schema, value) {
- schema = discriminator(this.caster, name, schema, value);
- this.caster.discriminators[name] = _createConstructor(schema, this.caster);
- return this.caster.discriminators[name];
- };
- SingleNestedPath.defaultOptions = {};
- SingleNestedPath.set = SchemaType.set;
- SingleNestedPath.prototype.clone = function() {
- const options = Object.assign({}, this.options);
- const schematype = new this.constructor(this.schema, this.path, options);
- schematype.validators = this.validators.slice();
- if (this.requiredValidator !== undefined) {
- schematype.requiredValidator = this.requiredValidator;
- }
- schematype.caster.discriminators = Object.assign({}, this.caster.discriminators);
- return schematype;
- };
|