2014-11-14 15:06:53 +01:00
|
|
|
var eventStore = require('./eventStore');
|
|
|
|
var eventType = require('./eventType');
|
|
|
|
var logger = require('./logger');
|
|
|
|
var knex = require('./dbPool');
|
2014-12-10 18:45:02 +01:00
|
|
|
var NotFoundError = require('./error/NotFoundError');
|
2014-11-14 15:06:53 +01:00
|
|
|
var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters'];
|
2014-10-23 09:50:23 +02:00
|
|
|
|
|
|
|
eventStore.on(eventType.featureCreated, function (event) {
|
2014-12-09 14:44:25 +01:00
|
|
|
return createFeature(event.data);
|
2014-11-14 15:06:53 +01:00
|
|
|
});
|
2014-10-23 09:50:23 +02:00
|
|
|
|
2014-10-23 16:00:51 +02:00
|
|
|
eventStore.on(eventType.featureUpdated, function (event) {
|
2014-12-09 14:44:25 +01:00
|
|
|
return updateFeature(event.data);
|
2014-11-14 15:06:53 +01:00
|
|
|
});
|
2014-10-23 16:00:51 +02:00
|
|
|
|
2015-02-09 18:28:51 +01:00
|
|
|
eventStore.on(eventType.featureArchived, function (event) {
|
2014-12-15 22:40:07 +01:00
|
|
|
return archiveFeature(event.data);
|
|
|
|
});
|
|
|
|
|
2015-02-09 18:28:51 +01:00
|
|
|
eventStore.on(eventType.featureRevived, function (event) {
|
2014-12-17 21:56:27 +01:00
|
|
|
return reviveFeature(event.data);
|
|
|
|
});
|
|
|
|
|
2014-10-23 09:50:23 +02:00
|
|
|
function getFeatures() {
|
2014-11-14 15:06:53 +01:00
|
|
|
return knex
|
|
|
|
.select(FEATURE_COLUMNS)
|
|
|
|
.from('features')
|
2014-12-15 22:40:07 +01:00
|
|
|
.where({archived: 0})
|
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
|
|
|
}
|
|
|
|
|
2014-10-23 15:17:04 +02:00
|
|
|
function getFeature(name) {
|
2014-11-14 15:06:53 +01:00
|
|
|
return knex
|
2014-11-14 16:58:05 +01:00
|
|
|
.first(FEATURE_COLUMNS)
|
2014-11-14 15:06:53 +01:00
|
|
|
.from('features')
|
|
|
|
.where({name: name})
|
2014-11-14 16:58:05 +01:00
|
|
|
.then(rowToFeature);
|
2014-10-23 15:17:04 +02:00
|
|
|
}
|
|
|
|
|
2014-12-17 21:56:27 +01:00
|
|
|
function getArchivedFeatures() {
|
|
|
|
return knex
|
2015-03-05 17:55:21 +01:00
|
|
|
.select(FEATURE_COLUMNS)
|
2014-12-17 21:56:27 +01:00
|
|
|
.from('features')
|
|
|
|
.where({archived: 1})
|
2015-03-05 17:55:21 +01:00
|
|
|
.orderBy('name', 'asc')
|
|
|
|
.map(rowToFeature);
|
2014-12-17 21:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-14 15:06:53 +01:00
|
|
|
function rowToFeature(row) {
|
2014-11-14 16:58:05 +01:00
|
|
|
if (!row) {
|
2014-12-10 18:45:02 +01:00
|
|
|
throw new NotFoundError('No feature toggle found');
|
2014-11-14 16:58:05 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
2014-10-23 09:50:23 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 14:44:25 +01:00
|
|
|
function eventDataToRow(data) {
|
2014-11-14 15:06:53 +01:00
|
|
|
return {
|
2014-12-09 14:44:25 +01:00
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
|
|
|
enabled: data.enabled ? 1 : 0,
|
2014-12-17 21:56:27 +01:00
|
|
|
archived: data.archived ? 1 :0,
|
2014-12-09 14:44:25 +01:00
|
|
|
strategy_name: data.strategy, // jshint ignore: line
|
|
|
|
parameters: data.parameters
|
2014-11-14 15:06:53 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-12-09 14:44:25 +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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-15 22:40:07 +01:00
|
|
|
function archiveFeature(data) {
|
|
|
|
return knex('features')
|
|
|
|
.where({name: data.name})
|
|
|
|
.update({archived: 1})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error('Could not archive feature, error was: ', err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-17 21:56:27 +01:00
|
|
|
function reviveFeature(data) {
|
|
|
|
return knex('features')
|
|
|
|
.where({name: data.name})
|
|
|
|
.update({archived: 0, enabled: 0})
|
|
|
|
.catch(function (err) {
|
|
|
|
logger.error('Could not archive feature, error was: ', err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-15 22:40:07 +01:00
|
|
|
|
2014-10-23 09:50:23 +02:00
|
|
|
module.exports = {
|
2014-10-23 15:17:04 +02:00
|
|
|
getFeatures: getFeatures,
|
2014-12-09 14:44:25 +01:00
|
|
|
getFeature: getFeature,
|
2014-12-17 21:56:27 +01:00
|
|
|
getArchivedFeatures: getArchivedFeatures,
|
2014-12-09 14:44:25 +01:00
|
|
|
_createFeature: createFeature, // visible for testing
|
|
|
|
_updateFeature: updateFeature // visible for testing
|
2014-10-23 09:50:23 +02:00
|
|
|
};
|