2022-01-30 11:11:36 +08:00
|
|
|
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
|
|
|
import merge from 'deepmerge';
|
2021-09-07 00:28:16 +08:00
|
|
|
import { EventEmitter } from 'events';
|
2022-06-17 10:25:59 +08:00
|
|
|
import glob from 'glob';
|
2022-04-19 17:04:54 +08:00
|
|
|
import lodash from 'lodash';
|
2022-06-17 10:25:59 +08:00
|
|
|
import { basename, isAbsolute, resolve } from 'path';
|
2022-07-04 13:41:07 +08:00
|
|
|
import semver from 'semver';
|
2022-04-29 20:04:02 +08:00
|
|
|
import {
|
|
|
|
ModelCtor,
|
|
|
|
Op,
|
|
|
|
Options,
|
|
|
|
QueryInterfaceDropAllTablesOptions,
|
|
|
|
QueryOptions,
|
|
|
|
Sequelize,
|
|
|
|
SyncOptions,
|
2022-06-18 00:18:12 +08:00
|
|
|
Transactionable,
|
2022-04-29 20:04:02 +08:00
|
|
|
Utils
|
|
|
|
} from 'sequelize';
|
2022-05-30 23:10:32 +08:00
|
|
|
import { SequelizeStorage, Umzug } from 'umzug';
|
2021-12-06 21:23:34 +08:00
|
|
|
import { Collection, CollectionOptions, RepositoryType } from './collection';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { ImporterReader, ImportFileExtension } from './collection-importer';
|
2021-12-06 21:23:34 +08:00
|
|
|
import * as FieldTypes from './fields';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { Field, FieldContext, RelationField } from './fields';
|
2022-05-30 23:10:32 +08:00
|
|
|
import { Migrations } from './migration';
|
2022-02-23 18:18:38 +08:00
|
|
|
import { Model } from './model';
|
2021-12-06 21:23:34 +08:00
|
|
|
import { ModelHook } from './model-hook';
|
|
|
|
import extendOperators from './operators';
|
2022-01-18 16:38:03 +08:00
|
|
|
import { RelationRepository } from './relation-repository/relation-repository';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { Repository } from './repository';
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export interface MergeOptions extends merge.Options {}
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export interface PendingOptions {
|
|
|
|
field: RelationField;
|
|
|
|
model: ModelCtor<Model>;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
interface MapOf<T> {
|
|
|
|
[key: string]: T;
|
2021-01-13 16:23:15 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:20:25 +08:00
|
|
|
export interface IDatabaseOptions extends Options {
|
|
|
|
tablePrefix?: string;
|
2022-05-30 23:10:32 +08:00
|
|
|
migrator?: any;
|
2022-02-15 00:20:25 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 23:10:32 +08:00
|
|
|
export type DatabaseOptions = IDatabaseOptions;
|
2020-12-15 20:16:55 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
interface RegisterOperatorsContext {
|
|
|
|
db?: Database;
|
|
|
|
path?: string;
|
|
|
|
field?: Field;
|
2022-04-19 17:04:54 +08:00
|
|
|
app?: any;
|
2021-01-14 10:35:15 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
export interface CleanOptions extends QueryInterfaceDropAllTablesOptions {
|
|
|
|
drop?: boolean;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:25:59 +08:00
|
|
|
export type AddMigrationsOptions = {
|
|
|
|
context?: any;
|
|
|
|
namespace?: string;
|
|
|
|
extensions?: string[];
|
|
|
|
directory: string;
|
|
|
|
};
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
|
2021-01-14 10:35:15 +08:00
|
|
|
|
2022-07-04 13:41:07 +08:00
|
|
|
class DatabaseVersion {
|
|
|
|
db: Database;
|
|
|
|
|
|
|
|
constructor(db: Database) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
async satisfies(versions) {
|
|
|
|
const dialects = {
|
|
|
|
sqlite: {
|
|
|
|
sql: 'select sqlite_version() as version',
|
|
|
|
get: (v) => v,
|
|
|
|
},
|
|
|
|
mysql: {
|
|
|
|
sql: 'select version() as version',
|
|
|
|
get: (v) => v,
|
|
|
|
},
|
|
|
|
postgres: {
|
|
|
|
sql: 'select version() as version',
|
|
|
|
get: (v) => {
|
|
|
|
const keys = v.split(' ');
|
|
|
|
keys.shift();
|
|
|
|
return semver.minVersion(keys.shift()).version;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
for (const dialect of Object.keys(dialects)) {
|
|
|
|
if (this.db.inDialect(dialect)) {
|
|
|
|
if (!versions?.[dialect]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const [result] = (await this.db.sequelize.query(dialects[dialect].sql)) as any;
|
|
|
|
return semver.satisfies(dialects[dialect].get(result?.[0]?.version), versions[dialect]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
export class Database extends EventEmitter implements AsyncEmitter {
|
|
|
|
sequelize: Sequelize;
|
2022-05-30 23:10:32 +08:00
|
|
|
migrator: Umzug;
|
|
|
|
migrations: Migrations;
|
2021-12-06 21:23:34 +08:00
|
|
|
fieldTypes = new Map();
|
2022-02-15 00:20:25 +08:00
|
|
|
options: IDatabaseOptions;
|
2022-02-23 18:18:38 +08:00
|
|
|
models = new Map<string, ModelCtor<Model>>();
|
2021-12-06 21:23:34 +08:00
|
|
|
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 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
modelHook: ModelHook;
|
2022-07-04 13:41:07 +08:00
|
|
|
version: DatabaseVersion;
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
delayCollectionExtend = new Map<string, { collectionOptions: CollectionOptions; mergeOptions?: any }[]>();
|
|
|
|
|
|
|
|
constructor(options: DatabaseOptions) {
|
2021-09-07 00:28:16 +08:00
|
|
|
super();
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2022-07-04 13:41:07 +08:00
|
|
|
this.version = new DatabaseVersion(this);
|
|
|
|
|
2022-05-30 23:10:32 +08:00
|
|
|
const opts = {
|
|
|
|
sync: {
|
|
|
|
alter: {
|
|
|
|
drop: false,
|
2022-04-30 17:28:30 +08:00
|
|
|
},
|
2022-05-30 23:10:32 +08:00
|
|
|
force: false,
|
|
|
|
},
|
2022-08-04 21:27:55 +08:00
|
|
|
...lodash.clone(options),
|
2022-05-30 23:10:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options.storage && options.storage !== ':memory:') {
|
|
|
|
if (!isAbsolute(options.storage)) {
|
|
|
|
opts.storage = resolve(process.cwd(), options.storage);
|
2022-05-19 00:40:55 +08:00
|
|
|
}
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-01 11:41:46 +08:00
|
|
|
if (options.dialect === 'sqlite') {
|
|
|
|
delete opts.timezone;
|
2022-06-01 12:25:21 +08:00
|
|
|
} else if (!opts.timezone) {
|
|
|
|
opts.timezone = '+00:00';
|
2022-06-01 11:41:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-30 23:10:32 +08:00
|
|
|
this.sequelize = new Sequelize(opts);
|
|
|
|
this.options = opts;
|
2021-12-06 21:23:34 +08:00
|
|
|
this.collections = new Map();
|
|
|
|
this.modelHook = new ModelHook(this);
|
2021-09-07 00:28:16 +08:00
|
|
|
|
2021-12-06 21:23:34 +08: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-09-07 00:28:16 +08:00
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
// register database field types
|
|
|
|
for (const [name, field] of Object.entries(FieldTypes)) {
|
|
|
|
if (['Field', 'RelationField'].includes(name)) {
|
|
|
|
continue;
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
let key = name.replace(/Field$/g, '');
|
|
|
|
key = key.substring(0, 1).toLowerCase() + key.substring(1);
|
|
|
|
this.registerFieldTypes({
|
|
|
|
[key]: field,
|
|
|
|
});
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
this.initOperators();
|
2022-05-30 23:10:32 +08:00
|
|
|
|
|
|
|
const migratorOptions: any = this.options.migrator || {};
|
|
|
|
|
|
|
|
const context = {
|
|
|
|
db: this,
|
|
|
|
sequelize: this.sequelize,
|
|
|
|
queryInterface: this.sequelize.getQueryInterface(),
|
|
|
|
...migratorOptions.context,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.migrations = new Migrations(context);
|
|
|
|
this.migrator = new Umzug({
|
|
|
|
logger: migratorOptions.logger || console,
|
|
|
|
migrations: this.migrations.callback(),
|
|
|
|
context,
|
|
|
|
storage: new SequelizeStorage({
|
2022-06-17 10:25:59 +08:00
|
|
|
modelName: `${this.options.tablePrefix || ''}migrations`,
|
2022-05-30 23:10:32 +08:00
|
|
|
...migratorOptions.storage,
|
|
|
|
sequelize: this.sequelize,
|
|
|
|
}),
|
|
|
|
});
|
2022-08-04 21:27:55 +08:00
|
|
|
|
|
|
|
this.sequelize.beforeDefine((model, opts) => {
|
|
|
|
if (this.options.tablePrefix) {
|
|
|
|
opts.tableName = `${this.options.tablePrefix}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
|
|
}
|
|
|
|
});
|
2022-05-30 23:10:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
addMigration(item) {
|
|
|
|
return this.migrations.add(item);
|
2021-09-07 00:28:16 +08:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:25:59 +08:00
|
|
|
addMigrations(options: AddMigrationsOptions) {
|
|
|
|
const { namespace, context, extensions = ['js', 'ts'], directory } = options;
|
|
|
|
const patten = `${directory}/*.{${extensions.join(',')}}`;
|
|
|
|
const files = glob.sync(patten, {
|
|
|
|
ignore: ['**/*.d.ts'],
|
|
|
|
});
|
|
|
|
for (const file of files) {
|
|
|
|
let filename = basename(file);
|
|
|
|
filename = filename.substring(0, filename.lastIndexOf('.')) || filename;
|
|
|
|
this.migrations.add({
|
|
|
|
name: namespace ? `${namespace}/${filename}` : filename,
|
|
|
|
migration: this.requireModule(file),
|
|
|
|
context,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 14:25:10 +08:00
|
|
|
inDialect(...dialect: string[]) {
|
|
|
|
return dialect.includes(this.sequelize.getDialect());
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:25:59 +08:00
|
|
|
private requireModule(module: any) {
|
|
|
|
if (typeof module === 'string') {
|
|
|
|
module = require(module);
|
|
|
|
}
|
|
|
|
if (typeof module !== 'object') {
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
return module.__esModule ? module.default : module;
|
|
|
|
}
|
|
|
|
|
2020-10-24 15:34:43 +08:00
|
|
|
/**
|
2021-12-06 21:23:34 +08:00
|
|
|
* Add collection to database
|
|
|
|
* @param options
|
2020-10-24 15:34:43 +08:00
|
|
|
*/
|
2021-12-06 21:23:34 +08: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 15:34:43 +08:00
|
|
|
});
|
2021-12-06 21:23:34 +08:00
|
|
|
|
|
|
|
this.collections.set(collection.name, collection);
|
|
|
|
this.modelCollection.set(collection.model, collection);
|
|
|
|
|
|
|
|
this.emit('afterDefineCollection', collection);
|
|
|
|
|
|
|
|
return collection;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-15 00:20:25 +08:00
|
|
|
getTablePrefix() {
|
|
|
|
return this.options.tablePrefix || '';
|
|
|
|
}
|
|
|
|
|
2020-10-24 15:34:43 +08:00
|
|
|
/**
|
2021-12-06 21:23:34 +08:00
|
|
|
* get exists collection by its name
|
|
|
|
* @param name
|
2020-10-24 15:34:43 +08:00
|
|
|
*/
|
2021-12-06 21:23:34 +08:00
|
|
|
getCollection(name: string): Collection {
|
|
|
|
return this.collections.get(name);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
hasCollection(name: string): boolean {
|
|
|
|
return this.collections.has(name);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
removeCollection(name: string) {
|
|
|
|
const collection = this.collections.get(name);
|
|
|
|
this.emit('beforeRemoveCollection', collection);
|
|
|
|
|
|
|
|
const result = this.collections.delete(name);
|
|
|
|
|
2022-06-18 00:18:12 +08:00
|
|
|
this.sequelize.modelManager.removeModel(collection.model);
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
if (result) {
|
|
|
|
this.emit('afterRemoveCollection', collection);
|
|
|
|
}
|
2022-06-11 20:46:30 +08:00
|
|
|
|
2022-06-18 00:18:12 +08:00
|
|
|
return collection;
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 12:38:57 +08:00
|
|
|
getModel<M extends Model>(name: string) {
|
|
|
|
return this.getCollection(name).model as ModelCtor<M>;
|
|
|
|
}
|
|
|
|
|
2022-01-23 12:42:35 +08:00
|
|
|
getRepository<R extends Repository>(name: string): R;
|
2022-01-18 16:38:03 +08:00
|
|
|
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('.');
|
2022-03-02 18:35:49 +08:00
|
|
|
return this.getRepository(collection)?.relation(relation)?.of(relationId) as R;
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
2022-03-02 18:35:49 +08:00
|
|
|
return this.getCollection(name)?.repository;
|
2022-01-18 16:38:03 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08: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 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08: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-11-11 15:23:39 +08:00
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
registerFieldTypes(fieldTypes: MapOf<typeof Field>) {
|
|
|
|
for (const [type, fieldType] of Object.entries(fieldTypes)) {
|
|
|
|
this.fieldTypes.set(type, fieldType);
|
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
registerModels(models: MapOf<ModelCtor<any>>) {
|
|
|
|
for (const [type, schemaType] of Object.entries(models)) {
|
|
|
|
this.models.set(type, schemaType);
|
2020-11-11 15:23:39 +08:00
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
registerRepositories(repositories: MapOf<RepositoryType>) {
|
|
|
|
for (const [type, schemaType] of Object.entries(repositories)) {
|
|
|
|
this.repositories.set(type, schemaType);
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08: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 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
registerOperators(operators: MapOf<OperatorFunc>) {
|
|
|
|
for (const [key, operator] of Object.entries(operators)) {
|
|
|
|
this.operators.set(key, operator);
|
|
|
|
}
|
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08: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 15:34:43 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
return new Field(options, context);
|
|
|
|
}
|
2020-10-24 15:34:43 +08:00
|
|
|
|
2021-12-06 21:23:34 +08: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 15:34:43 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08: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 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
async clean(options: CleanOptions) {
|
|
|
|
const { drop, ...others } = options;
|
|
|
|
if (drop) {
|
|
|
|
await this.sequelize.getQueryInterface().dropAllTables(others);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 00:18:12 +08:00
|
|
|
async collectionExistsInDb(name, options?: Transactionable) {
|
|
|
|
const tables = await this.sequelize.getQueryInterface().showAllTables({
|
|
|
|
transaction: options?.transaction,
|
|
|
|
});
|
|
|
|
return !!tables.find((table) => table === `${this.getTablePrefix()}${name}`);
|
2022-06-17 10:25:59 +08:00
|
|
|
}
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
public isSqliteMemory() {
|
|
|
|
return this.sequelize.getDialect() === 'sqlite' && lodash.get(this.options, 'storage') == ':memory:';
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:25:59 +08:00
|
|
|
async auth(options: QueryOptions & { retry?: number } = {}) {
|
|
|
|
const { retry = 10, ...others } = options;
|
2022-04-29 20:04:02 +08:00
|
|
|
const delay = (ms) => new Promise((yea) => setTimeout(yea, ms));
|
2022-05-19 00:40:55 +08:00
|
|
|
let count = 1;
|
2022-04-29 20:04:02 +08:00
|
|
|
const authenticate = async () => {
|
|
|
|
try {
|
|
|
|
await this.sequelize.authenticate(others);
|
|
|
|
console.log('Connection has been established successfully.');
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
2022-06-17 10:25:59 +08:00
|
|
|
if (count >= retry) {
|
2022-04-29 20:04:02 +08:00
|
|
|
throw error;
|
|
|
|
}
|
2022-05-19 00:40:55 +08:00
|
|
|
console.log('reconnecting...', count);
|
2022-04-29 20:04:02 +08:00
|
|
|
++count;
|
|
|
|
await delay(500);
|
|
|
|
return await authenticate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return await authenticate();
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
async reconnect() {
|
2022-03-28 22:01:10 +08:00
|
|
|
if (this.isSqliteMemory()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-30 11:11:36 +08:00
|
|
|
// @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 21:23:34 +08:00
|
|
|
async close() {
|
2022-03-28 22:01:10 +08:00
|
|
|
if (this.isSqliteMemory()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-11 15:23:39 +08:00
|
|
|
return this.sequelize.close();
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
2020-12-15 20:16:55 +08:00
|
|
|
|
2022-06-08 11:22:08 +08:00
|
|
|
on(event: string | symbol, listener): this {
|
|
|
|
// NOTE: to match if event is a sequelize or model type
|
|
|
|
const type = this.modelHook.match(event);
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2022-06-08 11:22:08 +08:00
|
|
|
if (type && !this.modelHook.hasBoundEvent(type)) {
|
|
|
|
this.sequelize.addHook(type, this.modelHook.buildSequelizeHook(type));
|
|
|
|
this.modelHook.bindEvent(type);
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.on(event, listener);
|
2021-09-29 07:38:05 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08: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-29 07:38:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
return result;
|
2020-12-29 14:53:39 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function extend(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
|
|
|
|
return {
|
|
|
|
collectionOptions,
|
|
|
|
mergeOptions,
|
|
|
|
extend: true,
|
|
|
|
};
|
2020-10-24 15:34:43 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2022-02-11 14:34:33 +08:00
|
|
|
export const defineCollection = (collectionOptions: CollectionOptions) => {
|
|
|
|
return collectionOptions;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const extendCollection = (collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) => {
|
|
|
|
return {
|
|
|
|
collectionOptions,
|
|
|
|
mergeOptions,
|
|
|
|
extend: true,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
applyMixins(Database, [AsyncEmitter]);
|
|
|
|
|
|
|
|
export default Database;
|