From af3e6ce192f2f2f1f18651005195c6c29e488f5e Mon Sep 17 00:00:00 2001 From: monoid Date: Sat, 9 Jan 2021 20:27:58 +0900 Subject: [PATCH] add feature to create db itself --- src/database.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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