nocobase/packages/database/src/database.ts

326 lines
9.2 KiB
TypeScript
Raw Normal View History

Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 03:11:36 +00:00
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
import merge from 'deepmerge';
import { EventEmitter } from 'events';
import {
Model,
ModelCtor,
Op,
Options,
QueryInterfaceDropAllTablesOptions,
Sequelize,
SyncOptions,
Utils
} from 'sequelize';
2021-12-06 13:23:34 +00:00
import { Collection, CollectionOptions, RepositoryType } from './collection';
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 03:11:36 +00:00
import { ImporterReader, ImportFileExtension } from './collection-importer';
2021-12-06 13:23:34 +00:00
import * as FieldTypes from './fields';
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 03:11:36 +00:00
import { Field, FieldContext, RelationField } from './fields';
2021-12-06 13:23:34 +00:00
import { ModelHook } from './model-hook';
import extendOperators from './operators';
import { RelationRepository } from './relation-repository/relation-repository';
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 03:11:36 +00:00
import { Repository } from './repository';
2020-10-24 07:34:43 +00:00
2021-12-06 13:23:34 +00:00
export interface MergeOptions extends merge.Options {}
2020-10-24 07:34:43 +00:00
2021-12-06 13:23:34 +00:00
export interface PendingOptions {
field: RelationField;
model: ModelCtor<Model>;
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
interface MapOf<T> {
[key: string]: T;
}
2021-12-06 13:23:34 +00:00
export type DatabaseOptions = Options | Sequelize;
2020-12-15 12:16:55 +00:00
2021-12-06 13:23:34 +00:00
interface RegisterOperatorsContext {
db?: Database;
path?: string;
field?: Field;
2021-01-14 02:35:15 +00:00
}
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 03:11:36 +00:00
export interface CleanOptions extends QueryInterfaceDropAllTablesOptions {
drop?: boolean;
}
2021-12-06 13:23:34 +00:00
type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
2021-01-14 02:35:15 +00:00
2021-12-06 13:23:34 +00:00
export class Database extends EventEmitter implements AsyncEmitter {
sequelize: Sequelize;
fieldTypes = new Map();
models = new Map<string, ModelCtor<any>>();
repositories = new Map<string, RepositoryType>();
operators = new Map();
collections = new Map<string, Collection>();
pendingFields = new Map<string, RelationField[]>();
modelCollection = new Map<ModelCtor<any>, Collection>();
2020-10-24 07:34:43 +00:00
2021-12-06 13:23:34 +00:00
modelHook: ModelHook;
2020-10-24 07:34:43 +00:00
2021-12-06 13:23:34 +00:00
delayCollectionExtend = new Map<string, { collectionOptions: CollectionOptions; mergeOptions?: any }[]>();
constructor(options: DatabaseOptions) {
super();
2020-10-24 07:34:43 +00:00
2021-12-06 13:23:34 +00:00
if (options instanceof Sequelize) {
this.sequelize = options;
} else {
this.sequelize = new Sequelize(options);
}
2021-12-06 13:23:34 +00:00
this.collections = new Map();
this.modelHook = new ModelHook(this);
2021-12-06 13:23:34 +00:00
this.on('afterDefineCollection', (collection: Collection) => {
// after collection defined, call bind method on pending fields
this.pendingFields.get(collection.name)?.forEach((field) => field.bind());
this.delayCollectionExtend.get(collection.name)?.forEach((collectionExtend) => {
collection.updateOptions(collectionExtend.collectionOptions, collectionExtend.mergeOptions);
});
});
2021-12-06 13:23:34 +00:00
// register database field types
for (const [name, field] of Object.entries(FieldTypes)) {
if (['Field', 'RelationField'].includes(name)) {
continue;
}
2021-12-06 13:23:34 +00:00
let key = name.replace(/Field$/g, '');
key = key.substring(0, 1).toLowerCase() + key.substring(1);
this.registerFieldTypes({
[key]: field,
});
}
2021-12-06 13:23:34 +00:00
this.initOperators();
}
2020-10-24 07:34:43 +00:00
/**
2021-12-06 13:23:34 +00:00
* Add collection to database
* @param options
2020-10-24 07:34:43 +00:00
*/
2021-12-06 13:23:34 +00:00
collection<Attributes = any, CreateAttributes = Attributes>(
options: CollectionOptions,
): Collection<Attributes, CreateAttributes> {
this.emit('beforeDefineCollection', options);
const collection = new Collection(options, {
database: this,
2020-10-24 07:34:43 +00:00
});
2021-12-06 13:23:34 +00:00
this.collections.set(collection.name, collection);
this.modelCollection.set(collection.model, collection);
this.emit('afterDefineCollection', collection);
return collection;
2020-10-24 07:34:43 +00:00
}
/**
2021-12-06 13:23:34 +00:00
* get exists collection by its name
* @param name
2020-10-24 07:34:43 +00:00
*/
2021-12-06 13:23:34 +00:00
getCollection(name: string): Collection {
return this.collections.get(name);
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
hasCollection(name: string): boolean {
return this.collections.has(name);
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
removeCollection(name: string) {
const collection = this.collections.get(name);
this.emit('beforeRemoveCollection', collection);
const result = this.collections.delete(name);
if (result) {
this.emit('afterRemoveCollection', collection);
}
2020-10-24 07:34:43 +00:00
}
getModel<M extends Model>(name: string) {
return this.getCollection(name).model as ModelCtor<M>;
}
getRepository<R extends Repository>(name: string): R;
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
getRepository<R extends RelationRepository>(name: string, relationId?: string | number): Repository | R {
if (relationId) {
const [collection, relation] = name.split('.');
return this.getRepository(collection).relation(relation).of(relationId) as R;
}
return this.getCollection(name).repository;
}
2021-12-06 13:23:34 +00:00
addPendingField(field: RelationField) {
const associating = this.pendingFields;
const items = this.pendingFields.get(field.target) || [];
items.push(field);
associating.set(field.target, items);
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
removePendingField(field: RelationField) {
const items = this.pendingFields.get(field.target) || [];
const index = items.indexOf(field);
if (index !== -1) {
delete items[index];
this.pendingFields.set(field.target, items);
}
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
registerFieldTypes(fieldTypes: MapOf<typeof Field>) {
for (const [type, fieldType] of Object.entries(fieldTypes)) {
this.fieldTypes.set(type, fieldType);
}
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
registerModels(models: MapOf<ModelCtor<any>>) {
for (const [type, schemaType] of Object.entries(models)) {
this.models.set(type, schemaType);
}
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
registerRepositories(repositories: MapOf<RepositoryType>) {
for (const [type, schemaType] of Object.entries(repositories)) {
this.repositories.set(type, schemaType);
2020-10-24 07:34:43 +00:00
}
}
2021-12-06 13:23:34 +00:00
initOperators() {
const operators = new Map();
// Sequelize 内置
for (const key in Op) {
operators.set('$' + key, Op[key]);
const val = Utils.underscoredIf(key, true);
operators.set('$' + val, Op[key]);
operators.set('$' + val.replace(/_/g, ''), Op[key]);
}
this.operators = operators;
this.registerOperators({
...extendOperators,
});
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
registerOperators(operators: MapOf<OperatorFunc>) {
for (const [key, operator] of Object.entries(operators)) {
this.operators.set(key, operator);
}
}
2020-10-24 07:34:43 +00:00
2021-12-06 13:23:34 +00:00
buildField(options, context: FieldContext) {
const { type } = options;
const Field = this.fieldTypes.get(type);
if (!Field) {
throw Error(`unsupported field type ${type}`);
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
return new Field(options, context);
}
2020-10-24 07:34:43 +00:00
2021-12-06 13:23:34 +00:00
async sync(options?: SyncOptions) {
const isMySQL = this.sequelize.getDialect() === 'mysql';
if (isMySQL) {
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 0', null);
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
const result = await this.sequelize.sync(options);
if (isMySQL) {
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 1', null);
}
return result;
2020-10-24 07:34:43 +00:00
}
Application (#175) * feat: getRepository * getRepository return type * export action * add: acl * feat: setResourceAction * feat: action alias * chore: code struct * feat: removeResourceAction * chore: file name * ignorecase * remove ACL * feat: ACL * feat: role toJSON * using emit * chore: test * feat: plugin-acl * feat: acl with predicate * grant universal action test * grant action test * update resource action test * revoke resource action * usingActionsConfig switch * plugin-ui-schema-storage * remove global acl instance * fix: collection manager with sqlite * add own action listener * add acl middleware * add acl allowConfigure strategy option * add plugin-acl allowConfigure * change acl resourceName * add acl middleware merge params * bugfix * append fields on acl action params * acl middleware parse template * fix: collection-manager migrate * add acl association field test * feat(plugin-acl): grant association field actions * chore(plugin-acl): type name * feat(plugin-acl): regrant actions on resource action update * feat(plugin-acl): regrant action on field destroy * fix(plugin-acl): test * fix(plugin-acl): test run * feat(plugin-acl): set default role * feat(plugin-users): set user default role * test(plugin-users): create user with role * feat(plugin-users): create user with role * feat(application): application hook * feat(database): reconnect * feat(database): application life cycle * feat(database): sync with option * feat(database): hook position * feat(database): hook position * feat(database): remove load in start * fix(application): get plugin * feat(test): loadAndInstall * feat: improve code * feat: improve code * fix: listen options * fix: bug * test(database): add test case Co-authored-by: chenos <chenlinxh@gmail.com>
2022-01-30 03:11:36 +00:00
async clean(options: CleanOptions) {
const { drop, ...others } = options;
if (drop) {
await this.sequelize.getQueryInterface().dropAllTables(others);
}
}
async reconnect() {
// @ts-ignore
const ConnectionManager = this.sequelize.dialect.connectionManager.constructor;
// @ts-ignore
const connectionManager = new ConnectionManager(this.sequelize.dialect, this.sequelize);
// @ts-ignore
this.sequelize.dialect.connectionManager = connectionManager;
// @ts-ignore
this.sequelize.connectionManager = connectionManager;
}
closed() {
// @ts-ignore
return this.sequelize.connectionManager.pool._draining;
}
2021-12-06 13:23:34 +00:00
async close() {
return this.sequelize.close();
2020-10-24 07:34:43 +00:00
}
2020-12-15 12:16:55 +00:00
2021-12-06 13:23:34 +00:00
on(event: string | symbol, listener: (...args: any[]) => void): this {
const modelEventName = this.modelHook.isModelHook(event);
if (modelEventName && !this.modelHook.hasBindEvent(modelEventName)) {
this.sequelize.addHook(modelEventName, this.modelHook.sequelizeHookBuilder(modelEventName));
this.modelHook.bindEvent(modelEventName);
}
return super.on(event, listener);
2021-09-28 23:38:05 +00:00
}
2021-12-06 13:23:34 +00:00
async import(options: { directory: string; extensions?: ImportFileExtension[] }): Promise<Map<string, Collection>> {
const reader = new ImporterReader(options.directory, options.extensions);
const modules = await reader.read();
const result = new Map<string, Collection>();
for (const module of modules) {
if (module.extend) {
const collectionName = module.collectionOptions.name;
const existCollection = this.getCollection(collectionName);
if (existCollection) {
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
} else {
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
}
} else {
const collection = this.collection(module);
result.set(collection.name, collection);
2021-09-28 23:38:05 +00:00
}
}
2021-12-06 13:23:34 +00:00
return result;
2020-12-29 06:53:39 +00:00
}
2021-12-06 13:23:34 +00:00
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
}
export function extend(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
return {
collectionOptions,
mergeOptions,
extend: true,
};
2020-10-24 07:34:43 +00:00
}
2021-12-06 13:23:34 +00:00
export const defineCollection = (collectionOptions: CollectionOptions) => {
return collectionOptions;
};
export const extendCollection = (collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) => {
return {
collectionOptions,
mergeOptions,
extend: true,
};
};
2021-12-06 13:23:34 +00:00
applyMixins(Database, [AsyncEmitter]);
export default Database;