This commit is contained in:
PLBXNebulia-Formation 2025-11-21 09:23:11 +01:00
commit d1c8cae2c1
1417 changed files with 326736 additions and 0 deletions

View file

@ -0,0 +1,132 @@
'use strict';
const isNestedProjection = require('../projection/isNestedProjection');
module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildren, isBeforeSetters, pathsToSkip, options) {
const paths = Object.keys(doc.$__schema.paths);
const plen = paths.length;
const skipParentChangeTracking = options && options.skipParentChangeTracking;
for (let i = 0; i < plen; ++i) {
let def;
let curPath = '';
const p = paths[i];
if (p === '_id' && doc.$__.skipId) {
continue;
}
const type = doc.$__schema.paths[p];
const path = type.splitPath();
const len = path.length;
if (path[len - 1] === '$*') {
continue;
}
let included = false;
let doc_ = doc._doc;
for (let j = 0; j < len; ++j) {
if (doc_ == null) {
break;
}
const piece = path[j];
curPath += (!curPath.length ? '' : '.') + piece;
if (exclude === true) {
if (curPath in fields) {
break;
}
} else if (exclude === false && fields && !included) {
const hasSubpaths = type.$isSingleNested || type.$isMongooseDocumentArray;
if ((curPath in fields && !isNestedProjection(fields[curPath])) || (j === len - 1 && hasSubpaths && hasIncludedChildren != null && hasIncludedChildren[curPath])) {
included = true;
} else if (hasIncludedChildren != null && !hasIncludedChildren[curPath]) {
break;
}
}
if (j === len - 1) {
if (doc_[piece] !== void 0) {
break;
}
if (isBeforeSetters != null) {
if (typeof type.defaultValue === 'function') {
if (!type.defaultValue.$runBeforeSetters && isBeforeSetters) {
break;
}
if (type.defaultValue.$runBeforeSetters && !isBeforeSetters) {
break;
}
} else if (!isBeforeSetters) {
// Non-function defaults should always run **before** setters
continue;
}
}
if (pathsToSkip && pathsToSkip[curPath]) {
break;
}
if (fields && exclude !== null) {
if (exclude === true) {
// apply defaults to all non-excluded fields
if (p in fields) {
continue;
}
try {
def = type.getDefault(doc, false);
} catch (err) {
doc.invalidate(p, err);
break;
}
if (typeof def !== 'undefined') {
doc_[piece] = def;
applyChangeTracking(doc, p, skipParentChangeTracking);
}
} else if (included) {
// selected field
try {
def = type.getDefault(doc, false);
} catch (err) {
doc.invalidate(p, err);
break;
}
if (typeof def !== 'undefined') {
doc_[piece] = def;
applyChangeTracking(doc, p, skipParentChangeTracking);
}
}
} else {
try {
def = type.getDefault(doc, false);
} catch (err) {
doc.invalidate(p, err);
break;
}
if (typeof def !== 'undefined') {
doc_[piece] = def;
applyChangeTracking(doc, p, skipParentChangeTracking);
}
}
} else {
doc_ = doc_[piece];
}
}
}
};
/*!
* ignore
*/
function applyChangeTracking(doc, fullPath, skipParentChangeTracking) {
doc.$__.activePaths.default(fullPath);
if (!skipParentChangeTracking && doc.$isSubdocument && doc.$isSingleNested && doc.$parent() != null) {
doc.$parent().$__.activePaths.default(doc.$__pathRelativeToParent(fullPath));
}
}

View file

@ -0,0 +1,105 @@
'use strict';
const handleTimestampOption = require('../schema/handleTimestampOption');
const mpath = require('mpath');
module.exports = applyTimestamps;
/**
* Apply a given schema's timestamps to the given POJO
*
* @param {Schema} schema
* @param {Object} obj
* @param {Object} [options]
* @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
*/
function applyTimestamps(schema, obj, options) {
if (obj == null) {
return obj;
}
applyTimestampsToChildren(schema, obj, options);
return applyTimestampsToDoc(schema, obj, options);
}
/**
* Apply timestamps to any subdocuments
*
* @param {Schema} schema subdocument schema
* @param {Object} res subdocument
* @param {Object} [options]
* @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
*/
function applyTimestampsToChildren(schema, res, options) {
for (const childSchema of schema.childSchemas) {
const _path = childSchema.model.path;
const _schema = childSchema.schema;
if (!_path) {
continue;
}
const _obj = mpath.get(_path, res);
if (_obj == null || (Array.isArray(_obj) && _obj.flat(Infinity).length === 0)) {
continue;
}
applyTimestamps(_schema, _obj, options);
}
}
/**
* Apply timestamps to a given document. Does not apply timestamps to subdocuments: use `applyTimestampsToChildren` instead
*
* @param {Schema} schema
* @param {Object} obj
* @param {Object} [options]
* @param {Boolean} [options.isUpdate=false] if true, treat this as an update: just set updatedAt, skip setting createdAt. If false, set both createdAt and updatedAt
* @param {Function} [options.currentTime] if set, Mongoose will call this function to get the current time.
*/
function applyTimestampsToDoc(schema, obj, options) {
if (obj == null || typeof obj !== 'object') {
return;
}
if (Array.isArray(obj)) {
for (const el of obj) {
applyTimestampsToDoc(schema, el, options);
}
return;
}
if (schema.discriminators && Object.keys(schema.discriminators).length > 0) {
for (const discriminatorKey of Object.keys(schema.discriminators)) {
const discriminator = schema.discriminators[discriminatorKey];
const key = discriminator.discriminatorMapping.key;
const value = discriminator.discriminatorMapping.value;
if (obj[key] == value) {
schema = discriminator;
break;
}
}
}
const createdAt = handleTimestampOption(schema.options.timestamps, 'createdAt');
const updatedAt = handleTimestampOption(schema.options.timestamps, 'updatedAt');
const currentTime = options?.currentTime;
let ts = null;
if (currentTime != null) {
ts = currentTime();
} else if (schema.base?.now) {
ts = schema.base.now();
} else {
ts = new Date();
}
if (createdAt && obj[createdAt] == null && !options?.isUpdate) {
obj[createdAt] = ts;
}
if (updatedAt) {
obj[updatedAt] = ts;
}
}

View file

@ -0,0 +1,146 @@
'use strict';
const mpath = require('mpath');
module.exports = applyVirtuals;
/**
* Apply a given schema's virtuals to a given POJO
*
* @param {Schema} schema
* @param {Object} obj
* @param {Array<string>} [virtuals] optional whitelist of virtuals to apply
* @returns
*/
function applyVirtuals(schema, obj, virtuals) {
if (obj == null) {
return obj;
}
let virtualsForChildren = virtuals;
let toApply = null;
if (Array.isArray(virtuals)) {
virtualsForChildren = [];
toApply = [];
for (const virtual of virtuals) {
if (virtual.length === 1) {
toApply.push(virtual[0]);
} else {
virtualsForChildren.push(virtual);
}
}
}
applyVirtualsToChildren(schema, obj, virtualsForChildren);
return applyVirtualsToDoc(schema, obj, toApply);
}
/**
* Apply virtuals to any subdocuments
*
* @param {Schema} schema subdocument schema
* @param {Object} res subdocument
* @param {Array<String>} [virtuals] optional whitelist of virtuals to apply
*/
function applyVirtualsToChildren(schema, res, virtuals) {
let attachedVirtuals = false;
for (const childSchema of schema.childSchemas) {
const _path = childSchema.model.path;
const _schema = childSchema.schema;
if (!_path) {
continue;
}
const _obj = mpath.get(_path, res);
if (_obj == null || (Array.isArray(_obj) && _obj.flat(Infinity).length === 0)) {
continue;
}
let virtualsForChild = null;
if (Array.isArray(virtuals)) {
virtualsForChild = [];
for (const virtual of virtuals) {
if (virtual[0] == _path) {
virtualsForChild.push(virtual.slice(1));
}
}
if (virtualsForChild.length === 0) {
continue;
}
}
applyVirtuals(_schema, _obj, virtualsForChild);
attachedVirtuals = true;
}
if (virtuals && virtuals.length && !attachedVirtuals) {
applyVirtualsToDoc(schema, res, virtuals);
}
}
/**
* Apply virtuals to a given document. Does not apply virtuals to subdocuments: use `applyVirtualsToChildren` instead
*
* @param {Schema} schema
* @param {Object} doc
* @param {Array<String>} [virtuals] optional whitelist of virtuals to apply
* @returns
*/
function applyVirtualsToDoc(schema, obj, virtuals) {
if (obj == null || typeof obj !== 'object') {
return;
}
if (Array.isArray(obj)) {
for (const el of obj) {
applyVirtualsToDoc(schema, el, virtuals);
}
return;
}
if (schema.discriminators && Object.keys(schema.discriminators).length > 0) {
for (const discriminatorKey of Object.keys(schema.discriminators)) {
const discriminator = schema.discriminators[discriminatorKey];
const key = discriminator.discriminatorMapping.key;
const value = discriminator.discriminatorMapping.value;
if (obj[key] == value) {
schema = discriminator;
break;
}
}
}
if (virtuals == null) {
virtuals = Object.keys(schema.virtuals);
}
for (const virtual of virtuals) {
if (schema.virtuals[virtual] == null) {
continue;
}
const virtualType = schema.virtuals[virtual];
const sp = Array.isArray(virtual)
? virtual
: virtual.indexOf('.') === -1
? [virtual]
: virtual.split('.');
let cur = obj;
for (let i = 0; i < sp.length - 1; ++i) {
cur[sp[i]] = sp[i] in cur ? cur[sp[i]] : {};
cur = cur[sp[i]];
}
let val = virtualType.applyGetters(cur[sp[sp.length - 1]], obj);
const isPopulateVirtual =
virtualType.options && (virtualType.options.ref || virtualType.options.refPath);
if (isPopulateVirtual && val === undefined) {
if (virtualType.options.justOne) {
val = null;
} else {
val = [];
}
}
cur[sp[sp.length - 1]] = val;
}
}

View file

@ -0,0 +1,45 @@
'use strict';
/*!
* ignore
*/
module.exports = function cleanModifiedSubpaths(doc, path, options) {
options = options || {};
const skipDocArrays = options.skipDocArrays;
let deleted = 0;
if (!doc) {
return deleted;
}
for (const modifiedPath of Object.keys(doc.$__.activePaths.getStatePaths('modify'))) {
if (skipDocArrays) {
const schemaType = doc.$__schema.path(modifiedPath);
if (schemaType && schemaType.$isMongooseDocumentArray) {
continue;
}
}
if (modifiedPath.startsWith(path + '.')) {
doc.$__.activePaths.clearPath(modifiedPath);
++deleted;
if (doc.$isSubdocument) {
cleanParent(doc, modifiedPath);
}
}
}
return deleted;
};
function cleanParent(doc, path, seen = new Set()) {
if (seen.has(doc)) {
throw new Error('Infinite subdocument loop: subdoc with _id ' + doc._id + ' is a parent of itself');
}
const parent = doc.$parent();
const newPath = doc.$__pathRelativeToParent(void 0, false) + '.' + path;
parent.$__.activePaths.clearPath(newPath);
if (parent.$isSubdocument) {
cleanParent(parent, newPath, seen);
}
}

238
node_modules/mongoose/lib/helpers/document/compile.js generated vendored Normal file
View file

@ -0,0 +1,238 @@
'use strict';
const clone = require('../../helpers/clone');
const documentSchemaSymbol = require('../../helpers/symbols').documentSchemaSymbol;
const internalToObjectOptions = require('../../options').internalToObjectOptions;
const utils = require('../../utils');
let Document;
const getSymbol = require('../../helpers/symbols').getSymbol;
const scopeSymbol = require('../../helpers/symbols').scopeSymbol;
const isPOJO = utils.isPOJO;
/*!
* exports
*/
exports.compile = compile;
exports.defineKey = defineKey;
const _isEmptyOptions = Object.freeze({
minimize: true,
virtuals: false,
getters: false,
transform: false
});
const noDottedPathGetOptions = Object.freeze({
noDottedPath: true
});
/**
* Compiles schemas.
* @param {Object} tree
* @param {Any} proto
* @param {String} prefix
* @param {Object} options
* @api private
*/
function compile(tree, proto, prefix, options) {
Document = Document || require('../../document');
const typeKey = options.typeKey;
for (const key of Object.keys(tree)) {
const limb = tree[key];
const hasSubprops = isPOJO(limb) &&
Object.keys(limb).length > 0 &&
(!limb[typeKey] || (typeKey === 'type' && isPOJO(limb.type) && limb.type.type));
const subprops = hasSubprops ? limb : null;
defineKey({ prop: key, subprops: subprops, prototype: proto, prefix: prefix, options: options });
}
}
/**
* Defines the accessor named prop on the incoming prototype.
* @param {Object} options
* @param {String} options.prop
* @param {Boolean} options.subprops
* @param {Any} options.prototype
* @param {String} [options.prefix]
* @param {Object} options.options
* @api private
*/
function defineKey({ prop, subprops, prototype, prefix, options }) {
Document = Document || require('../../document');
const path = (prefix ? prefix + '.' : '') + prop;
prefix = prefix || '';
const useGetOptions = prefix ? Object.freeze({}) : noDottedPathGetOptions;
if (subprops) {
Object.defineProperty(prototype, prop, {
enumerable: true,
configurable: true,
get: function() {
const _this = this;
if (!this.$__.getters) {
this.$__.getters = {};
}
if (!this.$__.getters[path]) {
const nested = Object.create(Document.prototype, getOwnPropertyDescriptors(this));
// save scope for nested getters/setters
if (!prefix) {
nested.$__[scopeSymbol] = this;
}
nested.$__.nestedPath = path;
Object.defineProperty(nested, 'schema', {
enumerable: false,
configurable: true,
writable: false,
value: prototype.schema
});
Object.defineProperty(nested, '$__schema', {
enumerable: false,
configurable: true,
writable: false,
value: prototype.schema
});
Object.defineProperty(nested, documentSchemaSymbol, {
enumerable: false,
configurable: true,
writable: false,
value: prototype.schema
});
Object.defineProperty(nested, 'toObject', {
enumerable: false,
configurable: true,
writable: false,
value: function() {
return clone(_this.get(path, null, {
virtuals: this &&
this.schema &&
this.schema.options &&
this.schema.options.toObject &&
this.schema.options.toObject.virtuals || null
}));
}
});
Object.defineProperty(nested, '$__get', {
enumerable: false,
configurable: true,
writable: false,
value: function() {
return _this.get(path, null, {
virtuals: this && this.schema && this.schema.options && this.schema.options.toObject && this.schema.options.toObject.virtuals || null
});
}
});
Object.defineProperty(nested, 'toJSON', {
enumerable: false,
configurable: true,
writable: false,
value: function() {
return _this.get(path, null, {
virtuals: this && this.schema && this.schema.options && this.schema.options.toJSON && this.schema.options.toJSON.virtuals || null
});
}
});
Object.defineProperty(nested, '$__isNested', {
enumerable: false,
configurable: true,
writable: false,
value: true
});
Object.defineProperty(nested, '$isEmpty', {
enumerable: false,
configurable: true,
writable: false,
value: function() {
return Object.keys(this.get(path, null, _isEmptyOptions) || {}).length === 0;
}
});
Object.defineProperty(nested, '$__parent', {
enumerable: false,
configurable: true,
writable: false,
value: this
});
compile(subprops, nested, path, options);
this.$__.getters[path] = nested;
}
return this.$__.getters[path];
},
set: function(v) {
if (v != null && v.$__isNested) {
// Convert top-level to POJO, but leave subdocs hydrated so `$set`
// can handle them. See gh-9293.
v = v.$__get();
} else if (v instanceof Document && !v.$__isNested) {
v = v.$toObject(internalToObjectOptions);
}
const doc = this.$__[scopeSymbol] || this;
doc.$set(path, v);
}
});
} else {
Object.defineProperty(prototype, prop, {
enumerable: true,
configurable: true,
get: function() {
return this[getSymbol].call(
this.$__[scopeSymbol] || this,
path,
null,
useGetOptions
);
},
set: function(v) {
this.$set.call(this.$__[scopeSymbol] || this, path, v);
}
});
}
}
// gets descriptors for all properties of `object`
// makes all properties non-enumerable to match previous behavior to #2211
function getOwnPropertyDescriptors(object) {
const result = {};
Object.getOwnPropertyNames(object).forEach(function(key) {
const skip = [
'isNew',
'$__',
'$errors',
'errors',
'_doc',
'$locals',
'$op',
'__parentArray',
'__index',
'$isDocumentArrayElement'
].indexOf(key) === -1;
if (skip) {
return;
}
result[key] = Object.getOwnPropertyDescriptor(object, key);
result[key].enumerable = false;
});
return result;
}

View file

@ -0,0 +1,38 @@
'use strict';
/**
* Find the deepest subdocument along a given path to ensure setter functions run
* with the correct subdocument as `this`. If no subdocuments, returns the top-level
* document.
*
* @param {Document} doc
* @param {String[]} parts
* @param {Schema} schema
* @returns Document
*/
module.exports = function getDeepestSubdocumentForPath(doc, parts, schema) {
let curPath = parts[0];
let curSchema = schema;
let subdoc = doc;
for (let i = 0; i < parts.length - 1; ++i) {
const curSchemaType = curSchema.path(curPath);
if (curSchemaType && curSchemaType.schema) {
let newSubdoc = subdoc.get(curPath);
curSchema = curSchemaType.schema;
curPath = parts[i + 1];
if (Array.isArray(newSubdoc) && !isNaN(curPath)) {
newSubdoc = newSubdoc[curPath];
curPath = '';
}
if (newSubdoc == null) {
break;
}
subdoc = newSubdoc;
} else {
curPath += curPath.length ? '.' + parts[i + 1] : parts[i + 1];
}
}
return subdoc;
};

View file

@ -0,0 +1,53 @@
'use strict';
const get = require('../get');
const getSchemaDiscriminatorByValue = require('../discriminator/getSchemaDiscriminatorByValue');
/**
* Like `schema.path()`, except with a document, because impossible to
* determine path type without knowing the embedded discriminator key.
*
* @param {Document} doc
* @param {String|String[]} path
* @param {Object} [options]
* @api private
*/
module.exports = function getEmbeddedDiscriminatorPath(doc, path, options) {
options = options || {};
const typeOnly = options.typeOnly;
const parts = Array.isArray(path) ?
path :
(path.indexOf('.') === -1 ? [path] : path.split('.'));
let schemaType = null;
let type = 'adhocOrUndefined';
const schema = getSchemaDiscriminatorByValue(doc.schema, doc.get(doc.schema.options.discriminatorKey)) || doc.schema;
for (let i = 0; i < parts.length; ++i) {
const subpath = parts.slice(0, i + 1).join('.');
schemaType = schema.path(subpath);
if (schemaType == null) {
type = 'adhocOrUndefined';
continue;
}
if (schemaType.instance === 'Mixed') {
return typeOnly ? 'real' : schemaType;
}
type = schema.pathType(subpath);
if ((schemaType.$isSingleNested || schemaType.$isMongooseDocumentArrayElement) &&
schemaType.schema.discriminators != null) {
const discriminators = schemaType.schema.discriminators;
const discriminatorKey = doc.get(subpath + '.' +
get(schemaType, 'schema.options.discriminatorKey'));
if (discriminatorKey == null || discriminators[discriminatorKey] == null) {
continue;
}
const rest = parts.slice(i + 1).join('.');
return getEmbeddedDiscriminatorPath(doc.get(subpath), rest, options);
}
}
// Are we getting the whole schema or just the type, 'real', 'nested', etc.
return typeOnly ? type : schemaType;
};

View file

@ -0,0 +1,35 @@
'use strict';
const utils = require('../../utils');
const keysToSkip = new Set(['__index', '__parentArray', '_doc']);
/**
* Using spread operator on a Mongoose document gives you a
* POJO that has a tendency to cause infinite recursion. So
* we use this function on `set()` to prevent that.
*/
module.exports = function handleSpreadDoc(v, includeExtraKeys) {
if (utils.isPOJO(v) && v.$__ != null && v._doc != null) {
if (includeExtraKeys) {
const extraKeys = {};
for (const key of Object.keys(v)) {
if (typeof key === 'symbol') {
continue;
}
if (key[0] === '$') {
continue;
}
if (keysToSkip.has(key)) {
continue;
}
extraKeys[key] = v[key];
}
return { ...v._doc, ...extraKeys };
}
return v._doc;
}
return v;
};