This commit is contained in:
2026-04-02 17:50:10 +08:00
parent 60697d80be
commit ce0aa4f789
373 changed files with 82520 additions and 46 deletions

5
backend/node_modules/mongoose/lib/plugins/index.js generated vendored Normal file
View File

@@ -0,0 +1,5 @@
'use strict';
exports.saveSubdocs = require('./saveSubdocs');
exports.sharding = require('./sharding');
exports.trackTransaction = require('./trackTransaction');

View File

@@ -0,0 +1,94 @@
'use strict';
const symbols = require('../schema/symbols');
/*!
* ignore
*/
module.exports = function saveSubdocs(schema) {
const unshift = true;
schema.s.hooks.pre('save', false, saveSubdocsPreSave, null, unshift);
schema.s.hooks.post('save', saveSubdocsPostSave, null, unshift);
schema.s.hooks.pre('save', saveSubdocsPreDeleteOne);
schema.s.hooks.post('save', saveSubdocsPostDeleteOne);
};
async function saveSubdocsPreSave() {
if (this.$isSubdocument) {
return;
}
const subdocs = this.$getAllSubdocs({ useCache: true });
if (!subdocs.length) {
return;
}
const options = this.$__.saveOptions;
await Promise.all(subdocs.map(subdoc => subdoc._execDocumentPreHooks('save', options, [options])));
// Invalidate subdocs cache because subdoc pre hooks can add new subdocuments
if (this.$__.saveOptions) {
this.$__.saveOptions.__subdocs = null;
}
}
async function saveSubdocsPostSave() {
if (this.$isSubdocument) {
return;
}
const subdocs = this.$getAllSubdocs({ useCache: true });
if (!subdocs.length) {
return;
}
const options = this.$__.saveOptions;
const promises = [];
for (const subdoc of subdocs) {
promises.push(subdoc._execDocumentPostHooks('save', options));
}
await Promise.all(promises);
}
async function saveSubdocsPreDeleteOne() {
const removedSubdocs = this.$__.removedSubdocs;
if (!removedSubdocs?.length) {
return;
}
const options = this.$__.saveOptions;
const promises = [];
for (const subdoc of removedSubdocs) {
promises.push(subdoc._execDocumentPreHooks('deleteOne', options));
}
await Promise.all(promises);
}
async function saveSubdocsPostDeleteOne() {
const removedSubdocs = this.$__.removedSubdocs;
if (!removedSubdocs?.length) {
return;
}
const options = this.$__.saveOptions;
const promises = [];
for (const subdoc of removedSubdocs) {
promises.push(subdoc._execDocumentPostHooks('deleteOne', options));
}
this.$__.removedSubdocs = null;
await Promise.all(promises);
}
saveSubdocsPreSave[symbols.builtInMiddleware] = true;
saveSubdocsPostSave[symbols.builtInMiddleware] = true;
saveSubdocsPreDeleteOne[symbols.builtInMiddleware] = true;
saveSubdocsPostDeleteOne[symbols.builtInMiddleware] = true;

101
backend/node_modules/mongoose/lib/plugins/sharding.js generated vendored Normal file
View File

@@ -0,0 +1,101 @@
'use strict';
const objectIdSymbol = require('../helpers/symbols').objectIdSymbol;
const symbols = require('../schema/symbols');
const utils = require('../utils');
/*!
* ignore
*/
module.exports = function shardingPlugin(schema) {
schema.post('init', shardingPluginPostInit);
schema.pre('save', shardingPluginPreSave);
schema.post('save', shardingPluginPostSave);
schema.pre('deleteOne', { document: true, query: false }, shardingPluginPreDeleteOne);
schema.pre('updateOne', { document: true, query: false }, shardingPluginPreUpdateOne);
};
function shardingPluginPostInit() {
storeShard.call(this);
return this;
}
function shardingPluginPreSave() {
applyWhere.call(this);
}
function shardingPluginPostSave() {
storeShard.call(this);
}
function shardingPluginPreDeleteOne() {
applyWhere.call(this);
}
function shardingPluginPreUpdateOne() {
applyWhere.call(this);
}
/*!
* ignore
*/
function applyWhere() {
let paths;
let len;
if (this.$__.shardval) {
paths = Object.keys(this.$__.shardval);
len = paths.length;
this.$where = this.$where || {};
for (let i = 0; i < len; ++i) {
this.$where[paths[i]] = this.$__.shardval[paths[i]];
}
}
}
/*!
* ignore
*/
module.exports.storeShard = storeShard;
/*!
* ignore
*/
function storeShard() {
// backwards compat
const key = this.$__schema.options.shardKey || this.$__schema.options.shardkey;
if (!utils.isPOJO(key)) {
return;
}
const orig = this.$__.shardval = {};
const paths = Object.keys(key);
const len = paths.length;
let val;
for (let i = 0; i < len; ++i) {
val = this.$__getValue(paths[i]);
if (val == null) {
orig[paths[i]] = val;
} else if (utils.isMongooseObject(val)) {
orig[paths[i]] = val.toObject({ depopulate: true, _isNested: true });
} else if (val instanceof Date || val[objectIdSymbol]) {
orig[paths[i]] = val;
} else if (typeof val.valueOf === 'function') {
orig[paths[i]] = val.valueOf();
} else {
orig[paths[i]] = val;
}
}
}
shardingPluginPostInit[symbols.builtInMiddleware] = true;
shardingPluginPreSave[symbols.builtInMiddleware] = true;
shardingPluginPostSave[symbols.builtInMiddleware] = true;
shardingPluginPreDeleteOne[symbols.builtInMiddleware] = true;
shardingPluginPreUpdateOne[symbols.builtInMiddleware] = true;

View File

@@ -0,0 +1,89 @@
'use strict';
const arrayAtomicsSymbol = require('../helpers/symbols').arrayAtomicsSymbol;
const sessionNewDocuments = require('../helpers/symbols').sessionNewDocuments;
const symbols = require('../schema/symbols');
const utils = require('../utils');
module.exports = function trackTransaction(schema) {
schema.pre('save', trackTransactionPreSave);
};
function trackTransactionPreSave() {
const session = this.$session();
if (session == null) {
return;
}
if (session.transaction == null || session[sessionNewDocuments] == null) {
return;
}
if (!session[sessionNewDocuments].has(this)) {
const initialState = {};
if (this.isNew) {
initialState.isNew = true;
}
if (this.$__schema.options.versionKey) {
initialState.versionKey = this.get(this.$__schema.options.versionKey);
}
initialState.modifiedPaths = new Set(Object.keys(this.$__.activePaths.getStatePaths('modify')));
initialState.atomics = _getAtomics(this);
session[sessionNewDocuments].set(this, initialState);
}
}
function _getAtomics(doc, previous) {
const pathToAtomics = new Map();
previous = previous || new Map();
const pathsToCheck = Object.keys(doc.$__.activePaths.init).concat(Object.keys(doc.$__.activePaths.modify));
for (const path of pathsToCheck) {
const val = doc.$__getValue(path);
if (Array.isArray(val) &&
utils.isMongooseDocumentArray(val) &&
val.length &&
val[arrayAtomicsSymbol] != null &&
utils.hasOwnKeys(val[arrayAtomicsSymbol])) {
const existing = previous.get(path) || {};
pathToAtomics.set(path, mergeAtomics(existing, val[arrayAtomicsSymbol]));
}
}
const dirty = doc.$__dirty();
for (const dirt of dirty) {
const path = dirt.path;
const val = dirt.value;
if (val?.[arrayAtomicsSymbol] != null && utils.hasOwnKeys(val[arrayAtomicsSymbol])) {
const existing = previous.get(path) || {};
pathToAtomics.set(path, mergeAtomics(existing, val[arrayAtomicsSymbol]));
}
}
return pathToAtomics;
}
function mergeAtomics(destination, source) {
destination = destination || {};
if (source.$pullAll != null) {
destination.$pullAll = (destination.$pullAll || []).concat(source.$pullAll);
}
if (source.$push != null) {
destination.$push = destination.$push || {};
destination.$push.$each = (destination.$push.$each || []).concat(source.$push.$each);
}
if (source.$addToSet != null) {
destination.$addToSet = (destination.$addToSet || []).concat(source.$addToSet);
}
if (source.$set != null) {
destination.$set = Array.isArray(source.$set) ? [...source.$set] : Object.assign({}, source.$set);
}
return destination;
}
trackTransactionPreSave[symbols.builtInMiddleware] = true;