/
1Rrd0
/
Course
Обзор
Документация
Войти
/
1Rrd0
/
Course
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
node_modules/mongoose/lib/plugins/saveSubdocs.js
75 строк
2 KB
dima
2
13 дек 2025, 10:58
13 дек 2025, 10:58
ce5a48e
Код
Авторство
О чём код?
'use strict'; /*! * ignore */ module.exports = function saveSubdocs(schema) { const unshift = true; schema.s.hooks.pre('save', false, async function saveSubdocsPreSave() { if (this.$isSubdocument) { return; } const subdocs = this.$getAllSubdocs({ useCache: true }); if (!subdocs.length) { return; } await Promise.all(subdocs.map(subdoc => subdoc._execDocumentPreHooks('save'))); // Invalidate subdocs cache because subdoc pre hooks can add new subdocuments if (this.$__.saveOptions) { this.$__.saveOptions.__subdocs = null; } }, null, unshift); schema.s.hooks.pre('save', async function saveSubdocsPreDeleteOne() { const removedSubdocs = this.$__.removedSubdocs; if (!removedSubdocs || !removedSubdocs.length) { return; } const promises = []; for (const subdoc of removedSubdocs) { promises.push(subdoc._execDocumentPreHooks('deleteOne')); } await Promise.all(promises); }); schema.s.hooks.post('save', async function saveSubdocsPostDeleteOne() { const removedSubdocs = this.$__.removedSubdocs; if (!removedSubdocs || !removedSubdocs.length) { return; } const promises = []; for (const subdoc of removedSubdocs) { promises.push(subdoc._execDocumentPostHooks('deleteOne')); } this.$__.removedSubdocs = null; await Promise.all(promises); }); schema.s.hooks.post('save', async function saveSubdocsPostSave() { if (this.$isSubdocument) { return; } const subdocs = this.$getAllSubdocs({ useCache: true }); if (!subdocs.length) { return; } const promises = []; for (const subdoc of subdocs) { promises.push(subdoc._execDocumentPostHooks('save')); } await Promise.all(promises); }, null, unshift); };