Fix database config parsing.

pull/12/head
Sander Vocke 5 years ago
parent 055ed3e86d
commit 6cfae4ebe5
  1. 24
      server/knex/get_knex.ts
  2. 23
      server/knex/knex.ts
  3. 2
      server/knexfile.ts
  4. 3
      server/server.ts

@ -0,0 +1,24 @@
const environment = process.env.ENVIRONMENT || 'development'
import config from '../knexfile';
import Knex from 'knex';
export default function get_knex() {
if (!Object.keys(config).includes(environment)) {
throw "No Knex database configuration was found for environment '" +
environment + "'. Please check your configuration.";
}
var _knex = undefined;
console.log("Using Knex config: ", config[environment])
try {
_knex = require('knex')(config[environment]);
} catch (e) {
throw [
"Failed to initialize Knex database connection with config: "
+ JSON.stringify(config[environment]),
e
];
}
return _knex;
}

@ -1,23 +0,0 @@
const environment = process.env.ENVIRONMENT || 'development'
import config from '../knexfile';
import Knex from 'knex';
if (!Object.keys(config).includes(environment)) {
throw "No Knex database configuration was found for environment '" +
environment + "'. Please check your configuration.";
}
var _knex = undefined;
try {
_knex = require('knex')(config[environment]);
} catch (e) {
throw [
"Failed to initialize Knex database connection with config: "
+ JSON.stringify(config[environment]),
e
];
}
const knex:Knex = _knex;
export default knex;

@ -12,6 +12,6 @@ export default <Record<string,any>> {
// In production, we base the config on an environment
// variable setting.
production: process.env.MUDBASE_DB_CONFIG
production: JSON.parse(process.env.MUDBASE_DB_CONFIG || "")
};

@ -1,10 +1,11 @@
const express = require('express');
import knex from './knex/knex';
import get_knex from './knex/get_knex';
import { SetupApp } from './app';
const app = express();
const knex = get_knex();
knex.migrate.latest().then(() => {
const port = process.env.PORT || 5000;
const apiBase = process.env.API || "";

Loading…
Cancel
Save