From 184adb924d1f563780dc44a3d1ee67081b5a5b50 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 18 Dec 2020 15:50:03 +0800 Subject: [PATCH] fix: use database hook to trigger add createdBy/updatedBy fields --- .../plugin-users/src/__tests__/fields.test.ts | 2 +- .../src/hooks/collection-after-create.ts | 38 +++++++++++-------- packages/plugin-users/src/server.ts | 15 ++++++-- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/packages/plugin-users/src/__tests__/fields.test.ts b/packages/plugin-users/src/__tests__/fields.test.ts index 513509acf..a9fcad03b 100644 --- a/packages/plugin-users/src/__tests__/fields.test.ts +++ b/packages/plugin-users/src/__tests__/fields.test.ts @@ -62,7 +62,7 @@ describe('user fields', () => { }); // TODO(bug): 重复添加字段不能与 fields 表同步,应做到同步 - it.only('add model and then add createdBy/updatedBy field', async () => { + it('add model and then add createdBy/updatedBy field', async () => { const Collection = db.getModel('collections'); const collection = await Collection.import({ name: 'posts', diff --git a/packages/plugin-users/src/hooks/collection-after-create.ts b/packages/plugin-users/src/hooks/collection-after-create.ts index b78bf20b6..98604a2e8 100644 --- a/packages/plugin-users/src/hooks/collection-after-create.ts +++ b/packages/plugin-users/src/hooks/collection-after-create.ts @@ -1,4 +1,4 @@ -function makeOptions(type: string, options: any) { +export function makeOptions(type: string, options: any) { if (!options) { return; } @@ -21,22 +21,30 @@ function makeOptions(type: string, options: any) { }; } -export default async function(model, options) { - const { database } = model; - const tableName = model.get('name') as string; - const table = database.getTable(tableName); +export default async function(table) { const { createdBy, updatedBy } = table.getOptions(); const fieldsToMake = { createdBy, updatedBy }; - const addedFields = Object.keys(fieldsToMake) + Object.keys(fieldsToMake) .filter(type => Boolean(fieldsToMake[type])) .map(type => table.addField(makeOptions(type, fieldsToMake[type]))); - - if (addedFields.length) { - await table.sync({ - force: false, - alter: { - drop: false, - } - }); - } } + +// export default async function(model, options) { +// const { database } = model; +// const tableName = model.get('name') as string; +// const table = database.getTable(tableName); +// const { createdBy, updatedBy } = table.getOptions(); +// const fieldsToMake = { createdBy, updatedBy }; +// const addedFields = Object.keys(fieldsToMake) +// .filter(type => Boolean(fieldsToMake[type])) +// .map(type => table.addField(makeOptions(type, fieldsToMake[type]))); + +// if (addedFields.length) { +// await table.sync({ +// force: false, +// alter: { +// drop: false, +// } +// }); +// } +// } diff --git a/packages/plugin-users/src/server.ts b/packages/plugin-users/src/server.ts index 0aa891c2c..911e98bea 100644 --- a/packages/plugin-users/src/server.ts +++ b/packages/plugin-users/src/server.ts @@ -4,13 +4,12 @@ import Database, { registerFields } from '@nocobase/database'; import Resourcer from '@nocobase/resourcer'; import * as fields from './fields'; -import hooks from './hooks'; +// import hooks from './hooks'; import login from './actions/login'; import register from './actions/register'; import logout from './actions/logout'; import check from './actions/check'; - - +import { makeOptions } from './hooks/collection-after-create'; export default async function (options = {}) { const database: Database = this.database; @@ -22,7 +21,15 @@ export default async function (options = {}) { directory: path.resolve(__dirname, 'collections'), }); - hooks.call(this); + database.addHook('afterTableInit', (table) => { + const { createdBy, updatedBy } = table.getOptions(); + const fieldsToMake = { createdBy, updatedBy }; + Object.keys(fieldsToMake) + .filter(type => Boolean(fieldsToMake[type])) + .map(type => table.addField(makeOptions(type, fieldsToMake[type]))); + }); + + // hooks.call(this); resourcer.registerActionHandlers({ 'users:login': login,