From 83daf5574198d2458f93d72db6996757697f9c0b Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Tue, 4 Apr 2023 11:34:20 +0800 Subject: [PATCH] chore: add transaction in set field action (#1647) --- .../src/resourcers/collections.ts | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/packages/plugins/collection-manager/src/resourcers/collections.ts b/packages/plugins/collection-manager/src/resourcers/collections.ts index 258241af26..78b954dfc9 100644 --- a/packages/plugins/collection-manager/src/resourcers/collections.ts +++ b/packages/plugins/collection-manager/src/resourcers/collections.ts @@ -4,28 +4,40 @@ export default { async ['collections:setFields'](ctx, next) { const { filterByTk, values } = ctx.action.params; - // WARN: 删掉 key 才能保存 - const fields = values.fields?.map(f => { - delete f.key; - return f; - }); + const transaction = await ctx.app.db.sequelize.transaction(); - const db = ctx.app.db as Database; + try { + const fields = values.fields?.map((f) => { + delete f.key; + return f; + }); - const collection = await db.getRepository('collections').findOne({ - filter: { - name: filterByTk, - }, - }); + const db = ctx.app.db as Database; - await db.getRepository('collections').update({ - filterByTk, - values: { - fields, - }, - }); + const collection = await db.getRepository('collections').findOne({ + filter: { + name: filterByTk, + }, + transaction, + }); - await collection.loadFields(); + await db.getRepository('collections').update({ + filterByTk, + values: { + fields, + }, + transaction, + }); + + await collection.loadFields({ + transaction, + }); + + await transaction.commit(); + } catch (e) { + await transaction.rollback(); + throw e; + } await next(); },