28 lines
706 B
TypeScript
28 lines
706 B
TypeScript
|
import { existsSync } from 'fs';
|
||
|
import Knex from 'knex';
|
||
|
|
||
|
export async function connectDB(){
|
||
|
const config = require('./../knexfile');
|
||
|
const env = process.env.NODE_ENV || 'development';
|
||
|
const knex = Knex(config[env]);
|
||
|
let tries = 0;
|
||
|
for(;;){
|
||
|
try{
|
||
|
console.log("try to connect db");
|
||
|
await knex.raw('select 1 + 1;');
|
||
|
console.log("connect success");
|
||
|
}
|
||
|
catch(err){
|
||
|
if(tries < 3){
|
||
|
tries++;
|
||
|
console.error(`connection fail ${err} retry...`);
|
||
|
continue;
|
||
|
}
|
||
|
else{
|
||
|
throw err;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
return knex;
|
||
|
}
|