fix: add createdAt/updatedAt/createdBy/updatedBy options to the collections table for developer mode

This commit is contained in:
chenos 2020-12-18 19:41:40 +08:00
parent afce1142d9
commit 7e9b4a8f45
4 changed files with 66 additions and 3 deletions

View File

@ -3,8 +3,8 @@ import { TableOptions } from '@nocobase/database';
export default {
title: '示例',
showInDataMenu: true,
createdBy: true,
updatedBy: true,
// createdBy: true,
// updatedBy: true,
fields: [
{
interface: 'string',

View File

@ -8,6 +8,8 @@ export default {
draggable: true,
model: 'CollectionModel',
developerMode: true,
createdAt: 'createdTime',
updatedAt: 'updatedTime',
fields: [
{
interface: 'sort',
@ -143,11 +145,62 @@ export default {
showInDetail: true,
},
},
{
interface: 'boolean',
type: 'virtual',
name: 'createdAt',
title: '记录创建时间',
developerMode: true,
defaultValue: true,
component: {
type: 'checkbox',
default: true,
showInForm: true,
},
},
{
interface: 'boolean',
type: 'virtual',
name: 'updatedAt',
title: '记录修改时间',
developerMode: true,
defaultValue: true,
component: {
type: 'checkbox',
default: true,
showInForm: true,
},
},
{
interface: 'boolean',
type: 'virtual',
name: 'createdBy',
title: '记录创建人信息',
developerMode: true,
component: {
type: 'checkbox',
default: true,
showInForm: true,
},
},
{
interface: 'boolean',
type: 'virtual',
name: 'updatedBy',
title: '记录修改人信息',
developerMode: true,
component: {
type: 'checkbox',
default: true,
showInForm: true,
},
},
{
interface: 'boolean',
type: 'boolean',
name: 'developerMode',
title: '开发者模式',
developerMode: true,
defaultValue: false,
component: {
type: 'boolean',

View File

@ -10,6 +10,7 @@ export default {
collections: {
beforeValidate: collectionsBeforeValidate,
afterCreate: collectionsAfterCreate,
afterUpdate: collectionsAfterCreate,
},
fields: {
beforeValidate: fieldsBeforeValidate,

View File

@ -22,7 +22,16 @@ export default async function (options = {}) {
});
database.addHook('afterTableInit', (table) => {
const { createdBy, updatedBy } = table.getOptions();
let { createdBy, updatedBy, internal } = table.getOptions();
// 非内置表,默认创建 createdBy 和 updatedBy
if (!internal) {
if (typeof createdBy === 'undefined') {
createdBy = true;
}
if (typeof updatedBy === 'undefined') {
updatedBy = true;
}
}
const fieldsToMake = { createdBy, updatedBy };
Object.keys(fieldsToMake)
.filter(type => Boolean(fieldsToMake[type]))