This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -75,9 +75,12 @@ function applyHooks(model, schema, options) {
if (hook.name === 'updateOne' || hook.name === 'deleteOne') {
return !!hook['document'];
}
if (hook.name === 'remove') {
if (hook.name === 'remove' || hook.name === 'init') {
return hook['document'] == null || !!hook['document'];
}
if (hook.query != null || hook.document != null) {
return hook.document !== false;
}
return true;
}).
filter(hook => {

View File

@@ -20,7 +20,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
const model = decideModelByObject(originalModel, op['insertOne']['document']);
const doc = new model(op['insertOne']['document']);
if (model.schema.options.timestamps != null) {
if (model.schema.options.timestamps) {
doc.initializeTimestamps();
}
if (options.session != null) {
@@ -37,8 +37,12 @@ module.exports = function castBulkWrite(originalModel, op, options) {
} else if (op['updateOne']) {
return (callback) => {
try {
if (!op['updateOne']['filter']) throw new Error('Must provide a filter object.');
if (!op['updateOne']['update']) throw new Error('Must provide an update object.');
if (!op['updateOne']['filter']) {
throw new Error('Must provide a filter object.');
}
if (!op['updateOne']['update']) {
throw new Error('Must provide an update object.');
}
const model = decideModelByObject(originalModel, op['updateOne']['filter']);
const schema = model.schema;
@@ -54,7 +58,6 @@ module.exports = function castBulkWrite(originalModel, op, options) {
applyTimestampsToChildren(now, op['updateOne']['update'], model.schema);
if (op['updateOne'].setDefaultsOnInsert) {
setDefaultsOnInsert(op['updateOne']['filter'], model.schema, op['updateOne']['update'], {
setDefaultsOnInsert: true,
@@ -71,8 +74,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
strict: strict,
overwrite: false,
upsert: op['updateOne'].upsert
});
}, model, op['updateOne']['filter']);
} catch (error) {
return callback(error, null);
}
@@ -82,8 +84,12 @@ module.exports = function castBulkWrite(originalModel, op, options) {
} else if (op['updateMany']) {
return (callback) => {
try {
if (!op['updateMany']['filter']) throw new Error('Must provide a filter object.');
if (!op['updateMany']['update']) throw new Error('Must provide an update object.');
if (!op['updateMany']['filter']) {
throw new Error('Must provide a filter object.');
}
if (!op['updateMany']['update']) {
throw new Error('Must provide an update object.');
}
const model = decideModelByObject(originalModel, op['updateMany']['filter']);
const schema = model.schema;
@@ -115,7 +121,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
strict: strict,
overwrite: false,
upsert: op['updateMany'].upsert
});
}, model, op['updateMany']['filter']);
} catch (error) {
return callback(error, null);
@@ -141,7 +147,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
// set `skipId`, otherwise we get "_id field cannot be changed"
const doc = new model(op['replaceOne']['replacement'], strict, true);
if (model.schema.options.timestamps != null) {
if (model.schema.options.timestamps) {
doc.initializeTimestamps();
}
if (options.session != null) {
@@ -153,6 +159,7 @@ module.exports = function castBulkWrite(originalModel, op, options) {
if (error) {
return callback(error, null);
}
op['replaceOne']['replacement'] = op['replaceOne']['replacement'].toBSON();
callback(null);
});
};
@@ -211,7 +218,7 @@ function _addDiscriminatorToObject(schema, obj) {
function decideModelByObject(model, object) {
const discriminatorKey = model.schema.options.discriminatorKey;
if (object != null && object.hasOwnProperty(discriminatorKey)) {
model = getDiscriminatorByValue(model, object[discriminatorKey]) || model;
model = getDiscriminatorByValue(model.discriminators, object[discriminatorKey]) || model;
}
return model;
}
}

View File

@@ -1,5 +1,6 @@
'use strict';
const Mixed = require('../../schema/mixed');
const defineKey = require('../document/compile').defineKey;
const get = require('../get');
const utils = require('../../utils');
@@ -62,7 +63,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
}
let value = name;
if (typeof tiedValue == 'string' && tiedValue.length) {
if ((typeof tiedValue === 'string' && tiedValue.length) || tiedValue != null) {
value = tiedValue;
}
@@ -84,22 +85,25 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
for (const path of baseSchemaPaths) {
if (schema.nested[path]) {
conflictingPaths.push(path);
continue;
}
if (path.indexOf('.') === -1) {
continue;
}
const sp = path.split('.');
const sp = path.split('.').slice(0, -1);
let cur = '';
for (const piece of sp) {
cur += (cur.length ? '.' : '') + piece;
if (schema.paths[cur] || schema.singleNestedPaths[cur]) {
if (schema.paths[cur] instanceof Mixed ||
schema.singleNestedPaths[cur] instanceof Mixed) {
conflictingPaths.push(path);
}
}
}
utils.merge(schema, baseSchema, {
isDiscriminatorSchemaMerge: true,
omit: { discriminators: true, base: true },
omitNested: conflictingPaths.reduce((cur, path) => {
cur['tree.' + path] = true;
@@ -122,17 +126,17 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
default: value,
select: true,
set: function(newName) {
if (newName === value) {
if (newName === value || (Array.isArray(value) && utils.deepEqual(newName, value))) {
return value;
}
throw new Error('Can\'t set discriminator key "' + key + '"');
},
$skipDiscriminatorCheck: true
};
obj[key][schema.options.typeKey] = existingPath ?
existingPath.instance :
String;
obj[key][schema.options.typeKey] = existingPath ? existingPath.options[schema.options.typeKey] : String;
schema.add(obj);
schema.discriminatorMapping = { key: key, value: value, isRoot: false };
if (baseSchema.options.collection) {
@@ -149,11 +153,17 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu
for (const _key of keys) {
if (!CUSTOMIZABLE_DISCRIMINATOR_OPTIONS[_key]) {
// Special case: compiling a model sets `pluralization = true` by default. Avoid throwing an error
// for that case. See gh-9238
if (_key === 'pluralization' && schema.options[_key] == true && baseSchema.options[_key] == null) {
continue;
}
if (!utils.deepEqual(schema.options[_key], baseSchema.options[_key])) {
throw new Error('Can\'t customize discriminator option ' + _key +
' (can only modify ' +
Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
')');
' (can only modify ' +
Object.keys(CUSTOMIZABLE_DISCRIMINATOR_OPTIONS).join(', ') +
')');
}
}
}