123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- 'use strict';
- const checkEmbeddedDiscriminatorKeyProjection =
- require('./helpers/discriminator/checkEmbeddedDiscriminatorKeyProjection');
- const get = require('./helpers/get');
- const getDiscriminatorByValue =
- require('./helpers/discriminator/getDiscriminatorByValue');
- const isDefiningProjection = require('./helpers/projection/isDefiningProjection');
- const clone = require('./helpers/clone');
- exports.preparePopulationOptions = function preparePopulationOptions(query, options) {
- const _populate = query.options.populate;
- const pop = Object.keys(_populate).reduce((vals, key) => vals.concat([_populate[key]]), []);
-
- if (options.lean != null) {
- pop.
- filter(p => get(p, 'options.lean') == null).
- forEach(makeLean(options.lean));
- }
- return pop;
- };
- exports.preparePopulationOptionsMQ = function preparePopulationOptionsMQ(query, options) {
- const _populate = query._mongooseOptions.populate;
- const pop = Object.keys(_populate).reduce((vals, key) => vals.concat([_populate[key]]), []);
-
- if (options.lean != null) {
- pop.
- filter(p => get(p, 'options.lean') == null).
- forEach(makeLean(options.lean));
- }
- const session = get(query, 'options.session', null);
- if (session != null) {
- pop.forEach(path => {
- if (path.options == null) {
- path.options = { session: session };
- return;
- }
- if (!('session' in path.options)) {
- path.options.session = session;
- }
- });
- }
- const projection = query._fieldsForExec();
- pop.forEach(p => {
- p._queryProjection = projection;
- });
- return pop;
- };
- exports.createModel = function createModel(model, doc, fields, userProvidedFields, options) {
- model.hooks.execPreSync('createModel', doc);
- const discriminatorMapping = model.schema ?
- model.schema.discriminatorMapping :
- null;
- const key = discriminatorMapping && discriminatorMapping.isRoot ?
- discriminatorMapping.key :
- null;
- const value = doc[key];
- if (key && value && model.discriminators) {
- const discriminator = model.discriminators[value] || getDiscriminatorByValue(model.discriminators, value);
- if (discriminator) {
- const _fields = clone(userProvidedFields);
- exports.applyPaths(_fields, discriminator.schema);
- return new discriminator(undefined, _fields, true);
- }
- }
- if (typeof options === 'undefined') {
- options = {};
- options.defaults = true;
- }
- return new model(undefined, fields, {
- skipId: true,
- isNew: false,
- willInit: true,
- defaults: options.defaults
- });
- };
- exports.applyPaths = function applyPaths(fields, schema) {
-
- let exclude;
- let keys;
- let keyIndex;
- if (fields) {
- keys = Object.keys(fields);
- keyIndex = keys.length;
- while (keyIndex--) {
- if (keys[keyIndex][0] === '+') {
- continue;
- }
- const field = fields[keys[keyIndex]];
-
- if (!isDefiningProjection(field)) {
- continue;
- }
- exclude = !field;
- break;
- }
- }
-
-
- const selected = [];
- const excluded = [];
- const stack = [];
- analyzeSchema(schema);
- switch (exclude) {
- case true:
- for (const fieldName of excluded) {
- fields[fieldName] = 0;
- }
- break;
- case false:
- if (schema &&
- schema.paths['_id'] &&
- schema.paths['_id'].options &&
- schema.paths['_id'].options.select === false) {
- fields._id = 0;
- }
- for (const fieldName of selected) {
- fields[fieldName] = fields[fieldName] || 1;
- }
- break;
- case undefined:
- if (fields == null) {
- break;
- }
-
- for (const key of Object.keys(fields || {})) {
- if (key.startsWith('+')) {
- delete fields[key];
- }
- }
-
-
- for (const fieldName of excluded) {
- fields[fieldName] = 0;
- }
- break;
- }
- function analyzeSchema(schema, prefix) {
- prefix || (prefix = '');
-
- if (stack.indexOf(schema) !== -1) {
- return [];
- }
- stack.push(schema);
- const addedPaths = [];
- schema.eachPath(function(path, type) {
- if (prefix) path = prefix + '.' + path;
- let addedPath = analyzePath(path, type);
-
- if (addedPath == null && type.$isMongooseArray && !type.$isMongooseDocumentArray) {
- addedPath = analyzePath(path, type.caster);
- }
- if (addedPath != null) {
- addedPaths.push(addedPath);
- }
-
- if (type.schema) {
- const _addedPaths = analyzeSchema(type.schema, path);
-
-
- if (exclude === false) {
- checkEmbeddedDiscriminatorKeyProjection(fields, path, type.schema,
- selected, _addedPaths);
- }
- }
- });
- stack.pop();
- return addedPaths;
- }
- function analyzePath(path, type) {
- const plusPath = '+' + path;
- const hasPlusPath = fields && plusPath in fields;
- if (hasPlusPath) {
-
- delete fields[plusPath];
- }
- if (typeof type.selected !== 'boolean') return;
- if (hasPlusPath) {
-
- delete fields[plusPath];
-
-
- if (exclude === false && keys.length > 1 && !~keys.indexOf(path)) {
- fields[path] = 1;
- }
- return;
- }
-
- const pieces = path.split('.');
- let cur = '';
- for (let i = 0; i < pieces.length; ++i) {
- cur += cur.length ? '.' + pieces[i] : pieces[i];
- if (excluded.indexOf(cur) !== -1) {
- return;
- }
- }
-
-
-
- if (!exclude && get(type, 'options.$skipDiscriminatorCheck', false)) {
- let cur = '';
- for (let i = 0; i < pieces.length; ++i) {
- cur += (cur.length === 0 ? '' : '.') + pieces[i];
- const projection = get(fields, cur, false) || get(fields, cur + '.$', false);
- if (projection && typeof projection !== 'object') {
- return;
- }
- }
- }
- (type.selected ? selected : excluded).push(path);
- return path;
- }
- };
- function makeLean(val) {
- return function(option) {
- option.options || (option.options = {});
- if (val != null && Array.isArray(val.virtuals)) {
- val = Object.assign({}, val);
- val.virtuals = val.virtuals.
- filter(path => typeof path === 'string' && path.startsWith(option.path + '.')).
- map(path => path.slice(option.path.length + 1));
- }
- option.options.lean = val;
- };
- }
- exports.handleDeleteWriteOpResult = function handleDeleteWriteOpResult(callback) {
- return function _handleDeleteWriteOpResult(error, res) {
- if (error) {
- return callback(error);
- }
- const mongooseResult = Object.assign({}, res.result);
- if (get(res, 'result.n', null) != null) {
- mongooseResult.deletedCount = res.result.n;
- }
- if (res.deletedCount != null) {
- mongooseResult.deletedCount = res.deletedCount;
- }
- return callback(null, mongooseResult);
- };
- };
|