2021-09-25 23:56:26 +08:00
|
|
|
import {
|
2021-09-27 15:28:32 +08:00
|
|
|
Association,
|
|
|
|
BulkCreateOptions,
|
|
|
|
CreateOptions as SequelizeCreateOptions,
|
2021-12-06 21:12:54 +08:00
|
|
|
FindAndCountOptions as SequelizeAndCountOptions,
|
|
|
|
FindOptions as SequelizeFindOptions,
|
|
|
|
Model,
|
|
|
|
ModelCtor,
|
|
|
|
Op,
|
|
|
|
Transaction,
|
2021-09-25 23:56:26 +08:00
|
|
|
} from 'sequelize';
|
2021-12-06 21:12:54 +08:00
|
|
|
|
2021-09-25 23:56:26 +08:00
|
|
|
import { Collection } from './collection';
|
2021-12-06 21:12:54 +08:00
|
|
|
import lodash, { omit } from 'lodash';
|
2021-09-25 23:56:26 +08:00
|
|
|
import { Database } from './database';
|
2021-12-06 21:12:54 +08:00
|
|
|
import { updateAssociations, updateModelByValues } from './update-associations';
|
2021-09-27 15:28:32 +08:00
|
|
|
import { RelationField } from './fields';
|
2021-12-06 21:12:54 +08:00
|
|
|
import FilterParser from './filter-parser';
|
|
|
|
import { OptionsParser } from './options-parser';
|
|
|
|
import { RelationRepository } from './relation-repository/relation-repository';
|
|
|
|
import { HasOneRepository } from './relation-repository/hasone-repository';
|
|
|
|
import { BelongsToRepository } from './relation-repository/belongs-to-repository';
|
|
|
|
import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
|
|
|
|
import { HasManyRepository } from './relation-repository/hasmany-repository';
|
|
|
|
import { UpdateGuard } from './update-guard';
|
|
|
|
import { transactionWrapperBuilder } from './transaction-decorator';
|
|
|
|
|
|
|
|
const debug = require('debug')('noco-database');
|
2021-09-23 00:16:04 +08:00
|
|
|
|
2021-09-25 23:56:26 +08:00
|
|
|
export interface IRepository {}
|
2021-09-23 00:16:04 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
interface CreateManyOptions extends BulkCreateOptions {
|
|
|
|
records: Values[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TransactionAble {
|
|
|
|
transaction?: Transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FilterAble {
|
|
|
|
filter: Filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PrimaryKey = string | number;
|
|
|
|
export type PK = PrimaryKey | PrimaryKey[];
|
|
|
|
|
|
|
|
export type Filter = any;
|
|
|
|
export type Appends = string[];
|
|
|
|
export type Except = string[];
|
|
|
|
export type Fields = string[];
|
|
|
|
export type Sort = string[];
|
|
|
|
|
|
|
|
export type WhiteList = string[];
|
|
|
|
export type BlackList = string[];
|
|
|
|
export type AssociationKeysToBeUpdate = string[];
|
|
|
|
|
|
|
|
export type Values = {
|
|
|
|
[key: string]: string | number | Values | Array<number | string | Values>;
|
|
|
|
};
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export interface CountOptions extends Omit<SequelizeCreateOptions, 'distinct' | 'where' | 'include'>, TransactionAble {
|
2021-12-06 21:12:54 +08:00
|
|
|
fields?: Fields;
|
|
|
|
filter?: Filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FilterByPK {
|
|
|
|
filterByPk?: PrimaryKey;
|
|
|
|
}
|
2021-09-27 15:28:32 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export interface FindOptions extends SequelizeFindOptions, CommonFindOptions, FilterByPK {}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
export interface CommonFindOptions {
|
|
|
|
filter?: Filter;
|
|
|
|
fields?: Fields;
|
|
|
|
appends?: Appends;
|
|
|
|
except?: Except;
|
|
|
|
sort?: Sort;
|
2021-09-25 23:56:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
interface FindOneOptions extends FindOptions, CommonFindOptions {}
|
|
|
|
|
|
|
|
export interface DestroyOptions extends TransactionAble {
|
|
|
|
filter?: Filter;
|
|
|
|
filterByPk?: PrimaryKey | PrimaryKey[];
|
|
|
|
truncate?: boolean;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
interface FindAndCountOptions extends Omit<SequelizeAndCountOptions, 'where' | 'include' | 'order'> {
|
2021-12-06 21:12:54 +08:00
|
|
|
// 数据过滤
|
|
|
|
filter?: Filter;
|
|
|
|
// 输出结果显示哪些字段
|
|
|
|
fields?: Fields;
|
|
|
|
// 输出结果不显示哪些字段
|
|
|
|
except?: Except;
|
|
|
|
// 附加字段,用于控制关系字段的输出
|
|
|
|
appends?: Appends;
|
|
|
|
// 排序,字段前面加上 “-” 表示降序
|
|
|
|
sort?: Sort;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
export interface CreateOptions extends TransactionAble {
|
|
|
|
values?: Values;
|
|
|
|
whitelist?: WhiteList;
|
|
|
|
blacklist?: BlackList;
|
|
|
|
updateAssociationValues?: AssociationKeysToBeUpdate;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
export interface UpdateOptions extends TransactionAble {
|
|
|
|
values: Values;
|
|
|
|
filter?: Filter;
|
|
|
|
filterByPk?: PrimaryKey;
|
|
|
|
whitelist?: WhiteList;
|
|
|
|
blacklist?: BlackList;
|
|
|
|
updateAssociationValues?: AssociationKeysToBeUpdate;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interface RelatedQueryOptions {
|
|
|
|
database: Database;
|
|
|
|
field: RelationField;
|
|
|
|
source: {
|
|
|
|
idOrInstance: any;
|
|
|
|
collection: Collection;
|
|
|
|
};
|
|
|
|
target: {
|
|
|
|
association: Association & {
|
|
|
|
accessors: any;
|
|
|
|
};
|
|
|
|
collection: Collection;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
const transaction = transactionWrapperBuilder(function () {
|
|
|
|
return (<Repository>this).collection.model.sequelize.transaction();
|
|
|
|
});
|
2021-09-27 15:28:32 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
class RelationRepositoryBuilder<R extends RelationRepository> {
|
|
|
|
collection: Collection;
|
|
|
|
associationName: string;
|
|
|
|
association: Association;
|
|
|
|
|
|
|
|
builderMap = {
|
|
|
|
HasOne: HasOneRepository,
|
|
|
|
BelongsTo: BelongsToRepository,
|
|
|
|
BelongsToMany: BelongsToManyRepository,
|
|
|
|
HasMany: HasManyRepository,
|
|
|
|
};
|
2021-09-27 15:28:32 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
constructor(collection: Collection, associationName: string) {
|
|
|
|
this.collection = collection;
|
|
|
|
this.associationName = associationName;
|
|
|
|
this.association = this.collection.model.associations[this.associationName];
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
protected builder() {
|
|
|
|
return this.builderMap;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
of(id: string | number): R {
|
|
|
|
const klass = this.builder()[this.association.associationType];
|
|
|
|
return new klass(this.collection, this.associationName, id);
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes>
|
|
|
|
implements IRepository
|
2021-12-06 21:12:54 +08:00
|
|
|
{
|
2021-09-25 23:56:26 +08:00
|
|
|
database: Database;
|
2021-09-27 15:28:32 +08:00
|
|
|
collection: Collection;
|
|
|
|
model: ModelCtor<Model>;
|
2021-09-23 00:16:04 +08:00
|
|
|
|
2021-09-25 23:56:26 +08:00
|
|
|
constructor(collection: Collection) {
|
|
|
|
this.database = collection.context.database;
|
|
|
|
this.collection = collection;
|
2021-09-27 15:28:32 +08:00
|
|
|
this.model = collection.model;
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* return count by filter
|
|
|
|
*/
|
|
|
|
async count(countOptions?: CountOptions): Promise<number> {
|
|
|
|
let options = countOptions ? lodash.clone(countOptions) : {};
|
|
|
|
|
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
|
|
|
|
if (countOptions?.filter) {
|
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
...this.parseFilter(countOptions.filter),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const count = await this.collection.model.count({
|
|
|
|
...options,
|
|
|
|
distinct: true,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
return count as any;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
async find(options?: FindOptions) {
|
2021-09-25 23:56:26 +08:00
|
|
|
const model = this.collection.model;
|
2021-12-06 21:12:54 +08:00
|
|
|
const transaction = await this.getTransaction(options);
|
2021-09-25 23:56:26 +08:00
|
|
|
const opts = {
|
|
|
|
subQuery: false,
|
2021-09-27 15:28:32 +08:00
|
|
|
...this.buildQueryOptions(options),
|
2021-09-25 23:56:26 +08:00
|
|
|
};
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
if (opts.include && opts.include.length > 0) {
|
2021-09-25 23:56:26 +08:00
|
|
|
const ids = (
|
|
|
|
await model.findAll({
|
|
|
|
...opts,
|
|
|
|
includeIgnoreAttributes: false,
|
|
|
|
attributes: [model.primaryKeyAttribute],
|
|
|
|
group: `${model.name}.${model.primaryKeyAttribute}`,
|
2021-12-06 21:12:54 +08:00
|
|
|
transaction,
|
2021-09-25 23:56:26 +08:00
|
|
|
})
|
2021-12-06 21:12:54 +08:00
|
|
|
).map((row) => row.get(model.primaryKeyAttribute));
|
|
|
|
|
|
|
|
const where = {
|
|
|
|
[model.primaryKeyAttribute]: {
|
|
|
|
[Op.in]: ids,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return await model.findAll({
|
|
|
|
...omit(opts, ['limit', 'offset']),
|
|
|
|
where,
|
|
|
|
transaction,
|
2021-09-25 23:56:26 +08:00
|
|
|
});
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
return await model.findAll({
|
2021-09-25 23:56:26 +08:00
|
|
|
...opts,
|
2021-12-06 21:12:54 +08:00
|
|
|
transaction,
|
2021-09-25 23:56:26 +08:00
|
|
|
});
|
|
|
|
}
|
2021-09-23 00:16:04 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* find and count
|
|
|
|
* @param options
|
|
|
|
*/
|
2021-12-06 21:23:34 +08:00
|
|
|
async findAndCount(options?: FindAndCountOptions): Promise<[Model[], number]> {
|
2021-12-06 21:12:54 +08:00
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
options = {
|
|
|
|
...options,
|
|
|
|
transaction,
|
2021-09-27 15:28:32 +08:00
|
|
|
};
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
return [await this.find(options), await this.count(options)];
|
2021-09-25 23:56:26 +08:00
|
|
|
}
|
2021-09-23 00:16:04 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* Find By Id
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
findById(id: PrimaryKey) {
|
|
|
|
return this.collection.model.findByPk(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find one record from database
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
async findOne(options?: FindOneOptions) {
|
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
|
|
|
|
const rows = await this.find({ ...options, limit: 1, transaction });
|
|
|
|
return rows.length == 1 ? rows[0] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save instance to database
|
|
|
|
*
|
|
|
|
* @param values
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
@transaction()
|
|
|
|
async create(options: CreateOptions): Promise<Model> {
|
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
|
|
|
|
const guard = UpdateGuard.fromOptions(this.model, options);
|
|
|
|
const values = guard.sanitize(options.values || {});
|
|
|
|
|
|
|
|
const instance = await this.model.create<any>(values, { transaction });
|
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
if (!instance) {
|
|
|
|
return;
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
await updateAssociations(instance, values, {
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
return instance;
|
|
|
|
}
|
2021-09-25 23:56:26 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* Save Many instances to database
|
|
|
|
*
|
|
|
|
* @param records
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
@transaction()
|
|
|
|
async createMany(options: CreateManyOptions) {
|
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
const { records } = options;
|
|
|
|
const instances = await this.collection.model.bulkCreate(records, {
|
|
|
|
...options,
|
|
|
|
transaction,
|
2021-09-25 23:56:26 +08:00
|
|
|
});
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
for (let i = 0; i < instances.length; i++) {
|
|
|
|
await updateAssociations(instances[i], records[i], { transaction });
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
return instances;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* Update model value
|
|
|
|
*
|
|
|
|
* @param values
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
@transaction()
|
|
|
|
async update(options: UpdateOptions) {
|
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
const guard = UpdateGuard.fromOptions(this.model, options);
|
|
|
|
|
|
|
|
const values = guard.sanitize(options.values);
|
|
|
|
|
|
|
|
const queryOptions = this.buildQueryOptions(options);
|
|
|
|
|
|
|
|
const instances = await this.find({
|
|
|
|
...queryOptions,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const instance of instances) {
|
|
|
|
await updateModelByValues(instance, values, {
|
|
|
|
sanitized: true,
|
|
|
|
transaction,
|
2021-09-27 15:28:32 +08:00
|
|
|
});
|
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@transaction((args, transaction) => {
|
|
|
|
return {
|
|
|
|
filterByPk: args[0],
|
|
|
|
transaction,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
async destroy(options?: PrimaryKey | PrimaryKey[] | DestroyOptions) {
|
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
|
|
|
|
options = <DestroyOptions>options;
|
|
|
|
|
|
|
|
let filterByPk = options.filterByPk;
|
|
|
|
|
|
|
|
if (filterByPk) {
|
|
|
|
if (!Array.isArray(filterByPk)) {
|
|
|
|
filterByPk = [filterByPk];
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:28:32 +08:00
|
|
|
return await this.model.destroy({
|
|
|
|
where: {
|
|
|
|
[this.model.primaryKeyAttribute]: {
|
2021-12-06 21:12:54 +08:00
|
|
|
[Op.in]: filterByPk,
|
2021-09-27 15:28:32 +08:00
|
|
|
},
|
|
|
|
},
|
2021-12-06 21:12:54 +08:00
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.filter) {
|
|
|
|
const instances = await this.find({
|
|
|
|
filter: options.filter,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
return await this.destroy({
|
2021-12-06 21:23:34 +08:00
|
|
|
filterByPk: instances.map((instance) => instance[this.model.primaryKeyAttribute]),
|
2021-12-06 21:12:54 +08:00
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.truncate) {
|
|
|
|
return await this.model.destroy({
|
|
|
|
truncate: true,
|
|
|
|
transaction,
|
2021-09-27 15:28:32 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
/**
|
|
|
|
* @param association target association
|
|
|
|
*/
|
2021-12-06 21:23:34 +08:00
|
|
|
relation<R extends RelationRepository>(association: string): RelationRepositoryBuilder<R> {
|
2021-12-06 21:12:54 +08:00
|
|
|
return new RelationRepositoryBuilder<R>(this.collection, association);
|
|
|
|
}
|
2021-09-27 15:28:32 +08:00
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
protected buildQueryOptions(options: any) {
|
2021-12-06 21:23:34 +08:00
|
|
|
const parser = new OptionsParser(this.collection.model, this.collection.context.database, options);
|
2021-12-06 21:12:54 +08:00
|
|
|
const params = parser.toSequelizeParams();
|
|
|
|
debug('sequelize query params %o', params);
|
|
|
|
return { ...options, ...params };
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
protected parseFilter(filter: Filter) {
|
2021-12-06 21:23:34 +08:00
|
|
|
const parser = new FilterParser(this.collection.model, this.collection.context.database, filter);
|
2021-12-06 21:12:54 +08:00
|
|
|
return parser.toSequelizeParams();
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:12:54 +08:00
|
|
|
protected async getTransaction(options: any, autoGen = false) {
|
|
|
|
if (lodash.isPlainObject(options) && options.transaction) {
|
|
|
|
return options.transaction;
|
2021-09-27 15:28:32 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
if (autoGen) {
|
|
|
|
return await this.model.sequelize.transaction();
|
2021-09-25 23:56:26 +08:00
|
|
|
}
|
2021-12-06 21:12:54 +08:00
|
|
|
|
|
|
|
return null;
|
2021-09-25 23:56:26 +08:00
|
|
|
}
|
2021-09-23 00:16:04 +08:00
|
|
|
}
|