fix(plugin-cm): fix unique option default value to update (#768)

This commit is contained in:
Junyi 2022-08-23 08:59:36 +08:00 committed by GitHub
parent b6daa9ad69
commit e6a2dff79a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

View File

@ -23,6 +23,36 @@ describe('field indexes', () => {
await app.destroy(); await app.destroy();
}); });
it('create unique constraint after added dulplicated records', async () => {
const tableName = 'test1';
// create an field with unique constraint
const field = await agent
.resource('collections.fields', tableName)
.create({
values: {
name: 'title',
type: 'string'
},
});
// create a record
const response1 = await agent.resource(tableName).create({
values: { title: 't1' }
});
// create another same record
const response2 = await agent.resource(tableName).create({
values: { title: 't1' }
});
const response3 = await agent.resource('fields').update({
filterByTk: field.id,
values: {
unique: true
}
});
expect(response3.status).toBe(400);
});
it('field value cannot be duplicated with unique index', async () => { it('field value cannot be duplicated with unique index', async () => {
const tableName = 'test1'; const tableName = 'test1';
// create an field with unique constraint // create an field with unique constraint

View File

@ -77,9 +77,9 @@ export class CollectionManagerPlugin extends Plugin {
this.app.db.on('fields.afterUpdate', async (model: FieldModel, { context, transaction }) => { this.app.db.on('fields.afterUpdate', async (model: FieldModel, { context, transaction }) => {
if (context) { if (context) {
const prev = model.previous('options')?.unique; const { unique: prev } = model.previous('options');
const next = model.get('options')?.unique; const { unique: next } = model.get('options');
if (lodash.isBoolean(prev) && lodash.isBoolean(next) && prev !== next) { if (Boolean(prev) !== Boolean(next)) {
await model.migrate({ transaction }); await model.migrate({ transaction });
} }
} }
@ -180,7 +180,9 @@ export class CollectionManagerPlugin extends Plugin {
const errorHandlerPlugin = <PluginErrorHandler>this.app.getPlugin('@nocobase/plugin-error-handler'); const errorHandlerPlugin = <PluginErrorHandler>this.app.getPlugin('@nocobase/plugin-error-handler');
errorHandlerPlugin.errorHandler.register( errorHandlerPlugin.errorHandler.register(
(err) => err instanceof UniqueConstraintError, (err) => {
return err instanceof UniqueConstraintError;
},
(err, ctx) => { (err, ctx) => {
return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`)); return ctx.throw(400, ctx.t(`The value of ${Object.keys(err.fields)} field duplicated`));
}, },