1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-api/lib/db/feature.js

108 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-06-20 10:27:19 +02:00
const eventType = require('../eventType');
const logger = require('../logger');
const NotFoundError = require('../error/NotFoundError');
const FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategies'];
2016-07-02 11:54:50 +02:00
module.exports = function (db, eventStore) {
2016-06-18 21:53:18 +02:00
eventStore.on(eventType.featureCreated, event => createFeature(event.data));
2016-06-18 21:53:18 +02:00
eventStore.on(eventType.featureUpdated, event => updateFeature(event.data));
2016-06-18 21:53:18 +02:00
eventStore.on(eventType.featureArchived, event => archiveFeature(event.data));
2016-06-18 21:53:18 +02:00
eventStore.on(eventType.featureRevived, event => reviveFeature(event.data));
2016-07-02 11:54:50 +02:00
function getFeatures () {
return db
.select(FEATURE_COLUMNS)
.from('features')
.where({ archived: 0 })
.orderBy('name', 'asc')
.map(rowToFeature);
}
2016-07-02 11:54:50 +02:00
function getFeature (name) {
return db
.first(FEATURE_COLUMNS)
.from('features')
2016-06-18 21:53:18 +02:00
.where({ name })
.then(rowToFeature);
}
2016-07-02 11:54:50 +02:00
function getArchivedFeatures () {
return db
.select(FEATURE_COLUMNS)
.from('features')
.where({ archived: 1 })
.orderBy('name', 'asc')
.map(rowToFeature);
}
2016-07-02 11:54:50 +02:00
function rowToFeature (row) {
if (!row) {
throw new NotFoundError('No feature toggle found');
}
return {
name: row.name,
description: row.description,
enabled: row.enabled > 0,
strategies: row.strategies,
};
}
2016-07-02 11:54:50 +02:00
function eventDataToRow (data) {
return {
name: data.name,
description: data.description,
enabled: data.enabled ? 1 : 0,
2016-07-02 11:54:50 +02:00
archived: data.archived ? 1 : 0,
strategies: JSON.stringify(data.strategies),
};
}
2016-07-02 11:54:50 +02:00
function createFeature (data) {
return db('features')
.insert(eventDataToRow(data))
2016-06-18 21:53:18 +02:00
.catch(err => {
logger.error('Could not insert feature, error was: ', err);
});
}
2016-07-02 11:54:50 +02:00
function updateFeature (data) {
return db('features')
.where({ name: data.name })
.update(eventDataToRow(data))
2016-06-18 21:53:18 +02:00
.catch(err => {
logger.error('Could not update feature, error was: ', err);
});
}
2016-07-02 11:54:50 +02:00
function archiveFeature (data) {
return db('features')
.where({ name: data.name })
.update({ archived: 1 })
2016-06-18 21:53:18 +02:00
.catch(err => {
logger.error('Could not archive feature, error was: ', err);
});
}
2016-07-02 11:54:50 +02:00
function reviveFeature (data) {
return db('features')
.where({ name: data.name })
.update({ archived: 0, enabled: 0 })
2016-06-18 21:53:18 +02:00
.catch(err => {
logger.error('Could not archive feature, error was: ', err);
});
}
2014-11-14 15:06:53 +01:00
return {
2016-06-18 21:53:18 +02:00
getFeatures,
getFeature,
getArchivedFeatures,
_createFeature: createFeature, // visible for testing
2016-06-18 21:55:46 +02:00
_updateFeature: updateFeature, // visible for testing
2014-11-14 15:06:53 +01:00
};
};