diff --git a/lib/dbPool.js b/lib/dbPool.js index f5079dc7be..f568272327 100644 --- a/lib/dbPool.js +++ b/lib/dbPool.js @@ -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(); \ No newline at end of file diff --git a/lib/eventDb.js b/lib/eventDb.js index d0651c49a6..8a1661b2b8 100644 --- a/lib/eventDb.js +++ b/lib/eventDb.js @@ -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 }; } diff --git a/lib/featureDb.js b/lib/featureDb.js index 757cf40924..18d29b0f6f 100644 --- a/lib/featureDb.js +++ b/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 diff --git a/package.json b/package.json index 6b96492e6d..66efbe18e2 100644 --- a/package.json +++ b/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",