feat: generate random name for actions/views/tabs
This commit is contained in:
parent
ae6ea41c52
commit
ed43bfe86d
5
packages/plugin-collections/src/hooks/generateName.ts
Normal file
5
packages/plugin-collections/src/hooks/generateName.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { BaseModel } from '../models';
|
||||
|
||||
export default async function (model: BaseModel) {
|
||||
model.generateNameIfNull();
|
||||
}
|
@ -3,6 +3,7 @@ import collectionsAfterCreate from './collections-after-create';
|
||||
|
||||
import fieldsBeforeValidate from './fields-before-validate';
|
||||
import fieldsAfterCreate from './fields-after-create';
|
||||
import generateName from './generateName';
|
||||
|
||||
export default {
|
||||
collections: {
|
||||
@ -12,5 +13,14 @@ export default {
|
||||
fields: {
|
||||
beforeValidate: fieldsBeforeValidate,
|
||||
afterCreate: fieldsAfterCreate
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
beforeValidate: generateName
|
||||
},
|
||||
views: {
|
||||
beforeValidate: generateName
|
||||
},
|
||||
tabs: {
|
||||
beforeValidate: generateName
|
||||
},
|
||||
};
|
||||
|
@ -2,8 +2,22 @@ import _ from 'lodash';
|
||||
import { getDataTypeKey, Model } from '@nocobase/database';
|
||||
import { Utils } from 'sequelize';
|
||||
|
||||
export function generateName(title?: string): string {
|
||||
return `${Math.random().toString(36).replace('0.', '').slice(-4).padStart(4, '0')}`;
|
||||
}
|
||||
|
||||
export class BaseModel extends Model {
|
||||
|
||||
generateName() {
|
||||
this.set('name', generateName());
|
||||
}
|
||||
|
||||
generateNameIfNull() {
|
||||
if (!this.get('name')) {
|
||||
this.generateName();
|
||||
}
|
||||
}
|
||||
|
||||
get additionalAttribute() {
|
||||
const tableOptions = this.database.getTable(this.constructor.name).getOptions();
|
||||
return _.get(tableOptions, 'additionalAttribute') || 'options';
|
||||
|
Loading…
Reference in New Issue
Block a user