add feature to create db itself
This commit is contained in:
parent
7208720fb6
commit
af3e6ce192
@ -1,9 +1,14 @@
|
|||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
import Knex from 'knex';
|
import Knex from 'knex';
|
||||||
|
import {Knex as KnexConfig} from './config';
|
||||||
|
|
||||||
export async function connectDB(){
|
export async function connectDB(){
|
||||||
const config = require('./../knexfile');
|
|
||||||
const env = process.env.NODE_ENV || 'development';
|
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]);
|
const knex = Knex(config[env]);
|
||||||
let tries = 0;
|
let tries = 0;
|
||||||
for(;;){
|
for(;;){
|
||||||
@ -24,5 +29,9 @@ export async function connectDB(){
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if(init_need){
|
||||||
|
const migrate = await import("../migrations/initial");
|
||||||
|
await migrate.up(knex);
|
||||||
|
}
|
||||||
return knex;
|
return knex;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user