1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/unleash-server/lib/featureDb.js

99 lines
2.8 KiB
JavaScript
Raw Normal View History

var eventStore = require('./eventStore'),
eventType = require('./eventType'),
logger = require('./logger'),
Promise = require('bluebird'),
dbPool = require('./dbPool');
eventStore.on(eventType.featureCreated, function (event) {
var sql = 'INSERT INTO features(name, enabled, strategy_name, parameters) VALUES ($1, $2, $3, $4)';
var params = [
event.data.name,
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);
}
});
}
);
2014-10-23 16:00:51 +02:00
eventStore.on(eventType.featureUpdated, function (event) {
var sql, params;
var changeRequest = event.data;
switch (changeRequest.field) {
case 'enabled':
sql = 'UPDATE features SET enabled = $1 WHERE name=$2';
params = [event.data.value ? 1 : 0, event.data.name];
break;
case 'strategy':
sql = 'UPDATE features SET strategy_name = $1 WHERE name=$2';
params = [event.data.value, event.data.name];
break;
case 'parameters':
sql = 'UPDATE features SET parameters = $1 WHERE name=$2';
params = [event.data.value, event.data.name];
break;
default:
break;
}
if(sql && params) {
dbPool.query(sql, params, function(err) {
if(err) {
logger.error('Could not update feature, error was: ', err);
}
});
} else {
2014-10-23 16:02:12 +02:00
logger.error('Could not handle feature-update event', event);
2014-10-23 16:00:51 +02:00
}
}
);
function getFeatures() {
2014-10-23 13:51:37 +02:00
var sql = 'SELECT name, enabled, strategy_name as strategy, parameters FROM features ORDER BY created_at';
return new Promise(function (resolve, reject) {
dbPool.query(sql, function(err, res) {
if(err) {reject(err);}
resolve(res.rows.map(mapToToggle));
});
});
}
function getFeature(name) {
var sql = 'SELECT name, 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]));
} else {
reject();
}
});
});
}
2014-10-23 13:51:37 +02:00
function mapToToggle(row) {
return {
name: row.name,
enabled: row.enabled > 0,
strategy: row.strategy,
parameters: row.parameters
};
}
module.exports = {
getFeatures: getFeatures,
getFeature: getFeature
};