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

95 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
2016-10-27 20:51:21 +02:00
const eventType = require('../event-type');
2016-06-20 10:27:19 +02:00
const logger = require('../logger');
2016-10-27 20:51:21 +02:00
const NotFoundError = require('../error/notfound-error');
2016-11-05 13:01:17 +01:00
const FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategies', 'created_at'];
2016-11-05 10:16:48 +01:00
class FeatureToggleStore {
constructor (db, eventStore) {
this.db = db;
eventStore.on(eventType.featureCreated, event => this._createFeature(event.data));
eventStore.on(eventType.featureUpdated, event => this._updateFeature(event.data));
eventStore.on(eventType.featureArchived, event => this._archiveFeature(event.data));
eventStore.on(eventType.featureRevived, event => this._reviveFeature(event.data));
}
2016-11-05 10:16:48 +01:00
getFeatures () {
return this.db
.select(FEATURE_COLUMNS)
.from('features')
.where({ archived: 0 })
.orderBy('name', 'asc')
2016-11-05 10:16:48 +01:00
.map(this.rowToFeature);
}
2016-11-05 10:16:48 +01:00
getFeature (name) {
return this.db
.first(FEATURE_COLUMNS)
.from('features')
2016-06-18 21:53:18 +02:00
.where({ name })
2016-11-05 10:16:48 +01:00
.then(this.rowToFeature);
}
2016-11-05 10:16:48 +01:00
getArchivedFeatures () {
return this.db
.select(FEATURE_COLUMNS)
.from('features')
.where({ archived: 1 })
.orderBy('name', 'asc')
2016-11-05 10:16:48 +01:00
.map(this.rowToFeature);
}
2016-11-05 10:16:48 +01:00
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-11-05 13:01:17 +01:00
createdAt: row.created_at,
};
}
2016-11-05 10:16:48 +01:00
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-11-05 10:16:48 +01:00
_createFeature (data) {
return this.db('features')
.insert(this.eventDataToRow(data))
2016-12-02 16:20:13 +01:00
.catch(err => logger.error('Could not insert feature, error was: ', err));
}
2016-11-05 10:16:48 +01:00
_updateFeature (data) {
return this.db('features')
.where({ name: data.name })
2016-11-05 10:16:48 +01:00
.update(this.eventDataToRow(data))
2016-12-02 16:20:13 +01:00
.catch(err => logger.error('Could not update feature, error was: ', err));
}
2016-11-05 10:16:48 +01:00
_archiveFeature (data) {
return this.db('features')
.where({ name: data.name })
.update({ archived: 1 })
2016-12-02 16:20:13 +01:00
.catch(err => logger.error('Could not archive feature, error was: ', err));
}
2016-11-05 10:16:48 +01:00
_reviveFeature (data) {
return this.db('features')
.where({ name: data.name })
.update({ archived: 0, enabled: 0 })
2016-12-02 16:20:13 +01:00
.catch(err => logger.error('Could not archive feature, error was: ', err));
}
};
2016-11-05 10:16:48 +01:00
module.exports = FeatureToggleStore;