feat: db authenticate (#342)
This commit is contained in:
parent
687e1f4bc5
commit
bca63298dc
@ -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;
|
||||
|
4
packages/core/server/src/commands/db-auth.ts
Normal file
4
packages/core/server/src/commands/db-auth.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export default async ({ app, cliArgs }) => {
|
||||
const [opts] = cliArgs;
|
||||
await app.db.auth({ repeat: opts.repeat || 10 });
|
||||
};
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user