diff --git a/packages/plugins/@nocobase/plugin-collection-tree/src/server/plugin.ts b/packages/plugins/@nocobase/plugin-collection-tree/src/server/plugin.ts index 10ab1412f2..51063f7f8b 100644 --- a/packages/plugins/@nocobase/plugin-collection-tree/src/server/plugin.ts +++ b/packages/plugins/@nocobase/plugin-collection-tree/src/server/plugin.ts @@ -31,6 +31,7 @@ class PluginCollectionTreeServer extends Plugin { if (!condition(collection.options)) { return; } + const tk = collection.filterTargetKey; const name = `${dataSource.name}_${collection.name}_path`; const parentForeignKey = collection.treeParentField?.foreignKey || 'parentId'; @@ -50,9 +51,8 @@ class PluginCollectionTreeServer extends Plugin { //afterCreate this.db.on(`${collection.name}.afterCreate`, async (model: Model, options) => { const { transaction } = options; - const tk = collection.filterTargetKey; let path = `/${model.get(tk)}`; - path = await this.getTreePath(model, path, collection, name, transaction); + path = await this.getTreePath(model, path, collection, tk, name, transaction); const rootPk = path.split('/')[1]; await this.app.db.getRepository(name).create({ values: { @@ -66,18 +66,16 @@ class PluginCollectionTreeServer extends Plugin { //afterUpdate this.db.on(`${collection.name}.afterUpdate`, async (model: Model, options) => { - const tk = collection.filterTargetKey; // only update parentId and filterTargetKey if (!(model._changed.has(tk) || model._changed.has(parentForeignKey))) { return; } const { transaction } = options; - await this.updateTreePath(model, collection, name, transaction); + await this.updateTreePath(model, collection, tk, name, transaction); }); // after remove this.db.on(`${collection.name}.afterBulkUpdate`, async (options) => { - const tk = collection.filterTargetKey; if (!(options.where && options.where[tk])) { return; } @@ -88,13 +86,12 @@ class PluginCollectionTreeServer extends Plugin { transaction: options.transaction, }); for (const model of instances) { - await this.updateTreePath(model, collection, name, options.transaction); + await this.updateTreePath(model, collection, tk, name, options.transaction); } }); //afterDestroy this.db.on(`${collection.name}.afterDestroy`, async (model: Model, options: DestroyOptions) => { - const tk = collection.filterTargetKey; await this.app.db.getRepository(name).destroy({ filter: { nodePk: model.get(tk), @@ -142,10 +139,10 @@ class PluginCollectionTreeServer extends Plugin { model: Model, path: string, collection: Collection, + tk: string, pathCollectionName: string, transaction?: Transaction, ) { - const tk = collection.filterTargetKey; const parentForeignKey = collection.treeParentField?.foreignKey || 'parentId'; if (model.get(parentForeignKey) && model.get(parentForeignKey) !== null) { const parent = await this.app.db.getRepository(collection.name).findOne({ @@ -167,7 +164,7 @@ class PluginCollectionTreeServer extends Plugin { }); const parentPath = lodash.get(parentPathData, 'path', null); if (parentPath == null) { - path = await this.getTreePath(parent, path, collection, pathCollectionName, transaction); + path = await this.getTreePath(parent, path, collection, tk, pathCollectionName, transaction); } else { path = `${parentPath}/${model.get(tk)}`; } @@ -180,12 +177,12 @@ class PluginCollectionTreeServer extends Plugin { private async updateTreePath( model: Model, collection: Collection, + tk: string, pathCollectionName: string, transaction: Transaction, ) { - const tk = collection.filterTargetKey; let path = `/${model.get(tk)}`; - path = await this.getTreePath(model, path, collection, pathCollectionName, transaction); + path = await this.getTreePath(model, path, collection, tk, pathCollectionName, transaction); const collectionTreePath = this.db.getCollection(pathCollectionName); const nodePkColumnName = collectionTreePath.getField('nodePk').columnName(); const pathData = await this.app.db.getRepository(pathCollectionName).findOne({