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

80 lines
2.0 KiB
JavaScript
Raw Normal View History

2014-11-14 15:06:53 +01:00
var eventStore = require('./eventStore');
var eventType = require('./eventType');
var logger = require('./logger');
var knex = require('./dbPool');
var NotFoundError = require('./error/NotFoundError');
2014-11-14 15:06:53 +01:00
var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters'];
eventStore.on(eventType.featureCreated, function (event) {
return createFeature(event.data);
2014-11-14 15:06:53 +01:00
});
2014-10-23 16:00:51 +02:00
eventStore.on(eventType.featureUpdated, function (event) {
return updateFeature(event.data);
2014-11-14 15:06:53 +01:00
});
2014-10-23 16:00:51 +02:00
function getFeatures() {
2014-11-14 15:06:53 +01:00
return knex
.select(FEATURE_COLUMNS)
.from('features')
2014-12-10 17:13:26 +01:00
.orderBy('name', 'asc')
2014-11-14 16:40:13 +01:00
.map(rowToFeature);
2014-10-23 13:51:37 +02:00
}
function getFeature(name) {
2014-11-14 15:06:53 +01:00
return knex
.first(FEATURE_COLUMNS)
2014-11-14 15:06:53 +01:00
.from('features')
.where({name: name})
.then(rowToFeature);
}
2014-11-14 15:06:53 +01:00
function rowToFeature(row) {
if (!row) {
throw new NotFoundError('No feature toggle found');
}
2014-10-23 13:51:37 +02:00
return {
name: row.name,
2014-11-10 16:21:22 +01:00
description: row.description,
2014-10-23 13:51:37 +02:00
enabled: row.enabled > 0,
2014-11-14 15:06:53 +01:00
strategy: row.strategy_name, // jshint ignore: line
2014-10-23 13:51:37 +02:00
parameters: row.parameters
};
}
function eventDataToRow(data) {
2014-11-14 15:06:53 +01:00
return {
name: data.name,
description: data.description,
enabled: data.enabled ? 1 : 0,
strategy_name: data.strategy, // jshint ignore: line
parameters: data.parameters
2014-11-14 15:06:53 +01:00
};
}
function createFeature(data) {
return knex('features')
.insert(eventDataToRow(data))
.catch(function (err) {
logger.error('Could not insert feature, error was: ', err);
});
}
function updateFeature(data) {
return knex('features')
.where({name: data.name})
.update(eventDataToRow(data))
.catch(function (err) {
logger.error('Could not update feature, error was: ', err);
});
}
module.exports = {
getFeatures: getFeatures,
getFeature: getFeature,
_createFeature: createFeature, // visible for testing
_updateFeature: updateFeature // visible for testing
};