diff --git a/src/database.ts b/src/database.ts index 73e5186..e883d54 100644 --- a/src/database.ts +++ b/src/database.ts @@ -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; } \ No newline at end of file