1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-11 00:08:30 +01:00

cleanup how vi configure database-stores. closes #139

This commit is contained in:
ivaosthu 2016-11-05 14:08:47 +01:00 committed by Ivar Conradi Østhus
parent 83a443dbeb
commit a5b37f0007
13 changed files with 89 additions and 107 deletions

View File

@ -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;

View File

@ -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;

View File

@ -2,7 +2,7 @@
const knex = require('knex');
module.exports = function (databaseConnection) {
module.exports.createDb = function (databaseConnection) {
return knex({
client: 'pg',
connection: databaseConnection,

View File

@ -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),
};
};

View File

@ -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 => {

View File

@ -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 => {

View File

@ -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()

View File

@ -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' });

View File

@ -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));
});

View File

@ -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 => {

View File

@ -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);

View File

@ -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 = {

View File

@ -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 () {