ionian/src/database.ts

28 lines
706 B
TypeScript
Raw Normal View History

2020-12-31 03:06:16 +09:00
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;
}