7fife-backend/node_modules/mongoose/lib/helpers/document/getDeepestSubdocumentForPat...

39 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-03-07 13:01:44 +00:00
'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;
};