feat: db authenticate (#342)

This commit is contained in:
chenos 2022-04-29 20:04:02 +08:00 committed by GitHub
parent 687e1f4bc5
commit bca63298dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 1 deletions

View File

@ -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;

View File

@ -0,0 +1,4 @@
export default async ({ app, cliArgs }) => {
const [opts] = cliArgs;
await app.db.auth({ repeat: opts.repeat || 10 });
};

View File

@ -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