2022-11-16 12:53:58 +08:00
|
|
|
import { Collection, CollectionContext, CollectionOptions } from './collection';
|
|
|
|
import { default as lodash } from 'lodash';
|
|
|
|
import { Field } from '.';
|
|
|
|
|
|
|
|
export class InheritedCollection extends Collection {
|
|
|
|
parents?: Collection[];
|
|
|
|
constructor(options: CollectionOptions, context: CollectionContext) {
|
|
|
|
if (!options.inherits) {
|
|
|
|
throw new Error('InheritedCollection must have inherits option');
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:33:20 +08:00
|
|
|
options.inherits = lodash.castArray(options.inherits);
|
|
|
|
|
2022-11-16 12:53:58 +08:00
|
|
|
super(options, context);
|
2022-11-20 17:33:20 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.bindParents();
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof ParentCollectionNotFound) {
|
|
|
|
const listener = (collection) => {
|
|
|
|
if (
|
|
|
|
options.inherits.includes(collection.name) &&
|
|
|
|
lodash.every(options.inherits, (name) => this.context.database.collections.has(name))
|
|
|
|
) {
|
|
|
|
this.bindParents();
|
|
|
|
this.db.removeListener('afterDefineCollection', listener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.db.addListener('afterDefineCollection', listener);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected bindParents() {
|
|
|
|
this.setParents(this.options.inherits);
|
2022-11-16 12:53:58 +08:00
|
|
|
this.setParentFields();
|
2022-11-26 09:24:53 +08:00
|
|
|
this.setFields(this.options.fields, false);
|
|
|
|
this.db.inheritanceMap.setInheritance(this.name, this.options.inherits);
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected setParents(inherits: string | string[]) {
|
2022-11-20 17:33:20 +08:00
|
|
|
this.parents = lodash.castArray(inherits).map((name) => {
|
2022-11-26 09:24:53 +08:00
|
|
|
const existCollection = this.db.collections.get(name);
|
2022-11-20 17:33:20 +08:00
|
|
|
if (!existCollection) {
|
|
|
|
throw new ParentCollectionNotFound(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return existCollection;
|
|
|
|
});
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected setParentFields() {
|
2022-11-26 09:24:53 +08:00
|
|
|
for (const [name, fieldOptions] of this.parentFields()) {
|
|
|
|
this.setField(name, {
|
|
|
|
...fieldOptions,
|
|
|
|
inherit: true,
|
|
|
|
});
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getParents() {
|
|
|
|
return this.parents;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentFields() {
|
|
|
|
const fields = new Map<string, Field>();
|
|
|
|
for (const parent of this.parents) {
|
|
|
|
if (parent.isInherited()) {
|
|
|
|
for (const [name, field] of (<InheritedCollection>parent).parentFields()) {
|
2022-11-26 09:24:53 +08:00
|
|
|
fields.set(name, field.options);
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentFields = parent.fields;
|
|
|
|
for (const [name, field] of parentFields) {
|
2022-11-26 09:24:53 +08:00
|
|
|
fields.set(name, field.options);
|
2022-11-16 12:53:58 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-26 09:24:53 +08:00
|
|
|
|
2022-11-16 12:53:58 +08:00
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentAttributes() {
|
|
|
|
const attributes = {};
|
|
|
|
for (const parent of this.parents) {
|
|
|
|
if (parent.isInherited()) {
|
|
|
|
Object.assign(attributes, (<InheritedCollection>parent).parentAttributes());
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentAttributes = (<any>parent.model).tableAttributes;
|
|
|
|
|
|
|
|
Object.assign(attributes, parentAttributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
isInherited() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 17:33:20 +08:00
|
|
|
|
|
|
|
class ParentCollectionNotFound extends Error {
|
|
|
|
constructor(name: string) {
|
|
|
|
super(`parent collection ${name} not found`);
|
|
|
|
}
|
|
|
|
}
|