From 57655be667cd6828449123468b2738ff4bc2a3dd Mon Sep 17 00:00:00 2001 From: Martijn Vegter Date: Tue, 28 Apr 2020 21:45:24 +0200 Subject: [PATCH] feat: added Configuration --- lib/config/database.js | 48 ++++++++++++++++++++++++++++++++++++++++++ lib/config/index.js | 18 ++++++++++++++++ lib/database/index.js | 12 ++++------- 3 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 lib/config/database.js create mode 100644 lib/config/index.js diff --git a/lib/config/database.js b/lib/config/database.js new file mode 100644 index 0000000000..9fd5a95ef3 --- /dev/null +++ b/lib/config/database.js @@ -0,0 +1,48 @@ +/** + * @license + * Copyright CERN and copyright holders of ALICE O2. This software is + * distributed under the terms of the GNU General Public License v3 (GPL + * Version 3), copied verbatim in the file "COPYING". + * + * See http://alice-o2.web.cern.ch/license for full licensing information. + * + * In applying this license CERN does not waive the privileges and immunities + * granted to it by virtue of its status as an Intergovernmental Organization + * or submit itself to any jurisdiction. + */ + +// Default configuration +let dialect = 'mariadb'; +let host = 'localhost'; +let username = 'cern'; +let password = 'cern'; +let database = 'bookkeeping'; + +// Default environment specific override +if (process.env.NODE_ENV === 'test') { + host = 'database'; +} + +if (process.env.DATABASE_HOST) { + host = process.env.DATABASE_HOST; +} + +if (process.env.DATABASE_USERNAME) { + username = process.env.DATABASE_USERNAME; +} + +if (process.env.DATABASE_PASSWORD) { + password = process.env.DATABASE_PASSWORD; +} + +if (process.env.DATABASE_NAME) { + database = process.env.DATABASE_NAME; +} + +module.exports = { + dialect, + host, + username, + password, + database, +}; diff --git a/lib/config/index.js b/lib/config/index.js new file mode 100644 index 0000000000..6ff6bf5ba5 --- /dev/null +++ b/lib/config/index.js @@ -0,0 +1,18 @@ +/** + * @license + * Copyright CERN and copyright holders of ALICE O2. This software is + * distributed under the terms of the GNU General Public License v3 (GPL + * Version 3), copied verbatim in the file "COPYING". + * + * See http://alice-o2.web.cern.ch/license for full licensing information. + * + * In applying this license CERN does not waive the privileges and immunities + * granted to it by virtue of its status as an Intergovernmental Organization + * or submit itself to any jurisdiction. + */ + +const database = require('./database'); + +module.exports = { + database, +}; diff --git a/lib/database/index.js b/lib/database/index.js index b68d069054..b9b1561513 100644 --- a/lib/database/index.js +++ b/lib/database/index.js @@ -15,6 +15,7 @@ const path = require('path'); const { Sequelize } = require('sequelize'); const Umzug = require('umzug'); const { Database } = require('../application/interfaces'); +const { database: Config } = require('../config'); /** * Sequelize implementation of the Database interface. @@ -26,14 +27,9 @@ class SequelizeDatabase extends Database { constructor() { super(); - let host = process.env.DATABASE_HOST || 'localhost'; - if (process.env.NODE_ENV === 'test') { - host = 'database'; - } - - this.sequelize = new Sequelize('bookkeeping', 'cern', 'cern', { - host, - dialect: 'mariadb', + this.sequelize = new Sequelize(Config.database, Config.username, Config.password, { + host: Config.host, + dialect: Config.dialect, }); }