1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00

Use knex for DB access. Closes #46.

This commit is contained in:
Jari Bakken 2014-11-14 15:06:53 +01:00
parent 981c383bb5
commit 3d52d97f85
4 changed files with 89 additions and 104 deletions

View File

@ -1,23 +1,32 @@
var anyDB = require('any-db'), var logger = require('./logger');
logger = require('./logger'), var nconf = require('nconf');
nconf = require('nconf'), var fs = require('fs');
fs = require('fs'), var ini = require('ini');
ini = require('ini'); var knex = require('knex');
function getDatabaseUrl() {
function createDbPool() {
if (process.env.DATABASE_URL) { if (process.env.DATABASE_URL) {
logger.info('unleash started with 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) { } else if (nconf.argv().get('databaseini') !== undefined) {
// Finn specific way of delivering env variables // Finn specific way of delivering env variables
var databaseini = nconf.argv().get('databaseini'); var databaseini = nconf.argv().get('databaseini');
logger.info('unleash started with databaseini: ' + databaseini); logger.info('unleash started with databaseini: ' + databaseini);
var config = ini.parse(fs.readFileSync(databaseini, 'utf-8')); 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'); 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(); module.exports = createDbPool();

View File

@ -1,45 +1,40 @@
var Promise = require('bluebird'), var knex = require('./dbPool');
dbPool = require('./dbPool'); var EVENT_COLUMNS = ['id', 'type', 'created_by', 'data'];
function storeEvent(event) { function storeEvent(event) {
var sql = 'INSERT INTO events(type, created_by, data) VALUES ($1, $2, $3)'; return knex('events').insert({
var params = [event.type, event.createdBy, event.data]; type: event.type,
created_by: event.createdBy, // jshint ignore:line
return new Promise(function (resolve, reject) { data: event.data
dbPool.query(sql, params, function (err) {
if (err) {reject(err);}
resolve();
});
}); });
} }
function getEvents() { function getEvents() {
var sql = 'SELECT id, type, created_by as created, data FROM events ORDER BY created_at DESC'; return knex
return new Promise(function (resolve, reject) { .select(EVENT_COLUMNS)
dbPool.query(sql, function(err, res) { .from('events')
if(err) {reject(err);} .orderBy('created_at', 'desc')
resolve(res.rows.map(mapToEvent)); .then(function (rows) {
return rows.map(rowToEvent);
}); });
});
} }
function getEventsFilterByName(name) { function getEventsFilterByName(name) {
var sql = 'SELECT id, type, created_by as created, data ' + return knex
'FROM events WHERE data ->> \'name\' = $1 ' + .select(EVENT_COLUMNS)
'ORDER BY created_at DESC'; .from('events')
return new Promise(function (resolve, reject) { .whereRaw("data ->> 'name' = ?", [name])
dbPool.query(sql, [name], function(err, res) { .orderBy('created_at', 'desc')
if(err) {reject(err);} .then(function (rows) {
resolve(res.rows.map(mapToEvent)); return rows.map(rowToEvent);
}); });
});
} }
function mapToEvent(row) { function rowToEvent(row) {
return { return {
id: row.id, id: row.id,
type: row.type, type: row.type,
createdBy: row.created, createdBy: row.created_by, // jshint ignore:line
data: row.data data: row.data
}; };
} }

View File

@ -1,89 +1,71 @@
var eventStore = require('./eventStore'), var eventStore = require('./eventStore');
eventType = require('./eventType'), var eventType = require('./eventType');
logger = require('./logger'), var logger = require('./logger');
Promise = require('bluebird'), var knex = require('./dbPool');
dbPool = require('./dbPool'); var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters'];
eventStore.on(eventType.featureCreated, function (event) { eventStore.on(eventType.featureCreated, function (event) {
var sql = 'INSERT INTO features(name, description, enabled, strategy_name, parameters) ' + knex('features')
'VALUES ($1, $2, $3, $4, $5)'; .insert(eventToRow(event))
var params = [ .catch(function (err) {
event.data.name, logger.error('Could not insert feature, error was: ', err);
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);
}
}); });
} });
);
eventStore.on(eventType.featureUpdated, function (event) { eventStore.on(eventType.featureUpdated, function (event) {
var feature = event.data; knex('features')
.where({name: event.data.name})
var sql = 'UPDATE features SET enabled = $1, strategy_name = $2, ' + .update(eventToRow(event))
'description = $3, parameters = $4 WHERE name = $5'; .catch(function (err) {
logger.error('Could not update feature, error was: ', err);
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);
}
});
}
);
function getFeatures() { function getFeatures() {
var sql = 'SELECT name, description, enabled, strategy_name as strategy, parameters ' + return knex
'FROM features ORDER BY created_at DESC'; .select(FEATURE_COLUMNS)
return new Promise(function (resolve, reject) { .from('features')
dbPool.query(sql, function(err, res) { .orderBy('created_at', 'desc')
if(err) {reject(err);} .then(function (rows) {
resolve(res.rows.map(mapToToggle)); return rows.map(rowToFeature);
}); });
});
} }
function getFeature(name) { function getFeature(name) {
var sql = 'SELECT name, description, enabled, strategy_name as strategy, parameters ' + return knex
'FROM features WHERE name=$1'; .select(FEATURE_COLUMNS)
.from('features')
return new Promise(function (resolve, reject) { .where({name: name})
dbPool.query(sql, [name], function(err, res) { .limit(1)
if(err) {reject(err);} .then(function (rows) {
if (rows.length) {
if(res.rows.length === 1) { return rowToFeature(rows[0]);
resolve(mapToToggle(res.rows[0]));
} else { } else {
reject(); throw new Error('could not find feature named: ' + name);
} }
}); });
});
} }
function mapToToggle(row) { function rowToFeature(row) {
return { return {
name: row.name, name: row.name,
description: row.description, description: row.description,
enabled: row.enabled > 0, enabled: row.enabled > 0,
strategy: row.strategy, strategy: row.strategy_name, // jshint ignore: line
parameters: row.parameters 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 = { module.exports = {
getFeatures: getFeatures, getFeatures: getFeatures,
getFeature: getFeature getFeature: getFeature

View File

@ -28,8 +28,6 @@
"postinstall": "npm run build" "postinstall": "npm run build"
}, },
"dependencies": { "dependencies": {
"any-db": "2.1.0",
"any-db-postgres": "2.1.3",
"bluebird": "2.2.2", "bluebird": "2.2.2",
"body-parser": "1.4.3", "body-parser": "1.4.3",
"db-migrate": "0.7.1", "db-migrate": "0.7.1",
@ -37,15 +35,16 @@
"express": "4.9.8", "express": "4.9.8",
"express-validator": "2.6.0", "express-validator": "2.6.0",
"ini": "1.3.0", "ini": "1.3.0",
"jsx-loader": "^0.12.0",
"jsxhint": "0.4.15",
"knex": "^0.7.3",
"log4js": "0.6.21", "log4js": "0.6.21",
"nconf": "0.6.9", "nconf": "0.6.9",
"jsxhint": "0.4.15", "pg": "3.6.1",
"jsx-loader": "^0.12.0",
"react": "^0.12.0", "react": "^0.12.0",
"reqwest": "^1.1.4", "reqwest": "^1.1.4",
"webpack": "^1.4.9", "webpack": "^1.4.9",
"webpack-dev-middleware": "^1.0.11", "webpack-dev-middleware": "^1.0.11"
"pg": "3.6.1"
}, },
"devDependencies": { "devDependencies": {
"chai": "1.9.1", "chai": "1.9.1",