fix: remove node after collection removed (#1095)

This commit is contained in:
ChengLei Shao 2022-11-16 20:10:26 +08:00 committed by GitHub
parent f8f9b8cc9f
commit c5fcc62bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 0 deletions

View File

@ -15,6 +15,28 @@ pgOnly()('collection inherits', () => {
await db.close();
});
it('should remove Node after collection destroy', async () => {
const table1 = db.collection({
name: 'table1',
fields: [{ type: 'string', name: 'name' }],
});
db.collection({
name: 'table2',
fields: [{ type: 'string', name: 'integer' }],
});
const collection3 = db.collection({
name: 'table3',
inherits: ['table1', 'table2'],
});
await db.removeCollection(collection3.name);
expect(db.inheritanceMap.getNode('table3')).toBeUndefined();
expect(table1.isParent()).toBeFalsy();
});
it('can update relation with child table', async () => {
const A = db.collection({
name: 'a',

View File

@ -262,6 +262,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
transaction: options.transaction,
});
});
this.on('afterRemoveCollection', (collection) => {
this.inheritanceMap.removeNode(collection.name);
});
}
addMigration(item: MigrationItem) {

View File

@ -1,4 +1,5 @@
import lodash from 'lodash';
import Database from './database';
class TableNode {
name: string;
@ -14,6 +15,21 @@ class TableNode {
export default class InheritanceMap {
nodes: Map<string, TableNode> = new Map<string, TableNode>();
removeNode(name: string) {
const node = this.nodes.get(name);
if (!node) return;
for (const parent of node.parents) {
parent.children.delete(node);
}
for (const child of node.children) {
child.parents.delete(node);
}
this.nodes.delete(name);
}
getOrCreateNode(name: string) {
if (!this.nodes.has(name)) {
this.nodes.set(name, new TableNode(name));