* feat: add createdBy/updatedBy field config for table managed by collections * fix: update by different users and field initialization * fix: function argument * add: test cases Co-authored-by: chenos <chenlinxh@gmail.com>
28 lines
945 B
TypeScript
28 lines
945 B
TypeScript
import { BelongsToOptions, BELONGSTO, FieldContext } from '@nocobase/database';
|
|
import { setUserValue } from './utils';
|
|
|
|
export interface CreatedByOptions extends Omit<BelongsToOptions, 'type'> {
|
|
type: 'createdBy' | 'createdby'
|
|
}
|
|
|
|
export default class CreatedBy extends BELONGSTO {
|
|
|
|
static beforeBulkCreateHook(this: CreatedBy, models, { context }) {
|
|
models.forEach(model => {
|
|
setUserValue.call(this, model, { context });
|
|
});
|
|
}
|
|
|
|
constructor({ type, ...options }: CreatedByOptions, context: FieldContext) {
|
|
super({ ...options, type: 'belongsTo' } as BelongsToOptions, context);
|
|
const Model = context.sourceTable.getModel();
|
|
// TODO(feature): 可考虑策略模式,以在需要时对外提供接口
|
|
Model.addHook('beforeCreate', setUserValue.bind(this));
|
|
Model.addHook('beforeBulkCreate', CreatedBy.beforeBulkCreateHook.bind(this));
|
|
}
|
|
|
|
public getDataType(): Function {
|
|
return BELONGSTO;
|
|
}
|
|
}
|