fix: use database hook to trigger add createdBy/updatedBy fields
This commit is contained in:
parent
272b64b81b
commit
184adb924d
@ -62,7 +62,7 @@ describe('user fields', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// TODO(bug): 重复添加字段不能与 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 = db.getModel('collections');
|
||||||
const collection = await Collection.import({
|
const collection = await Collection.import({
|
||||||
name: 'posts',
|
name: 'posts',
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
function makeOptions(type: string, options: any) {
|
export function makeOptions(type: string, options: any) {
|
||||||
if (!options) {
|
if (!options) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -21,22 +21,30 @@ function makeOptions(type: string, options: any) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function(model, options) {
|
export default async function(table) {
|
||||||
const { database } = model;
|
|
||||||
const tableName = model.get('name') as string;
|
|
||||||
const table = database.getTable(tableName);
|
|
||||||
const { createdBy, updatedBy } = table.getOptions();
|
const { createdBy, updatedBy } = table.getOptions();
|
||||||
const fieldsToMake = { createdBy, updatedBy };
|
const fieldsToMake = { createdBy, updatedBy };
|
||||||
const addedFields = Object.keys(fieldsToMake)
|
Object.keys(fieldsToMake)
|
||||||
.filter(type => Boolean(fieldsToMake[type]))
|
.filter(type => Boolean(fieldsToMake[type]))
|
||||||
.map(type => table.addField(makeOptions(type, 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,
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
@ -4,13 +4,12 @@ import Database, { registerFields } from '@nocobase/database';
|
|||||||
import Resourcer from '@nocobase/resourcer';
|
import Resourcer from '@nocobase/resourcer';
|
||||||
|
|
||||||
import * as fields from './fields';
|
import * as fields from './fields';
|
||||||
import hooks from './hooks';
|
// import hooks from './hooks';
|
||||||
import login from './actions/login';
|
import login from './actions/login';
|
||||||
import register from './actions/register';
|
import register from './actions/register';
|
||||||
import logout from './actions/logout';
|
import logout from './actions/logout';
|
||||||
import check from './actions/check';
|
import check from './actions/check';
|
||||||
|
import { makeOptions } from './hooks/collection-after-create';
|
||||||
|
|
||||||
|
|
||||||
export default async function (options = {}) {
|
export default async function (options = {}) {
|
||||||
const database: Database = this.database;
|
const database: Database = this.database;
|
||||||
@ -22,7 +21,15 @@ export default async function (options = {}) {
|
|||||||
directory: path.resolve(__dirname, 'collections'),
|
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({
|
resourcer.registerActionHandlers({
|
||||||
'users:login': login,
|
'users:login': login,
|
||||||
|
Loading…
Reference in New Issue
Block a user