mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 09:17:23 +00:00
feat: destroy association field after target collection destroy (#376)
This commit is contained in:
parent
16f861ad7d
commit
c6839b30c1
@ -0,0 +1,50 @@
|
|||||||
|
import Database, { Collection as DBCollection } from '@nocobase/database';
|
||||||
|
import Application from '@nocobase/server';
|
||||||
|
import { createApp } from '.';
|
||||||
|
|
||||||
|
describe('collections repository', () => {
|
||||||
|
let db: Database;
|
||||||
|
let app: Application;
|
||||||
|
let Collection: DBCollection;
|
||||||
|
let Field: DBCollection;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
app = await createApp();
|
||||||
|
await app.db.sync();
|
||||||
|
db = app.db;
|
||||||
|
Collection = db.getCollection('collections');
|
||||||
|
Field = db.getCollection('fields');
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await app.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove association field after collection destroy', async () => {
|
||||||
|
await Collection.repository.create({
|
||||||
|
context: {},
|
||||||
|
values: {
|
||||||
|
name: 'posts',
|
||||||
|
fields: [{ type: 'hasMany', name: 'comments', options: { target: 'comments' } }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await Collection.repository.create({
|
||||||
|
context: {},
|
||||||
|
values: {
|
||||||
|
name: 'comments',
|
||||||
|
fields: [{ type: 'string', name: 'content' }],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.getRepository('collections').destroy({
|
||||||
|
filter: {
|
||||||
|
name: 'comments',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const fields = await db.getRepository('fields').find();
|
||||||
|
|
||||||
|
expect(fields.length).toEqual(0);
|
||||||
|
});
|
||||||
|
});
|
@ -41,6 +41,7 @@ export default {
|
|||||||
target: 'collections',
|
target: 'collections',
|
||||||
foreignKey: 'collectionName',
|
foreignKey: 'collectionName',
|
||||||
targetKey: 'name',
|
targetKey: 'name',
|
||||||
|
onDelete: 'CASCADE',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'hasMany',
|
type: 'hasMany',
|
||||||
@ -60,7 +61,7 @@ export default {
|
|||||||
type: 'belongsTo',
|
type: 'belongsTo',
|
||||||
name: 'uiSchema',
|
name: 'uiSchema',
|
||||||
target: 'uiSchemas',
|
target: 'uiSchemas',
|
||||||
foreignKey: 'uiSchemaUid'
|
foreignKey: 'uiSchemaUid',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'json',
|
type: 'json',
|
||||||
|
@ -58,6 +58,28 @@ export class CollectionManagerPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.app.db.on('collections.afterDestroy', async (model, { transaction }) => {
|
||||||
|
const name = model.get('name');
|
||||||
|
|
||||||
|
const fields = await this.app.db.getRepository('fields').find({
|
||||||
|
filter: {
|
||||||
|
'type.$in': ['belongsToMany', 'belongsTo', 'hasMany', 'hasOne'],
|
||||||
|
},
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
|
||||||
|
const deleteFieldsKey = fields
|
||||||
|
.filter((field) => (field.get('options') as any)?.target === name)
|
||||||
|
.map((field) => field.get('key') as string);
|
||||||
|
|
||||||
|
await this.app.db.getRepository('fields').destroy({
|
||||||
|
filter: {
|
||||||
|
'key.$in': deleteFieldsKey,
|
||||||
|
},
|
||||||
|
transaction,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
|
this.app.db.on('fields.afterCreate', async (model, { context, transaction }) => {
|
||||||
if (context) {
|
if (context) {
|
||||||
await model.migrate({ transaction });
|
await model.migrate({ transaction });
|
||||||
|
Loading…
Reference in New Issue
Block a user