From bca63298dc4adb384c3be2ba0d169f2103b105d3 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 29 Apr 2022 20:04:02 +0800 Subject: [PATCH] feat: db authenticate (#342) --- packages/core/database/src/database.ts | 34 +++++++++++++++++++- packages/core/server/src/commands/db-auth.ts | 4 +++ packages/core/server/src/commands/index.ts | 1 + 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 packages/core/server/src/commands/db-auth.ts diff --git a/packages/core/database/src/database.ts b/packages/core/database/src/database.ts index 024412837..68457ac3d 100644 --- a/packages/core/database/src/database.ts +++ b/packages/core/database/src/database.ts @@ -2,7 +2,16 @@ import { applyMixins, AsyncEmitter } from '@nocobase/utils'; import merge from 'deepmerge'; import { EventEmitter } from 'events'; import lodash from 'lodash'; -import { ModelCtor, Op, Options, QueryInterfaceDropAllTablesOptions, Sequelize, SyncOptions, Utils } from 'sequelize'; +import { + ModelCtor, + Op, + Options, + QueryInterfaceDropAllTablesOptions, + QueryOptions, + Sequelize, + SyncOptions, + Utils +} from 'sequelize'; import { Collection, CollectionOptions, RepositoryType } from './collection'; import { ImporterReader, ImportFileExtension } from './collection-importer'; import * as FieldTypes from './fields'; @@ -248,6 +257,29 @@ export class Database extends EventEmitter implements AsyncEmitter { return this.sequelize.getDialect() === 'sqlite' && lodash.get(this.options, 'storage') == ':memory:'; } + async auth(options: QueryOptions & { repeat?: number } = {}) { + const { repeat = 10, ...others } = options; + const delay = (ms) => new Promise((yea) => setTimeout(yea, ms)); + let count = 0; + const authenticate = async () => { + try { + await this.sequelize.authenticate(others); + console.log('Connection has been established successfully.'); + return true; + } catch (error) { + console.log('reconnecting...', count); + if (count >= repeat) { + throw error; + } + ++count; + await delay(500); + return await authenticate(); + } + }; + + return await authenticate(); + } + async reconnect() { if (this.isSqliteMemory()) { return; diff --git a/packages/core/server/src/commands/db-auth.ts b/packages/core/server/src/commands/db-auth.ts new file mode 100644 index 000000000..822fde10b --- /dev/null +++ b/packages/core/server/src/commands/db-auth.ts @@ -0,0 +1,4 @@ +export default async ({ app, cliArgs }) => { + const [opts] = cliArgs; + await app.db.auth({ repeat: opts.repeat || 10 }); +}; diff --git a/packages/core/server/src/commands/index.ts b/packages/core/server/src/commands/index.ts index 033db6bca..de43feeb2 100644 --- a/packages/core/server/src/commands/index.ts +++ b/packages/core/server/src/commands/index.ts @@ -22,6 +22,7 @@ export function createCli(app: Application) { program.command('start').description('start NocoBase application').option('-s, --silent').action(runSubCommand('start')); program.command('install').option('-f, --force').option('-c, --clean').option('-s, --silent').action(runSubCommand('install')); program.command('db:sync').option('-f, --force').action(runSubCommand('db-sync')); + program.command('db:auth').option('-r, --repeat [repeat]').action(runSubCommand('db-auth')); program.command('console').action(runSubCommand('console')); program