add feature to create db itself

This commit is contained in:
monoid 2021-01-09 20:27:58 +09:00
parent 7208720fb6
commit af3e6ce192

View File

@ -1,9 +1,14 @@
import { existsSync } from 'fs';
import Knex from 'knex';
import {Knex as KnexConfig} from './config';
export async function connectDB(){
const config = require('./../knexfile');
const env = process.env.NODE_ENV || 'development';
const config = KnexConfig.config;
if(env != "production" && env != "development"){
throw new Error("process unknown value in NODE_ENV: must be either \"development\" or \"production\"");
}
const init_need = existsSync(config[env].connection.filename);
const knex = Knex(config[env]);
let tries = 0;
for(;;){
@ -24,5 +29,9 @@ export async function connectDB(){
}
break;
}
if(init_need){
const migrate = await import("../migrations/initial");
await migrate.up(knex);
}
return knex;
}