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

169 lines
4.1 KiB
TypeScript

import Database, { Collection, MagicAttributeModel } from '@nocobase/database';
import { SyncOptions, Transactionable } from 'sequelize';
import { FieldModel } from './field';
import lodash from 'lodash';
interface LoadOptions extends Transactionable {
// TODO
skipField?: boolean;
skipExist?: boolean;
}
export class CollectionModel extends MagicAttributeModel {
get db(): Database {
return (<any>this.constructor).database;
}
async load(loadOptions: LoadOptions = {}) {
const { skipExist, skipField, transaction } = loadOptions;
const name = this.get('name');
let collection: Collection;
const collectionOptions = {
...this.get(),
fields: [],
};
if (this.db.hasCollection(name)) {
collection = this.db.getCollection(name);
if (skipExist) {
return collection;
}
collection.updateOptions(collectionOptions);
} else {
collection = this.db.collection(collectionOptions);
}
if (!skipField) {
await this.loadFields({ transaction });
}
return collection;
}
async loadFields(options: Transactionable = {}) {
// @ts-ignore
const instances: FieldModel[] = await this.getFields(options);
for (const instance of instances) {
await instance.load(options);
}
}
async remove(options?: any) {
const { transaction } = options || {};
const name = this.get('name');
const collection = this.db.getCollection(name);
if (!collection) {
return;
}
const fields = await this.db.getRepository('fields').find({
filter: {
'type.$in': ['belongsToMany', 'belongsTo', 'hasMany', 'hasOne'],
},
transaction,
});
for (const field of fields) {
if (field.get('target') && field.get('target') === name) {
await field.destroy({ transaction });
} else if (field.get('through') && field.get('through') === name) {
await field.destroy({ transaction });
}
}
await collection.removeFromDb({
transaction,
});
}
async migrate(options?: SyncOptions & Transactionable) {
const collection = await this.load({
transaction: options?.transaction,
});
try {
await collection.sync({
force: false,
alter: {
drop: false,
},
...options,
});
} catch (error) {
console.error(error);
const name = this.get('name');
this.db.removeCollection(name);
throw error;
}
}
isInheritedModel() {
return this.get('inherits');
}
async findParents(options: Transactionable) {
const { transaction } = options;
const findModelParents = async (model: CollectionModel, carry = []) => {
if (!model.get('inherits')) {
return;
}
const parents = lodash.castArray(model.get('inherits'));
for (const parent of parents) {
const parentModel = (await this.db.getCollection('collections').repository.findOne({
filterByTk: parent,
transaction,
})) as CollectionModel;
carry.push(parentModel.get('name'));
await findModelParents(parentModel, carry);
}
return carry;
};
return findModelParents(this);
}
async parentFields(options: Transactionable) {
const { transaction } = options;
return this.db.getCollection('fields').repository.find({
filter: {
collectionName: { $in: await this.findParents({ transaction }) },
},
transaction,
});
}
// sync fields from parents
async syncParentFields(options: Transactionable) {
const { transaction } = options;
const ancestorFields = await this.parentFields({ transaction });
const selfFields = await this.getFields({ transaction });
const inheritedFields = ancestorFields.filter((field: FieldModel) => {
return (
!field.isAssociationField() &&
!selfFields.find((selfField: FieldModel) => selfField.get('name') == field.get('name'))
);
});
for (const inheritedField of inheritedFields) {
await this.createField(lodash.omit(inheritedField.toJSON(), ['key', 'collectionName', 'sort']), {
transaction,
});
}
}
}