2022-02-11 14:34:33 +08:00
|
|
|
import merge from 'deepmerge';
|
2021-09-27 15:28:32 +08:00
|
|
|
import { EventEmitter } from 'events';
|
2022-02-11 14:34:33 +08:00
|
|
|
import { default as lodash, default as _ } from 'lodash';
|
2022-07-01 09:33:05 +08:00
|
|
|
import {
|
|
|
|
ModelCtor,
|
|
|
|
ModelOptions,
|
|
|
|
QueryInterfaceDropTableOptions,
|
|
|
|
SyncOptions,
|
|
|
|
Transactionable,
|
2022-10-14 14:51:19 +08:00
|
|
|
Utils,
|
2022-07-01 09:33:05 +08:00
|
|
|
} from 'sequelize';
|
2021-09-18 00:23:21 +08:00
|
|
|
import { Database } from './database';
|
2021-12-06 21:12:54 +08:00
|
|
|
import { Field, FieldOptions } from './fields';
|
2022-02-23 18:18:38 +08:00
|
|
|
import { Model } from './model';
|
2021-09-25 23:56:26 +08:00
|
|
|
import { Repository } from './repository';
|
2022-10-14 14:51:19 +08:00
|
|
|
import { checkIdentifier, md5 } from './utils';
|
2022-02-11 14:34:33 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
export type RepositoryType = typeof Repository;
|
2021-09-18 00:23:21 +08:00
|
|
|
|
2022-02-11 14:34:33 +08:00
|
|
|
export type CollectionSortable = string | boolean | { name?: string; scopeKey?: string };
|
|
|
|
|
2022-02-11 14:52:04 +08:00
|
|
|
export interface CollectionOptions extends Omit<ModelOptions, 'name' | 'hooks'> {
|
2021-09-27 15:28:32 +08:00
|
|
|
name: string;
|
|
|
|
tableName?: string;
|
2022-11-16 12:53:58 +08:00
|
|
|
inherits?: string[] | string;
|
2022-01-07 20:08:01 +08:00
|
|
|
filterTargetKey?: string;
|
2021-12-06 21:12:54 +08:00
|
|
|
fields?: FieldOptions[];
|
|
|
|
model?: string | ModelCtor<Model>;
|
|
|
|
repository?: string | RepositoryType;
|
2022-02-11 14:34:33 +08:00
|
|
|
sortable?: CollectionSortable;
|
2022-01-07 22:06:31 +08:00
|
|
|
/**
|
|
|
|
* @default true
|
|
|
|
*/
|
2021-12-16 10:58:51 +08:00
|
|
|
autoGenId?: boolean;
|
2022-01-07 22:06:31 +08:00
|
|
|
/**
|
|
|
|
* @default 'options'
|
|
|
|
*/
|
|
|
|
magicAttribute?: string;
|
2021-12-06 21:23:34 +08:00
|
|
|
[key: string]: any;
|
2021-09-18 00:23:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CollectionContext {
|
|
|
|
database: Database;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
export class Collection<
|
|
|
|
TModelAttributes extends {} = any,
|
|
|
|
TCreationAttributes extends {} = TModelAttributes,
|
|
|
|
> extends EventEmitter {
|
2021-09-18 00:23:21 +08:00
|
|
|
options: CollectionOptions;
|
|
|
|
context: CollectionContext;
|
2021-12-06 21:12:54 +08:00
|
|
|
isThrough?: boolean;
|
|
|
|
fields: Map<string, any> = new Map<string, any>();
|
2022-02-23 18:18:38 +08:00
|
|
|
model: ModelCtor<Model>;
|
2021-12-06 21:12:54 +08:00
|
|
|
repository: Repository<TModelAttributes, TCreationAttributes>;
|
2021-09-18 00:23:21 +08:00
|
|
|
|
2022-01-07 20:08:01 +08:00
|
|
|
get filterTargetKey() {
|
|
|
|
return lodash.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
|
|
|
|
}
|
|
|
|
|
2021-09-18 00:23:21 +08:00
|
|
|
get name() {
|
|
|
|
return this.options.name;
|
|
|
|
}
|
|
|
|
|
2022-06-18 00:18:12 +08:00
|
|
|
get db() {
|
|
|
|
return this.context.database;
|
|
|
|
}
|
|
|
|
|
2022-10-14 17:22:32 +08:00
|
|
|
constructor(options: CollectionOptions, context: CollectionContext) {
|
2021-09-27 15:28:32 +08:00
|
|
|
super();
|
2022-10-14 14:51:19 +08:00
|
|
|
this.checkOptions(options);
|
|
|
|
|
2021-09-18 00:23:21 +08:00
|
|
|
this.context = context;
|
2021-12-06 21:12:54 +08:00
|
|
|
this.options = options;
|
2022-10-14 14:51:19 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
this.bindFieldEventListener();
|
|
|
|
this.modelInit();
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2022-10-31 22:45:39 +08:00
|
|
|
this.db.modelCollection.set(this.model, this);
|
2022-11-16 12:53:58 +08:00
|
|
|
this.db.tableNameCollectionMap.set(this.model.tableName, this);
|
2022-10-31 22:45:39 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
this.setFields(options.fields);
|
|
|
|
this.setRepository(options.repository);
|
2022-02-11 14:34:33 +08:00
|
|
|
this.setSortable(options.sortable);
|
2021-12-06 21:12:54 +08:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:51:19 +08:00
|
|
|
private checkOptions(options: CollectionOptions) {
|
|
|
|
checkIdentifier(options.name);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
private sequelizeModelOptions() {
|
|
|
|
const { name, tableName } = this.options;
|
|
|
|
return {
|
2022-03-04 20:45:21 +08:00
|
|
|
..._.omit(this.options, ['name', 'fields', 'model', 'targetKey']),
|
2021-09-18 00:23:21 +08:00
|
|
|
modelName: name,
|
2021-12-06 21:12:54 +08:00
|
|
|
sequelize: this.context.database.sequelize,
|
2021-09-18 00:23:21 +08:00
|
|
|
tableName: tableName || name,
|
2021-12-06 21:12:54 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO
|
|
|
|
*/
|
|
|
|
modelInit() {
|
|
|
|
if (this.model) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-16 10:58:51 +08:00
|
|
|
const { name, model, autoGenId = true } = this.options;
|
2022-02-23 18:18:38 +08:00
|
|
|
let M: ModelCtor<Model> = Model;
|
2021-12-06 21:12:54 +08:00
|
|
|
if (this.context.database.sequelize.isDefined(name)) {
|
|
|
|
const m = this.context.database.sequelize.model(name);
|
|
|
|
if ((m as any).isThrough) {
|
2022-02-23 18:18:38 +08:00
|
|
|
// @ts-ignore
|
2021-12-06 21:12:54 +08:00
|
|
|
this.model = m;
|
2022-02-23 18:18:38 +08:00
|
|
|
// @ts-ignore
|
|
|
|
this.model.database = this.context.database;
|
|
|
|
// @ts-ignore
|
|
|
|
this.model.collection = this;
|
2021-12-06 21:12:54 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof model === 'string') {
|
|
|
|
M = this.context.database.models.get(model) || Model;
|
|
|
|
} else if (model) {
|
|
|
|
M = model;
|
|
|
|
}
|
2022-02-23 18:18:38 +08:00
|
|
|
// @ts-ignore
|
2021-12-06 21:12:54 +08:00
|
|
|
this.model = class extends M {};
|
|
|
|
this.model.init(null, this.sequelizeModelOptions());
|
2021-12-16 10:58:51 +08:00
|
|
|
|
|
|
|
if (!autoGenId) {
|
|
|
|
this.model.removeAttribute('id');
|
|
|
|
}
|
|
|
|
|
2022-02-23 18:18:38 +08:00
|
|
|
// @ts-ignore
|
|
|
|
this.model.database = this.context.database;
|
|
|
|
// @ts-ignore
|
|
|
|
this.model.collection = this;
|
2021-12-06 21:12:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setRepository(repository?: RepositoryType | string) {
|
|
|
|
let repo = Repository;
|
|
|
|
if (typeof repository === 'string') {
|
|
|
|
repo = this.context.database.repositories.get(repository) || Repository;
|
|
|
|
}
|
|
|
|
this.repository = new repo(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bindFieldEventListener() {
|
2022-08-20 23:23:13 +08:00
|
|
|
this.on('field.afterAdd', (field: Field) => field.bind());
|
|
|
|
this.on('field.afterRemove', (field: Field) => field.unbind());
|
2021-09-18 00:23:21 +08:00
|
|
|
}
|
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
forEachField(callback: (field: Field) => void) {
|
|
|
|
return [...this.fields.values()].forEach(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
findField(callback: (field: Field) => boolean) {
|
|
|
|
return [...this.fields.values()].find(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
hasField(name: string) {
|
|
|
|
return this.fields.has(name);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
getField<F extends Field>(name: string): F {
|
2021-09-27 15:28:32 +08:00
|
|
|
return this.fields.get(name);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
addField(name: string, options: FieldOptions): Field {
|
2021-12-06 21:12:54 +08:00
|
|
|
return this.setField(name, options);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
setField(name: string, options: FieldOptions): Field {
|
2022-10-14 14:51:19 +08:00
|
|
|
checkIdentifier(name);
|
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
const { database } = this.context;
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
const field = database.buildField(
|
|
|
|
{ name, ...options },
|
|
|
|
{
|
|
|
|
...this.context,
|
|
|
|
collection: this,
|
|
|
|
},
|
|
|
|
);
|
2022-10-14 14:51:19 +08:00
|
|
|
|
2022-11-16 12:53:58 +08:00
|
|
|
const oldField = this.fields.get(name);
|
|
|
|
|
|
|
|
if (oldField && oldField.options.inherit && options.type != oldField.options.type) {
|
|
|
|
throw new Error(
|
2022-11-16 16:40:51 +08:00
|
|
|
`Field type conflict: cannot set "${name}" on "${this.name}" to ${options.type}, parent "${name}" type is ${oldField.options.type}`,
|
2022-11-16 12:53:58 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:35:49 +08:00
|
|
|
this.removeField(name);
|
2021-09-27 15:28:32 +08:00
|
|
|
this.fields.set(name, field);
|
|
|
|
this.emit('field.afterAdd', field);
|
2022-11-16 12:53:58 +08:00
|
|
|
|
|
|
|
if (this.isParent()) {
|
|
|
|
for (const child of this.context.database.inheritanceMap.getChildren(this.name, {
|
|
|
|
deep: false,
|
|
|
|
})) {
|
|
|
|
const childCollection = this.db.getCollection(child);
|
|
|
|
const existField = childCollection.getField(name);
|
|
|
|
|
|
|
|
if (!existField || existField.options.inherit) {
|
|
|
|
childCollection.setField(name, {
|
|
|
|
...options,
|
|
|
|
inherit: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
return field;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
setFields(fields: FieldOptions[], resetFields = true) {
|
|
|
|
if (!Array.isArray(fields)) {
|
|
|
|
return;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
if (resetFields) {
|
|
|
|
this.resetFields();
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
for (const { name, ...options } of fields) {
|
|
|
|
this.addField(name, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resetFields() {
|
|
|
|
const fieldNames = this.fields.keys();
|
|
|
|
for (const fieldName of fieldNames) {
|
|
|
|
this.removeField(fieldName);
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 20:46:30 +08:00
|
|
|
remove() {
|
|
|
|
this.context.database.removeCollection(this.name);
|
|
|
|
}
|
|
|
|
|
2022-06-18 00:18:12 +08:00
|
|
|
async removeFromDb(options?: QueryInterfaceDropTableOptions) {
|
|
|
|
if (
|
|
|
|
await this.existsInDb({
|
|
|
|
transaction: options?.transaction,
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
const queryInterface = this.db.sequelize.getQueryInterface();
|
|
|
|
await queryInterface.dropTable(this.model.tableName, options);
|
|
|
|
}
|
|
|
|
this.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
async existsInDb(options?: Transactionable) {
|
|
|
|
return this.db.collectionExistsInDb(this.name, options);
|
|
|
|
}
|
|
|
|
|
2022-09-11 21:58:49 +08:00
|
|
|
removeField(name: string): void | Field {
|
2022-03-02 18:35:49 +08:00
|
|
|
if (!this.fields.has(name)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-11-22 16:19:48 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
const field = this.fields.get(name);
|
2022-11-22 16:19:48 +08:00
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
const bool = this.fields.delete(name);
|
|
|
|
if (bool) {
|
|
|
|
this.emit('field.afterRemove', field);
|
|
|
|
}
|
2022-06-18 00:18:12 +08:00
|
|
|
return field as Field;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* TODO
|
|
|
|
*/
|
|
|
|
updateOptions(options: CollectionOptions, mergeOptions?: any) {
|
|
|
|
let newOptions = lodash.cloneDeep(options);
|
|
|
|
newOptions = merge(this.options, newOptions, mergeOptions);
|
|
|
|
|
|
|
|
this.context.database.emit('beforeUpdateCollection', this, newOptions);
|
|
|
|
|
|
|
|
this.setFields(options.fields, false);
|
|
|
|
this.setRepository(options.repository);
|
|
|
|
|
|
|
|
this.context.database.emit('afterUpdateCollection', this);
|
2022-01-19 10:02:52 +08:00
|
|
|
|
|
|
|
return this;
|
2021-12-06 21:12:54 +08:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:34:33 +08:00
|
|
|
setSortable(sortable) {
|
|
|
|
if (!sortable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sortable === true) {
|
|
|
|
this.setField('sort', {
|
|
|
|
type: 'sort',
|
|
|
|
hidden: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (typeof sortable === 'string') {
|
|
|
|
this.setField(sortable, {
|
|
|
|
type: 'sort',
|
|
|
|
hidden: true,
|
|
|
|
});
|
|
|
|
} else if (typeof sortable === 'object') {
|
|
|
|
const { name, ...opts } = sortable;
|
|
|
|
this.setField(name || 'sort', { type: 'sort', hidden: true, ...opts });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* TODO
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
updateField(name: string, options: FieldOptions) {
|
|
|
|
if (!this.hasField(name)) {
|
|
|
|
throw new Error(`field ${name} not exists`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.name && options.name !== name) {
|
|
|
|
this.removeField(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setField(options.name || name, options);
|
|
|
|
}
|
|
|
|
|
2022-10-10 22:35:21 +08:00
|
|
|
addIndex(index: string | string[] | { fields: string[]; unique?: boolean; [key: string]: any }) {
|
2022-07-01 09:33:05 +08:00
|
|
|
if (!index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let indexes: any = this.model.options.indexes || [];
|
|
|
|
let indexName = [];
|
|
|
|
let indexItem;
|
|
|
|
if (typeof index === 'string') {
|
|
|
|
indexItem = {
|
|
|
|
fields: [index],
|
|
|
|
};
|
|
|
|
indexName = [index];
|
|
|
|
} else if (Array.isArray(index)) {
|
|
|
|
indexItem = {
|
|
|
|
fields: index,
|
|
|
|
};
|
|
|
|
indexName = index;
|
|
|
|
} else if (index?.fields) {
|
|
|
|
indexItem = index;
|
|
|
|
indexName = index.fields;
|
|
|
|
}
|
|
|
|
if (lodash.isEqual(this.model.primaryKeyAttributes, indexName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const name: string = this.model.primaryKeyAttributes.join(',');
|
|
|
|
if (name.startsWith(`${indexName.join(',')},`)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const item of indexes) {
|
|
|
|
if (lodash.isEqual(item.fields, indexName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const name: string = item.fields.join(',');
|
|
|
|
if (name.startsWith(`${indexName.join(',')},`)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!indexItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
indexes.push(indexItem);
|
|
|
|
this.model.options.indexes = indexes;
|
|
|
|
const tableName = this.model.getTableName();
|
|
|
|
// @ts-ignore
|
|
|
|
this.model._indexes = this.model.options.indexes
|
|
|
|
// @ts-ignore
|
2022-10-10 22:35:21 +08:00
|
|
|
.map((index) => Utils.nameIndex(this.model._conformIndex(index), tableName))
|
|
|
|
.map((item) => {
|
|
|
|
if (item.name && item.name.length > 63) {
|
|
|
|
item.name = 'i_' + md5(item.name);
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
2022-07-01 09:33:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
removeIndex(fields: any) {
|
|
|
|
if (!fields) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
const indexes: any[] = this.model._indexes;
|
|
|
|
// @ts-ignore
|
|
|
|
this.model._indexes = indexes.filter((item) => {
|
|
|
|
return !lodash.isEqual(item.fields, fields);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
async sync(syncOptions?: SyncOptions) {
|
2022-08-20 23:23:13 +08:00
|
|
|
const modelNames = new Set([this.model.name]);
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2022-09-11 21:58:49 +08:00
|
|
|
const { associations } = this.model;
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
for (const associationKey in associations) {
|
|
|
|
const association = associations[associationKey];
|
2022-08-20 23:23:13 +08:00
|
|
|
modelNames.add(association.target.name);
|
2022-11-16 12:53:58 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
if ((<any>association).through) {
|
2022-08-20 23:23:13 +08:00
|
|
|
modelNames.add((<any>association).through.model.name);
|
2021-12-06 21:12:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const models: ModelCtor<Model>[] = [];
|
|
|
|
// @ts-ignore
|
|
|
|
this.context.database.sequelize.modelManager.forEachModel((model) => {
|
2022-08-20 23:23:13 +08:00
|
|
|
if (modelNames.has(model.name)) {
|
2021-12-06 21:12:54 +08:00
|
|
|
models.push(model);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const model of models) {
|
|
|
|
await model.sync(syncOptions);
|
|
|
|
}
|
2021-09-18 00:23:21 +08:00
|
|
|
}
|
2022-11-16 12:53:58 +08:00
|
|
|
|
|
|
|
public isInherited() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isParent() {
|
|
|
|
return this.context.database.inheritanceMap.isParentNode(this.name);
|
|
|
|
}
|
2021-09-18 00:23:21 +08:00
|
|
|
}
|