2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const {
|
|
|
|
FEATURE_CREATED,
|
|
|
|
FEATURE_UPDATED,
|
|
|
|
FEATURE_ARCHIVED,
|
|
|
|
FEATURE_REVIVED,
|
|
|
|
} = require('../event-type');
|
2017-08-04 16:03:15 +02:00
|
|
|
const logger = require('../logger')('client-toggle-store.js');
|
2016-10-27 20:51:21 +02:00
|
|
|
const NotFoundError = require('../error/notfound-error');
|
2017-06-28 10:17:14 +02:00
|
|
|
const FEATURE_COLUMNS = [
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'enabled',
|
|
|
|
'strategies',
|
2018-01-30 15:57:12 +01:00
|
|
|
'variants',
|
2017-06-28 10:17:14 +02:00
|
|
|
'created_at',
|
|
|
|
];
|
2016-12-09 14:50:30 +01:00
|
|
|
const TABLE = 'features';
|
2014-10-23 09:50:23 +02:00
|
|
|
|
2016-11-05 10:16:48 +01:00
|
|
|
class FeatureToggleStore {
|
2017-06-28 10:17:14 +02:00
|
|
|
constructor(db, eventStore) {
|
2016-11-05 10:16:48 +01:00
|
|
|
this.db = db;
|
2017-06-28 10:17:14 +02:00
|
|
|
eventStore.on(FEATURE_CREATED, event =>
|
|
|
|
this._createFeature(event.data)
|
|
|
|
);
|
|
|
|
eventStore.on(FEATURE_UPDATED, event =>
|
|
|
|
this._updateFeature(event.data)
|
|
|
|
);
|
|
|
|
eventStore.on(FEATURE_ARCHIVED, event =>
|
|
|
|
this._archiveFeature(event.data)
|
|
|
|
);
|
|
|
|
eventStore.on(FEATURE_REVIVED, event =>
|
|
|
|
this._reviveFeature(event.data)
|
|
|
|
);
|
2016-11-05 10:16:48 +01:00
|
|
|
}
|
2016-05-01 22:53:09 +02:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getFeatures() {
|
2016-11-05 10:16:48 +01:00
|
|
|
return this.db
|
2016-05-01 22:53:09 +02:00
|
|
|
.select(FEATURE_COLUMNS)
|
2016-12-09 14:50:30 +01:00
|
|
|
.from(TABLE)
|
2016-05-01 22:53:09 +02:00
|
|
|
.where({ archived: 0 })
|
|
|
|
.orderBy('name', 'asc')
|
2016-11-05 10:16:48 +01:00
|
|
|
.map(this.rowToFeature);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getFeature(name) {
|
2016-11-05 10:16:48 +01:00
|
|
|
return this.db
|
2016-05-01 22:53:09 +02:00
|
|
|
.first(FEATURE_COLUMNS)
|
2016-12-09 14:50:30 +01:00
|
|
|
.from(TABLE)
|
2017-06-28 17:44:14 +02:00
|
|
|
.where({ name, archived: 0 })
|
2016-11-05 10:16:48 +01:00
|
|
|
.then(this.rowToFeature);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2018-01-20 13:28:04 +01:00
|
|
|
hasFeature(name) {
|
2017-12-18 09:08:04 +01:00
|
|
|
return this.db
|
2018-01-20 13:28:04 +01:00
|
|
|
.first('name', 'archived')
|
2017-12-18 09:08:04 +01:00
|
|
|
.from(TABLE)
|
|
|
|
.where({ name })
|
2018-01-20 13:28:04 +01:00
|
|
|
.then(row => {
|
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No feature toggle found');
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
archived: row.archived === 1,
|
|
|
|
};
|
|
|
|
});
|
2017-12-18 09:08:04 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getArchivedFeatures() {
|
2016-11-05 10:16:48 +01:00
|
|
|
return this.db
|
2016-05-01 22:53:09 +02:00
|
|
|
.select(FEATURE_COLUMNS)
|
2016-12-09 14:50:30 +01:00
|
|
|
.from(TABLE)
|
2016-05-01 22:53:09 +02:00
|
|
|
.where({ archived: 1 })
|
|
|
|
.orderBy('name', 'asc')
|
2016-11-05 10:16:48 +01:00
|
|
|
.map(this.rowToFeature);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
rowToFeature(row) {
|
2016-05-01 22:53:09 +02:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No feature toggle found');
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
description: row.description,
|
|
|
|
enabled: row.enabled > 0,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: row.strategies,
|
2018-01-30 15:57:12 +01:00
|
|
|
variants: row.variants,
|
2016-11-05 13:01:17 +01:00
|
|
|
createdAt: row.created_at,
|
2016-05-01 22:53:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
eventDataToRow(data) {
|
2016-05-01 22:53:09 +02:00
|
|
|
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,
|
2016-07-02 16:25:30 +02:00
|
|
|
strategies: JSON.stringify(data.strategies),
|
2018-01-30 15:57:12 +01:00
|
|
|
variants: data.variants ? JSON.stringify(data.variants) : null,
|
2018-02-07 21:58:53 +01:00
|
|
|
created_at: data.createdAt, // eslint-disable-line
|
2016-05-01 22:53:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
_createFeature(data) {
|
2016-12-09 14:50:30 +01:00
|
|
|
return this.db(TABLE)
|
2016-11-05 10:16:48 +01:00
|
|
|
.insert(this.eventDataToRow(data))
|
2017-06-28 10:17:14 +02:00
|
|
|
.catch(err =>
|
|
|
|
logger.error('Could not insert feature, error was: ', err)
|
|
|
|
);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
_updateFeature(data) {
|
2016-12-09 14:50:30 +01:00
|
|
|
return this.db(TABLE)
|
2016-05-01 22:53:09 +02:00
|
|
|
.where({ name: data.name })
|
2016-11-05 10:16:48 +01:00
|
|
|
.update(this.eventDataToRow(data))
|
2017-06-28 10:17:14 +02:00
|
|
|
.catch(err =>
|
|
|
|
logger.error('Could not update feature, error was: ', err)
|
|
|
|
);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
_archiveFeature({ name }) {
|
2016-12-09 14:54:18 +01:00
|
|
|
return this.db(TABLE)
|
|
|
|
.where({ name })
|
2017-06-28 17:44:14 +02:00
|
|
|
.update({ archived: 1, enabled: 0 })
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Could not archive feature, error was: ', err);
|
|
|
|
});
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
_reviveFeature({ name }) {
|
2016-12-09 14:54:18 +01:00
|
|
|
return this.db(TABLE)
|
|
|
|
.where({ name })
|
2016-05-01 22:53:09 +02:00
|
|
|
.update({ archived: 0, enabled: 0 })
|
2017-06-28 10:17:14 +02:00
|
|
|
.catch(err =>
|
|
|
|
logger.error('Could not archive feature, error was: ', err)
|
|
|
|
);
|
2014-11-14 16:58:05 +01:00
|
|
|
}
|
2017-06-28 10:17:14 +02:00
|
|
|
}
|
2016-11-05 10:16:48 +01:00
|
|
|
|
|
|
|
module.exports = FeatureToggleStore;
|