nocobase/packages/core/database/src/inherited-map.ts
ChengLei Shao e991b2965a
feat: collection inheritance (#1069)
* chore: test

* chore: inherited-collection class

* feat: collection inherit

* feat: collection inherit

* feat: inhertis sync runner

* test: get parents fields

* feat: collection inherit style promote

* feat: sync

* feat: sync alter table

* feat: pgOnly Test

* fix: child collection create api

* feat: replace parent field

* chore: reload parent fields

* test: reload collection test

* feat: details are displayed according to conditions

* fix: typo

* feat: inheritance map class

* chore: is parent node

* feat: display where child row created from

* fix: find with appends

* feat: add parent collection fields

* fix: create table

* feat: load fields for all children

* refactor: sync fields from parent

* test: has one field inhertis

* feat: replace child association target

* feat: should not replace child field when parent field update

* test: should update inherit field when parent field update

* feat: only the blocks directly inherited from the current data are displayed

* fix: inherit from multiple collections

* feat: only the blocks directly inherited from the current data are displayed

* fix: test

* feat: parent collection expend

* fix: test

* test: belongsToMany inherits

* test: belongsToMany inherits

* feat: block display

* feat: collection inherite

* feat: collection inherite

* feat: multiple inherits

* fix: sync runner

* feat: collection inherite

* feat: collecton inherits

* feat: cannot be modified after inheritance and saving

* feat: collection inherit for graph

* feat: collection inherits

* fix: drop inhertied field

* fix: should throw error when type conflit

* feat: output inherited fields

* feat: bulk update collection fields

* feat: collection fields

* feat: collection fields

* test: create relation with child table

* fix: test

* fix: test

* fix: test

* feat: style impove

* test: should not replace field with difference type

* feat: add text

* fix: throw error when replace field with difference type

* feat: overriding

* feat: kan bankanban group fields

* feat: calendar block fields

* feat: kan bankanban group fields

* fix: test

* feat: relationship fields

* feat: should delete child's field when parent field deleted

* feat: foreign key filter

* fix: build error & multiple inherit destory field

* fix: test

* chore: disable error

* feat: no recursive update associations (#1091)

* feat: update associations

* fix(collection-manager): should update uiSchema

* chore: flip if

* feat: mutile inherits

* feat: db dialect

* feat: inherits show by database

* chore: git hash into docker image

* fix: js gzip

* fix: dockerfile

* chore: error message

* feat: overriding

* feat: overriding

* feat: overriding

* feat: local

* feat: filter fields by interface

* fix: database logging env

* test: replace hasOne target

* feat: add view

* feat: local

* chore: enable error

* fix: update docs

Co-authored-by: katherinehhh <katherine_15995@163.com>
Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-16 12:53:58 +08:00

82 lines
1.9 KiB
TypeScript

import lodash from 'lodash';
class TableNode {
name: string;
parents: Set<TableNode>;
children: Set<TableNode>;
constructor(name: string) {
this.name = name;
this.parents = new Set();
this.children = new Set();
}
}
export default class InheritanceMap {
nodes: Map<string, TableNode> = new Map<string, TableNode>();
getOrCreateNode(name: string) {
if (!this.nodes.has(name)) {
this.nodes.set(name, new TableNode(name));
}
return this.getNode(name);
}
getNode(name: string) {
return this.nodes.get(name);
}
setInheritance(name: string, inherits: string | string[]) {
const node = this.getOrCreateNode(name);
const parents = lodash.castArray(inherits).map((name) => this.getOrCreateNode(name));
node.parents = new Set(parents);
for (const parent of parents) {
parent.children.add(node);
}
}
isParentNode(name: string) {
const node = this.getNode(name);
return node && node.children.size > 0;
}
getChildren(name: string, options: { deep: boolean } = { deep: true }): Set<string> {
const results = new Set<string>();
const node = this.getNode(name);
if (!node) return results;
for (const child of node.children) {
results.add(child.name);
if (!options.deep) {
continue;
}
for (const grandchild of this.getChildren(child.name)) {
results.add(grandchild);
}
}
return results;
}
getParents(name: string, options: { deep: boolean } = { deep: true }): Set<string> {
const results = new Set<string>();
const node = this.getNode(name);
if (!node) return results;
for (const parent of node.parents) {
results.add(parent.name);
if (!options.deep) {
continue;
}
for (const grandparent of this.getParents(parent.name)) {
results.add(grandparent);
}
}
return results;
}
}