mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-28 00:17:12 +01:00
cleanup how vi configure database-stores. closes #139
This commit is contained in:
parent
5161e75b87
commit
2810742055
@ -4,9 +4,22 @@
|
|||||||
const COLUMNS = ['app_name', 'instance_id', 'client_ip', 'last_seen', 'created_at'];
|
const COLUMNS = ['app_name', 'instance_id', 'client_ip', 'last_seen', 'created_at'];
|
||||||
const TABLE = 'client_instances';
|
const TABLE = 'client_instances';
|
||||||
|
|
||||||
module.exports = function (db) {
|
const mapRow = (row) => ({
|
||||||
function updateRow (details) {
|
appName: row.app_name,
|
||||||
return db(TABLE)
|
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('app_name', details.appName)
|
||||||
.where('instance_id', details.instanceId)
|
.where('instance_id', details.instanceId)
|
||||||
.update({
|
.update({
|
||||||
@ -15,47 +28,36 @@ module.exports = function (db) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertNewRow (details) {
|
insertNewRow (details) {
|
||||||
return db(TABLE).insert({
|
return this.db(TABLE).insert({
|
||||||
app_name: details.appName,
|
app_name: details.appName,
|
||||||
instance_id: details.instanceId,
|
instance_id: details.instanceId,
|
||||||
client_ip: details.clientIp,
|
client_ip: details.clientIp,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function insert (details) {
|
insert (details) {
|
||||||
console.log(details);
|
return this.db(TABLE)
|
||||||
return db(TABLE)
|
|
||||||
.count('*')
|
.count('*')
|
||||||
.where('app_name', details.appName)
|
.where('app_name', details.appName)
|
||||||
.where('instance_id', details.instanceId)
|
.where('instance_id', details.instanceId)
|
||||||
.map(row => ({ count: row.count }))
|
.map(row => ({ count: row.count }))
|
||||||
.then(rows => {
|
.then(rows => {
|
||||||
if (rows[0].count > 0) {
|
if (rows[0].count > 0) {
|
||||||
return updateRow(details);
|
return this.updateRow(details);
|
||||||
} else {
|
} else {
|
||||||
return insertNewRow(details);
|
return this.insertNewRow(details);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAll () {
|
getAll () {
|
||||||
return db
|
return this.db
|
||||||
.select(COLUMNS)
|
.select(COLUMNS)
|
||||||
.from(TABLE)
|
.from(TABLE)
|
||||||
.orderBy('last_seen', 'desc')
|
.orderBy('last_seen', 'desc')
|
||||||
.map(mapRow);
|
.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;
|
@ -3,9 +3,18 @@
|
|||||||
const COLUMNS = ['app_name', 'strategies'];
|
const COLUMNS = ['app_name', 'strategies'];
|
||||||
const TABLE = 'client_strategies';
|
const TABLE = 'client_strategies';
|
||||||
|
|
||||||
module.exports = function (db) {
|
const mapRow = (row) => ({
|
||||||
function updateRow (appName, strategies) {
|
appName: row.app_name,
|
||||||
return db(TABLE)
|
strategies: row.strategies,
|
||||||
|
});
|
||||||
|
|
||||||
|
class ClientStrategyStore {
|
||||||
|
constructor (db) {
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateRow (appName, strategies) {
|
||||||
|
return this.db(TABLE)
|
||||||
.where('app_name', appName) // eslint-disable-line
|
.where('app_name', appName) // eslint-disable-line
|
||||||
.update({
|
.update({
|
||||||
strategies: JSON.stringify(strategies),
|
strategies: JSON.stringify(strategies),
|
||||||
@ -13,40 +22,33 @@ module.exports = function (db) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertNewRow (appName, strategies) {
|
insertNewRow (appName, strategies) {
|
||||||
return db(TABLE).insert({
|
return this.db(TABLE).insert({
|
||||||
app_name: appName, // eslint-disable-line
|
app_name: appName, // eslint-disable-line
|
||||||
strategies: JSON.stringify(strategies),
|
strategies: JSON.stringify(strategies),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function insert (appName, strategies) {
|
insert (appName, strategies) {
|
||||||
return db(TABLE)
|
return this.db(TABLE)
|
||||||
.count('*')
|
.count('*')
|
||||||
.where('app_name', appName)
|
.where('app_name', appName)
|
||||||
.map(row => ({ count: row.count }))
|
.map(row => ({ count: row.count }))
|
||||||
.then(rows => {
|
.then(rows => {
|
||||||
if (rows[0].count > 0) {
|
if (rows[0].count > 0) {
|
||||||
return updateRow(appName, strategies);
|
return this.updateRow(appName, strategies);
|
||||||
} else {
|
} else {
|
||||||
return insertNewRow(appName, strategies);
|
return this.insertNewRow(appName, strategies);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAll () {
|
getAll () {
|
||||||
return db
|
return this.db
|
||||||
.select(COLUMNS)
|
.select(COLUMNS)
|
||||||
.from(TABLE)
|
.from(TABLE)
|
||||||
.map(mapRow);
|
.map(mapRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
function mapRow (row) {
|
|
||||||
return {
|
|
||||||
appName: row.app_name,
|
|
||||||
strategies: row.strategies,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return { insert, getAll };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = ClientStrategyStore;
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const knex = require('knex');
|
const knex = require('knex');
|
||||||
|
|
||||||
module.exports = function (databaseConnection) {
|
module.exports.createDb = function (databaseConnection) {
|
||||||
return knex({
|
return knex({
|
||||||
client: 'pg',
|
client: 'pg',
|
||||||
connection: databaseConnection,
|
connection: databaseConnection,
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { createDb } = require('./db-pool');
|
||||||
const EventStore = require('./event-store');
|
const EventStore = require('./event-store');
|
||||||
const FeatureToggleStore = require('./feature-toggle-store');
|
const FeatureToggleStore = require('./feature-toggle-store');
|
||||||
const StrategyStore = require('./strategy-store');
|
const StrategyStore = require('./strategy-store');
|
||||||
const clientInstancesDbCreator = require('./client-instances');
|
const ClientInstanceStore = require('./client-instance-store');
|
||||||
const ClientMetricsStore = require('./client-metrics-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);
|
const eventStore = new EventStore(db);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
db,
|
||||||
eventStore,
|
eventStore,
|
||||||
featureToggleStore: new FeatureToggleStore(db, eventStore),
|
featureToggleStore: new FeatureToggleStore(db, eventStore),
|
||||||
strategyStore: new StrategyStore(db, eventStore),
|
strategyStore: new StrategyStore(db, eventStore),
|
||||||
clientInstancesDb: clientInstancesDbCreator(db),
|
clientInstanceStore: new ClientInstanceStore(db),
|
||||||
clientMetricsStore: new ClientMetricsStore(db),
|
clientMetricsStore: new ClientMetricsStore(db),
|
||||||
clientStrategiesDb: clientStrategiesDbCreator(db),
|
clientStrategyStore: new ClientStrategyStore(db),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -4,7 +4,7 @@ const eventDiffer = require('../event-differ');
|
|||||||
const version = 1;
|
const version = 1;
|
||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const eventStore = config.eventStore;
|
const { eventStore } = config.stores;
|
||||||
|
|
||||||
app.get('/events', (req, res) => {
|
app.get('/events', (req, res) => {
|
||||||
eventStore.getEvents().then(events => {
|
eventStore.getEvents().then(events => {
|
||||||
|
@ -6,8 +6,7 @@ const ValidationError = require('../error/validation-error');
|
|||||||
const validateRequest = require('../error/validate-request');
|
const validateRequest = require('../error/validate-request');
|
||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const featureToggleStore = config.featureToggleStore;
|
const { featureToggleStore, eventStore } = config.stores;
|
||||||
const eventStore = config.eventStore;
|
|
||||||
|
|
||||||
app.get('/archive/features', (req, res) => {
|
app.get('/archive/features', (req, res) => {
|
||||||
featureToggleStore.getArchivedFeatures().then(archivedFeatures => {
|
featureToggleStore.getArchivedFeatures().then(archivedFeatures => {
|
||||||
|
@ -13,8 +13,7 @@ const legacyFeatureMapper = require('../helper/legacy-feature-mapper');
|
|||||||
const version = 1;
|
const version = 1;
|
||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const featureToggleStore = config.featureToggleStore;
|
const { featureToggleStore, eventStore } = config.stores;
|
||||||
const eventStore = config.eventStore;
|
|
||||||
|
|
||||||
app.get('/features', (req, res) => {
|
app.get('/features', (req, res) => {
|
||||||
featureToggleStore.getFeatures()
|
featureToggleStore.getFeatures()
|
||||||
|
@ -4,7 +4,7 @@ const logger = require('../logger');
|
|||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
app.get('/health', (req, res) => {
|
app.get('/health', (req, res) => {
|
||||||
config.db.select(1)
|
config.stores.db.select(1)
|
||||||
.from('features')
|
.from('features')
|
||||||
.then(() => {
|
.then(() => {
|
||||||
res.json({ health: 'GOOD' });
|
res.json({ health: 'GOOD' });
|
||||||
|
@ -7,9 +7,9 @@ const ClientMetricsService = require('../client-metrics/service');
|
|||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const {
|
const {
|
||||||
clientMetricsStore,
|
clientMetricsStore,
|
||||||
clientStrategiesDb,
|
clientStrategyStore,
|
||||||
clientInstancesDb,
|
clientInstanceStore,
|
||||||
} = config;
|
} = config.stores;
|
||||||
const metrics = new ClientMetrics();
|
const metrics = new ClientMetrics();
|
||||||
const service = new ClientMetricsService(clientMetricsStore);
|
const service = new ClientMetricsService(clientMetricsStore);
|
||||||
|
|
||||||
@ -43,8 +43,8 @@ module.exports = function (app, config) {
|
|||||||
app.post('/client/register', (req, res) => {
|
app.post('/client/register', (req, res) => {
|
||||||
const data = req.body;
|
const data = req.body;
|
||||||
const clientIp = req.ip;
|
const clientIp = req.ip;
|
||||||
clientStrategiesDb.insert(data.appName, data.strategies)
|
clientStrategyStore.insert(data.appName, data.strategies)
|
||||||
.then(() => clientInstancesDb.insert({
|
.then(() => clientStrategyStore.insert({
|
||||||
appName: data.appName,
|
appName: data.appName,
|
||||||
instanceId: data.instanceId,
|
instanceId: data.instanceId,
|
||||||
clientIp,
|
clientIp,
|
||||||
@ -56,11 +56,11 @@ module.exports = function (app, config) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/client/strategies', (req, res) => {
|
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) => {
|
app.get('/client/instances', (req, res) => {
|
||||||
clientInstancesDb.getAll()
|
clientInstanceStore.getAll()
|
||||||
.then(data => res.json(data))
|
.then(data => res.json(data))
|
||||||
.catch(err => console.error(err));
|
.catch(err => console.error(err));
|
||||||
});
|
});
|
||||||
|
@ -11,8 +11,7 @@ const extractUser = require('../extract-user');
|
|||||||
const version = 1;
|
const version = 1;
|
||||||
|
|
||||||
module.exports = function (app, config) {
|
module.exports = function (app, config) {
|
||||||
const strategyStore = config.strategyStore;
|
const { strategyStore, eventStore } = config.stores;
|
||||||
const eventStore = config.eventStore;
|
|
||||||
|
|
||||||
app.get('/strategies', (req, res) => {
|
app.get('/strategies', (req, res) => {
|
||||||
strategyStore.getStrategies().then(strategies => {
|
strategyStore.getStrategies().then(strategies => {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
const logger = require('./lib/logger');
|
const logger = require('./lib/logger');
|
||||||
const migrator = require('./migrator');
|
const migrator = require('./migrator');
|
||||||
|
const { createStores } = require('./lib/db');
|
||||||
|
|
||||||
const DEFAULT_OPTIONS = {
|
const DEFAULT_OPTIONS = {
|
||||||
databaseUri: process.env.DATABASE_URL || 'postgres://unleash_user:passord@localhost:5432/unleash',
|
databaseUri: process.env.DATABASE_URL || 'postgres://unleash_user:passord@localhost:5432/unleash',
|
||||||
@ -10,29 +11,14 @@ const DEFAULT_OPTIONS = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function createApp (options) {
|
function createApp (options) {
|
||||||
const db = require('./lib/db/db-pool')(options.databaseUri);
|
|
||||||
|
|
||||||
// Database dependecies (statefull)
|
// Database dependecies (statefull)
|
||||||
const {
|
const stores = createStores(options);
|
||||||
eventStore,
|
|
||||||
featureToggleStore,
|
|
||||||
strategyStore,
|
|
||||||
clientInstancesDb,
|
|
||||||
clientMetricsStore,
|
|
||||||
clientStrategiesDb,
|
|
||||||
} = require('./lib/db')(db);
|
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
baseUriPath: options.baseUriPath,
|
baseUriPath: options.baseUriPath,
|
||||||
port: options.port,
|
port: options.port,
|
||||||
publicFolder: options.publicFolder,
|
publicFolder: options.publicFolder,
|
||||||
db,
|
stores,
|
||||||
eventStore,
|
|
||||||
featureToggleStore,
|
|
||||||
strategyStore,
|
|
||||||
clientMetricsStore,
|
|
||||||
clientStrategiesDb,
|
|
||||||
clientInstancesDb,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const app = require('./app')(config);
|
const app = require('./app')(config);
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function getDatabaseUri () {
|
function getDatabaseUri () {
|
||||||
if (!process.env.TEST_DATABASE_URL) {
|
if (process.env.TEST_DATABASE_URL) {
|
||||||
throw new Error('please set 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 = {
|
module.exports = {
|
||||||
|
@ -4,27 +4,18 @@ process.env.NODE_ENV = 'test';
|
|||||||
|
|
||||||
const BPromise = require('bluebird');
|
const BPromise = require('bluebird');
|
||||||
let request = require('supertest');
|
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')({
|
const app = require('../../app')({
|
||||||
baseUriPath: '',
|
baseUriPath: '',
|
||||||
db: knex,
|
stores,
|
||||||
eventStore,
|
|
||||||
featureToggleStore,
|
|
||||||
strategyStore,
|
|
||||||
clientStrategiesDb,
|
|
||||||
clientInstancesDb,
|
|
||||||
clientMetricsStore,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
BPromise.promisifyAll(request);
|
BPromise.promisifyAll(request);
|
||||||
@ -44,7 +35,7 @@ function createStrategies () {
|
|||||||
emails: 'String',
|
emails: 'String',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
], strategy => strategyStore._createStrategy(strategy));
|
], strategy => stores.strategyStore._createStrategy(strategy));
|
||||||
}
|
}
|
||||||
|
|
||||||
function createFeatures () {
|
function createFeatures () {
|
||||||
@ -108,15 +99,15 @@ function createFeatures () {
|
|||||||
},
|
},
|
||||||
}],
|
}],
|
||||||
},
|
},
|
||||||
], feature => featureToggleStore._createFeature(feature));
|
], feature => stores.featureToggleStore._createFeature(feature));
|
||||||
}
|
}
|
||||||
|
|
||||||
function destroyStrategies () {
|
function destroyStrategies () {
|
||||||
return knex('strategies').del();
|
return stores.db('strategies').del();
|
||||||
}
|
}
|
||||||
|
|
||||||
function destroyFeatures () {
|
function destroyFeatures () {
|
||||||
return knex('features').del();
|
return stores.db('features').del();
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetDatabase () {
|
function resetDatabase () {
|
||||||
|
Loading…
Reference in New Issue
Block a user