mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-22 19:07:54 +01:00
Use knex for DB access. Closes #46.
This commit is contained in:
parent
981c383bb5
commit
3d52d97f85
@ -1,23 +1,32 @@
|
||||
var anyDB = require('any-db'),
|
||||
logger = require('./logger'),
|
||||
nconf = require('nconf'),
|
||||
fs = require('fs'),
|
||||
ini = require('ini');
|
||||
var logger = require('./logger');
|
||||
var nconf = require('nconf');
|
||||
var fs = require('fs');
|
||||
var ini = require('ini');
|
||||
var knex = require('knex');
|
||||
|
||||
|
||||
function createDbPool() {
|
||||
function getDatabaseUrl() {
|
||||
if (process.env.DATABASE_URL) {
|
||||
logger.info('unleash started with DATABASE_URL');
|
||||
return anyDB.createPool(process.env.DATABASE_URL);
|
||||
|
||||
return process.env.DATABASE_URL;
|
||||
} else if (nconf.argv().get('databaseini') !== undefined) {
|
||||
// Finn specific way of delivering env variables
|
||||
var databaseini = nconf.argv().get('databaseini');
|
||||
logger.info('unleash started with databaseini: ' + databaseini);
|
||||
var config = ini.parse(fs.readFileSync(databaseini, 'utf-8'));
|
||||
return anyDB.createPool(config.DATABASE_URL, {min: 2, max: 20});
|
||||
|
||||
return config.DATABASE_URL;
|
||||
}
|
||||
|
||||
throw new Error('please set DATABASE_URL or pass --databaseini');
|
||||
}
|
||||
|
||||
function createDbPool() {
|
||||
return knex({
|
||||
client: 'pg',
|
||||
connection: getDatabaseUrl(),
|
||||
pool: {min: 2, max: 20}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = createDbPool();
|
@ -1,45 +1,40 @@
|
||||
var Promise = require('bluebird'),
|
||||
dbPool = require('./dbPool');
|
||||
var knex = require('./dbPool');
|
||||
var EVENT_COLUMNS = ['id', 'type', 'created_by', 'data'];
|
||||
|
||||
function storeEvent(event) {
|
||||
var sql = 'INSERT INTO events(type, created_by, data) VALUES ($1, $2, $3)';
|
||||
var params = [event.type, event.createdBy, event.data];
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
dbPool.query(sql, params, function (err) {
|
||||
if (err) {reject(err);}
|
||||
resolve();
|
||||
});
|
||||
return knex('events').insert({
|
||||
type: event.type,
|
||||
created_by: event.createdBy, // jshint ignore:line
|
||||
data: event.data
|
||||
});
|
||||
}
|
||||
|
||||
function getEvents() {
|
||||
var sql = 'SELECT id, type, created_by as created, data FROM events ORDER BY created_at DESC';
|
||||
return new Promise(function (resolve, reject) {
|
||||
dbPool.query(sql, function(err, res) {
|
||||
if(err) {reject(err);}
|
||||
resolve(res.rows.map(mapToEvent));
|
||||
return knex
|
||||
.select(EVENT_COLUMNS)
|
||||
.from('events')
|
||||
.orderBy('created_at', 'desc')
|
||||
.then(function (rows) {
|
||||
return rows.map(rowToEvent);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getEventsFilterByName(name) {
|
||||
var sql = 'SELECT id, type, created_by as created, data ' +
|
||||
'FROM events WHERE data ->> \'name\' = $1 ' +
|
||||
'ORDER BY created_at DESC';
|
||||
return new Promise(function (resolve, reject) {
|
||||
dbPool.query(sql, [name], function(err, res) {
|
||||
if(err) {reject(err);}
|
||||
resolve(res.rows.map(mapToEvent));
|
||||
});
|
||||
});
|
||||
return knex
|
||||
.select(EVENT_COLUMNS)
|
||||
.from('events')
|
||||
.whereRaw("data ->> 'name' = ?", [name])
|
||||
.orderBy('created_at', 'desc')
|
||||
.then(function (rows) {
|
||||
return rows.map(rowToEvent);
|
||||
});
|
||||
}
|
||||
|
||||
function mapToEvent(row) {
|
||||
function rowToEvent(row) {
|
||||
return {
|
||||
id: row.id,
|
||||
type: row.type,
|
||||
createdBy: row.created,
|
||||
createdBy: row.created_by, // jshint ignore:line
|
||||
data: row.data
|
||||
};
|
||||
}
|
||||
|
106
lib/featureDb.js
106
lib/featureDb.js
@ -1,89 +1,71 @@
|
||||
var eventStore = require('./eventStore'),
|
||||
eventType = require('./eventType'),
|
||||
logger = require('./logger'),
|
||||
Promise = require('bluebird'),
|
||||
dbPool = require('./dbPool');
|
||||
var eventStore = require('./eventStore');
|
||||
var eventType = require('./eventType');
|
||||
var logger = require('./logger');
|
||||
var knex = require('./dbPool');
|
||||
var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters'];
|
||||
|
||||
eventStore.on(eventType.featureCreated, function (event) {
|
||||
var sql = 'INSERT INTO features(name, description, enabled, strategy_name, parameters) ' +
|
||||
'VALUES ($1, $2, $3, $4, $5)';
|
||||
var params = [
|
||||
event.data.name,
|
||||
event.data.description,
|
||||
event.data.enabled ? 1 : 0,
|
||||
event.data.strategy,
|
||||
event.data.parameters
|
||||
];
|
||||
|
||||
dbPool.query(sql, params, function(err) {
|
||||
if(err) {
|
||||
logger.error('Could not insert feature, error was: ', err);
|
||||
}
|
||||
knex('features')
|
||||
.insert(eventToRow(event))
|
||||
.catch(function (err) {
|
||||
logger.error('Could not insert feature, error was: ', err);
|
||||
});
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
eventStore.on(eventType.featureUpdated, function (event) {
|
||||
var feature = event.data;
|
||||
|
||||
var sql = 'UPDATE features SET enabled = $1, strategy_name = $2, ' +
|
||||
'description = $3, parameters = $4 WHERE name = $5';
|
||||
|
||||
var params = [
|
||||
feature.enabled ? 1 : 0,
|
||||
feature.strategy,
|
||||
feature.description,
|
||||
feature.paramaters,
|
||||
feature.name
|
||||
];
|
||||
|
||||
dbPool.query(sql, params, function(err) {
|
||||
if(err) {
|
||||
logger.error('Could not update feature, error was: ', err);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
);
|
||||
knex('features')
|
||||
.where({name: event.data.name})
|
||||
.update(eventToRow(event))
|
||||
.catch(function (err) {
|
||||
logger.error('Could not update feature, error was: ', err);
|
||||
});
|
||||
});
|
||||
|
||||
function getFeatures() {
|
||||
var sql = 'SELECT name, description, enabled, strategy_name as strategy, parameters ' +
|
||||
'FROM features ORDER BY created_at DESC';
|
||||
return new Promise(function (resolve, reject) {
|
||||
dbPool.query(sql, function(err, res) {
|
||||
if(err) {reject(err);}
|
||||
resolve(res.rows.map(mapToToggle));
|
||||
return knex
|
||||
.select(FEATURE_COLUMNS)
|
||||
.from('features')
|
||||
.orderBy('created_at', 'desc')
|
||||
.then(function (rows) {
|
||||
return rows.map(rowToFeature);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getFeature(name) {
|
||||
var sql = 'SELECT name, description, enabled, strategy_name as strategy, parameters ' +
|
||||
'FROM features WHERE name=$1';
|
||||
|
||||
return new Promise(function (resolve, reject) {
|
||||
dbPool.query(sql, [name], function(err, res) {
|
||||
if(err) {reject(err);}
|
||||
|
||||
if(res.rows.length === 1) {
|
||||
resolve(mapToToggle(res.rows[0]));
|
||||
return knex
|
||||
.select(FEATURE_COLUMNS)
|
||||
.from('features')
|
||||
.where({name: name})
|
||||
.limit(1)
|
||||
.then(function (rows) {
|
||||
if (rows.length) {
|
||||
return rowToFeature(rows[0]);
|
||||
} else {
|
||||
reject();
|
||||
throw new Error('could not find feature named: ' + name);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function mapToToggle(row) {
|
||||
function rowToFeature(row) {
|
||||
return {
|
||||
name: row.name,
|
||||
description: row.description,
|
||||
enabled: row.enabled > 0,
|
||||
strategy: row.strategy,
|
||||
strategy: row.strategy_name, // jshint ignore: line
|
||||
parameters: row.parameters
|
||||
};
|
||||
}
|
||||
|
||||
function eventToRow(event) {
|
||||
return {
|
||||
name: event.data.name,
|
||||
description: event.data.description,
|
||||
enabled: event.data.enabled ? 1 : 0,
|
||||
strategy_name: event.data.strategy, // jshint ignore: line
|
||||
parameters: event.data.parameters
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFeatures: getFeatures,
|
||||
getFeature: getFeature
|
||||
|
11
package.json
11
package.json
@ -28,8 +28,6 @@
|
||||
"postinstall": "npm run build"
|
||||
},
|
||||
"dependencies": {
|
||||
"any-db": "2.1.0",
|
||||
"any-db-postgres": "2.1.3",
|
||||
"bluebird": "2.2.2",
|
||||
"body-parser": "1.4.3",
|
||||
"db-migrate": "0.7.1",
|
||||
@ -37,15 +35,16 @@
|
||||
"express": "4.9.8",
|
||||
"express-validator": "2.6.0",
|
||||
"ini": "1.3.0",
|
||||
"jsx-loader": "^0.12.0",
|
||||
"jsxhint": "0.4.15",
|
||||
"knex": "^0.7.3",
|
||||
"log4js": "0.6.21",
|
||||
"nconf": "0.6.9",
|
||||
"jsxhint": "0.4.15",
|
||||
"jsx-loader": "^0.12.0",
|
||||
"pg": "3.6.1",
|
||||
"react": "^0.12.0",
|
||||
"reqwest": "^1.1.4",
|
||||
"webpack": "^1.4.9",
|
||||
"webpack-dev-middleware": "^1.0.11",
|
||||
"pg": "3.6.1"
|
||||
"webpack-dev-middleware": "^1.0.11"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "1.9.1",
|
||||
|
Loading…
Reference in New Issue
Block a user