diff --git a/packages/unleash-api/lib/db/client-instances.js b/packages/unleash-api/lib/db/client-instance-store.js similarity index 59% rename from packages/unleash-api/lib/db/client-instances.js rename to packages/unleash-api/lib/db/client-instance-store.js index 5e050a8f37..4a0db876ba 100644 --- a/packages/unleash-api/lib/db/client-instances.js +++ b/packages/unleash-api/lib/db/client-instance-store.js @@ -4,9 +4,22 @@ const COLUMNS = ['app_name', 'instance_id', 'client_ip', 'last_seen', 'created_at']; const TABLE = 'client_instances'; -module.exports = function (db) { - function updateRow (details) { - return db(TABLE) +const mapRow = (row) => ({ + appName: row.app_name, + instanceId: row.instance_id, + clientIp: row.client_ip, + lastSeen: row.last_seen, + createdAt: row.created_at, +}); + +class ClientInstanceStore { + + constructor (db) { + this.db = db; + } + + updateRow (details) { + return this.db(TABLE) .where('app_name', details.appName) .where('instance_id', details.instanceId) .update({ @@ -15,47 +28,36 @@ module.exports = function (db) { }); } - function insertNewRow (details) { - return db(TABLE).insert({ + insertNewRow (details) { + return this.db(TABLE).insert({ app_name: details.appName, instance_id: details.instanceId, client_ip: details.clientIp, }); } - function insert (details) { - console.log(details); - return db(TABLE) + insert (details) { + return this.db(TABLE) .count('*') .where('app_name', details.appName) .where('instance_id', details.instanceId) .map(row => ({ count: row.count })) .then(rows => { if (rows[0].count > 0) { - return updateRow(details); + return this.updateRow(details); } else { - return insertNewRow(details); + return this.insertNewRow(details); } }); } - function getAll () { - return db + getAll () { + return this.db .select(COLUMNS) .from(TABLE) .orderBy('last_seen', 'desc') .map(mapRow); } - - function mapRow (row) { - return { - appName: row.app_name, - instanceId: row.instance_id, - clientIp: row.client_ip, - lastSeen: row.last_seen, - createdAt: row.created_at, - }; - } - - return { insert, getAll }; }; + +module.exports = ClientInstanceStore; diff --git a/packages/unleash-api/lib/db/client-strategies.js b/packages/unleash-api/lib/db/client-strategy-store.js similarity index 57% rename from packages/unleash-api/lib/db/client-strategies.js rename to packages/unleash-api/lib/db/client-strategy-store.js index 0543c942d8..0be6e0683e 100644 --- a/packages/unleash-api/lib/db/client-strategies.js +++ b/packages/unleash-api/lib/db/client-strategy-store.js @@ -3,9 +3,18 @@ const COLUMNS = ['app_name', 'strategies']; const TABLE = 'client_strategies'; -module.exports = function (db) { - function updateRow (appName, strategies) { - return db(TABLE) +const mapRow = (row) => ({ + appName: row.app_name, + strategies: row.strategies, +}); + +class ClientStrategyStore { + constructor (db) { + this.db = db; + } + + updateRow (appName, strategies) { + return this.db(TABLE) .where('app_name', appName) // eslint-disable-line .update({ strategies: JSON.stringify(strategies), @@ -13,40 +22,33 @@ module.exports = function (db) { }); } - function insertNewRow (appName, strategies) { - return db(TABLE).insert({ + insertNewRow (appName, strategies) { + return this.db(TABLE).insert({ app_name: appName, // eslint-disable-line strategies: JSON.stringify(strategies), }); } - function insert (appName, strategies) { - return db(TABLE) + insert (appName, strategies) { + return this.db(TABLE) .count('*') .where('app_name', appName) .map(row => ({ count: row.count })) .then(rows => { if (rows[0].count > 0) { - return updateRow(appName, strategies); + return this.updateRow(appName, strategies); } else { - return insertNewRow(appName, strategies); + return this.insertNewRow(appName, strategies); } }); } - function getAll () { - return db + getAll () { + return this.db .select(COLUMNS) .from(TABLE) .map(mapRow); } - - function mapRow (row) { - return { - appName: row.app_name, - strategies: row.strategies, - }; - } - - return { insert, getAll }; }; + +module.exports = ClientStrategyStore; diff --git a/packages/unleash-api/lib/db/db-pool.js b/packages/unleash-api/lib/db/db-pool.js index 2d09b58ed2..ab6b734ce6 100644 --- a/packages/unleash-api/lib/db/db-pool.js +++ b/packages/unleash-api/lib/db/db-pool.js @@ -2,7 +2,7 @@ const knex = require('knex'); -module.exports = function (databaseConnection) { +module.exports.createDb = function (databaseConnection) { return knex({ client: 'pg', connection: databaseConnection, diff --git a/packages/unleash-api/lib/db/index.js b/packages/unleash-api/lib/db/index.js index 9b97acdf9c..8cbc185cef 100644 --- a/packages/unleash-api/lib/db/index.js +++ b/packages/unleash-api/lib/db/index.js @@ -1,21 +1,24 @@ 'use strict'; +const { createDb } = require('./db-pool'); const EventStore = require('./event-store'); const FeatureToggleStore = require('./feature-toggle-store'); const StrategyStore = require('./strategy-store'); -const clientInstancesDbCreator = require('./client-instances'); +const ClientInstanceStore = require('./client-instance-store'); const ClientMetricsStore = require('./client-metrics-store'); -const clientStrategiesDbCreator = require('./client-strategies'); +const ClientStrategyStore = require('./client-strategy-store'); -module.exports = (db) => { +module.exports.createStores = (config) => { + const db = createDb(config.databaseUri); const eventStore = new EventStore(db); return { + db, eventStore, featureToggleStore: new FeatureToggleStore(db, eventStore), strategyStore: new StrategyStore(db, eventStore), - clientInstancesDb: clientInstancesDbCreator(db), + clientInstanceStore: new ClientInstanceStore(db), clientMetricsStore: new ClientMetricsStore(db), - clientStrategiesDb: clientStrategiesDbCreator(db), + clientStrategyStore: new ClientStrategyStore(db), }; }; diff --git a/packages/unleash-api/lib/routes/event.js b/packages/unleash-api/lib/routes/event.js index 81bef95cd8..ea6bcc4b16 100644 --- a/packages/unleash-api/lib/routes/event.js +++ b/packages/unleash-api/lib/routes/event.js @@ -4,7 +4,7 @@ const eventDiffer = require('../event-differ'); const version = 1; module.exports = function (app, config) { - const eventStore = config.eventStore; + const { eventStore } = config.stores; app.get('/events', (req, res) => { eventStore.getEvents().then(events => { diff --git a/packages/unleash-api/lib/routes/feature-archive.js b/packages/unleash-api/lib/routes/feature-archive.js index a91a099686..8c037b3986 100644 --- a/packages/unleash-api/lib/routes/feature-archive.js +++ b/packages/unleash-api/lib/routes/feature-archive.js @@ -6,8 +6,7 @@ const ValidationError = require('../error/validation-error'); const validateRequest = require('../error/validate-request'); module.exports = function (app, config) { - const featureToggleStore = config.featureToggleStore; - const eventStore = config.eventStore; + const { featureToggleStore, eventStore } = config.stores; app.get('/archive/features', (req, res) => { featureToggleStore.getArchivedFeatures().then(archivedFeatures => { diff --git a/packages/unleash-api/lib/routes/feature.js b/packages/unleash-api/lib/routes/feature.js index 4706a88718..819b141b6d 100644 --- a/packages/unleash-api/lib/routes/feature.js +++ b/packages/unleash-api/lib/routes/feature.js @@ -13,8 +13,7 @@ const legacyFeatureMapper = require('../helper/legacy-feature-mapper'); const version = 1; module.exports = function (app, config) { - const featureToggleStore = config.featureToggleStore; - const eventStore = config.eventStore; + const { featureToggleStore, eventStore } = config.stores; app.get('/features', (req, res) => { featureToggleStore.getFeatures() diff --git a/packages/unleash-api/lib/routes/health-check.js b/packages/unleash-api/lib/routes/health-check.js index fbf984efe8..a21fa6312d 100644 --- a/packages/unleash-api/lib/routes/health-check.js +++ b/packages/unleash-api/lib/routes/health-check.js @@ -4,7 +4,7 @@ const logger = require('../logger'); module.exports = function (app, config) { app.get('/health', (req, res) => { - config.db.select(1) + config.stores.db.select(1) .from('features') .then(() => { res.json({ health: 'GOOD' }); diff --git a/packages/unleash-api/lib/routes/metrics.js b/packages/unleash-api/lib/routes/metrics.js index f480ee49f3..8119c44458 100644 --- a/packages/unleash-api/lib/routes/metrics.js +++ b/packages/unleash-api/lib/routes/metrics.js @@ -7,9 +7,9 @@ const ClientMetricsService = require('../client-metrics/service'); module.exports = function (app, config) { const { clientMetricsStore, - clientStrategiesDb, - clientInstancesDb, - } = config; + clientStrategyStore, + clientInstanceStore, + } = config.stores; const metrics = new ClientMetrics(); const service = new ClientMetricsService(clientMetricsStore); @@ -43,8 +43,8 @@ module.exports = function (app, config) { app.post('/client/register', (req, res) => { const data = req.body; const clientIp = req.ip; - clientStrategiesDb.insert(data.appName, data.strategies) - .then(() => clientInstancesDb.insert({ + clientStrategyStore.insert(data.appName, data.strategies) + .then(() => clientStrategyStore.insert({ appName: data.appName, instanceId: data.instanceId, clientIp, @@ -56,11 +56,11 @@ module.exports = function (app, config) { }); app.get('/client/strategies', (req, res) => { - clientStrategiesDb.getAll().then(data => res.json(data)); + clientStrategyStore.getAll().then(data => res.json(data)); }); app.get('/client/instances', (req, res) => { - clientInstancesDb.getAll() + clientInstanceStore.getAll() .then(data => res.json(data)) .catch(err => console.error(err)); }); diff --git a/packages/unleash-api/lib/routes/strategy.js b/packages/unleash-api/lib/routes/strategy.js index c0320c863c..8719c08571 100644 --- a/packages/unleash-api/lib/routes/strategy.js +++ b/packages/unleash-api/lib/routes/strategy.js @@ -11,8 +11,7 @@ const extractUser = require('../extract-user'); const version = 1; module.exports = function (app, config) { - const strategyStore = config.strategyStore; - const eventStore = config.eventStore; + const { strategyStore, eventStore } = config.stores; app.get('/strategies', (req, res) => { strategyStore.getStrategies().then(strategies => { diff --git a/packages/unleash-api/server-impl.js b/packages/unleash-api/server-impl.js index 95ee9e484c..4d723ab668 100644 --- a/packages/unleash-api/server-impl.js +++ b/packages/unleash-api/server-impl.js @@ -2,6 +2,7 @@ const logger = require('./lib/logger'); const migrator = require('./migrator'); +const { createStores } = require('./lib/db'); const DEFAULT_OPTIONS = { databaseUri: process.env.DATABASE_URL || 'postgres://unleash_user:passord@localhost:5432/unleash', @@ -10,29 +11,14 @@ const DEFAULT_OPTIONS = { }; function createApp (options) { - const db = require('./lib/db/db-pool')(options.databaseUri); - // Database dependecies (statefull) - const { - eventStore, - featureToggleStore, - strategyStore, - clientInstancesDb, - clientMetricsStore, - clientStrategiesDb, - } = require('./lib/db')(db); + const stores = createStores(options); const config = { baseUriPath: options.baseUriPath, port: options.port, publicFolder: options.publicFolder, - db, - eventStore, - featureToggleStore, - strategyStore, - clientMetricsStore, - clientStrategiesDb, - clientInstancesDb, + stores, }; const app = require('./app')(config); diff --git a/packages/unleash-api/test/e2e/database-config.js b/packages/unleash-api/test/e2e/database-config.js index 6aaf81cee5..0dcc7fc69b 100644 --- a/packages/unleash-api/test/e2e/database-config.js +++ b/packages/unleash-api/test/e2e/database-config.js @@ -1,11 +1,12 @@ 'use strict'; function getDatabaseUri () { - if (!process.env.TEST_DATABASE_URL) { - throw new Error('please set TEST_DATABASE_URL'); + if (process.env.TEST_DATABASE_URL) { + return process.env.TEST_DATABASE_URL; + } else { + console.log('Using default unleash_test database'); + return 'postgres://unleash_user:passord@localhost:5432/unleash_test'; } - - return process.env.TEST_DATABASE_URL; } module.exports = { diff --git a/packages/unleash-api/test/e2e/test-helper.js b/packages/unleash-api/test/e2e/test-helper.js index f85efcaad5..eec6f82239 100644 --- a/packages/unleash-api/test/e2e/test-helper.js +++ b/packages/unleash-api/test/e2e/test-helper.js @@ -4,27 +4,18 @@ process.env.NODE_ENV = 'test'; const BPromise = require('bluebird'); let request = require('supertest'); -const databaseUri = require('./database-config').getDatabaseUri(); -const knex = require('../../lib/db/db-pool')(databaseUri); -const { - eventStore, - featureToggleStore, - strategyStore, - clientInstancesDb, - clientStrategiesDb, - clientMetricsStore, -} = require('../../lib/db')(knex); +const options = { + databaseUri: require('./database-config').getDatabaseUri(), +}; + +const { createStores } = require('../../lib/db'); + +const stores = createStores(options); const app = require('../../app')({ baseUriPath: '', - db: knex, - eventStore, - featureToggleStore, - strategyStore, - clientStrategiesDb, - clientInstancesDb, - clientMetricsStore, + stores, }); BPromise.promisifyAll(request); @@ -44,7 +35,7 @@ function createStrategies () { emails: 'String', }, }, - ], strategy => strategyStore._createStrategy(strategy)); + ], strategy => stores.strategyStore._createStrategy(strategy)); } function createFeatures () { @@ -108,15 +99,15 @@ function createFeatures () { }, }], }, - ], feature => featureToggleStore._createFeature(feature)); + ], feature => stores.featureToggleStore._createFeature(feature)); } function destroyStrategies () { - return knex('strategies').del(); + return stores.db('strategies').del(); } function destroyFeatures () { - return knex('features').del(); + return stores.db('features').del(); } function resetDatabase () {