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

202 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
2020-09-18 09:05:09 +02:00
const metricsHelper = require('../metrics-helper');
const { DB_TIME } = require('../events');
2017-06-28 10:17:14 +02:00
const {
FEATURE_CREATED,
FEATURE_UPDATED,
FEATURE_ARCHIVED,
FEATURE_REVIVED,
FEATURE_IMPORT,
DROP_FEATURES,
2017-06-28 10:17:14 +02:00
} = require('../event-type');
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',
'type',
2017-06-28 10:17:14 +02:00
'enabled',
'stale',
2017-06-28 10:17:14 +02:00
'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';
2016-11-05 10:16:48 +01:00
class FeatureToggleStore {
2020-09-18 09:05:09 +02:00
constructor(db, eventStore, eventBus, getLogger) {
2016-11-05 10:16:48 +01:00
this.db = db;
2020-09-18 09:05:09 +02:00
this.getLogger = getLogger('feature-toggle-store.js');
this.timer = action =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'feature-toggle',
action,
});
2017-06-28 10:17:14 +02:00
eventStore.on(FEATURE_CREATED, event =>
this._createFeature(event.data),
2017-06-28 10:17:14 +02:00
);
eventStore.on(FEATURE_UPDATED, event =>
this._updateFeature(event.data),
2017-06-28 10:17:14 +02:00
);
eventStore.on(FEATURE_ARCHIVED, event =>
this._archiveFeature(event.data),
2017-06-28 10:17:14 +02:00
);
eventStore.on(FEATURE_REVIVED, event =>
this._reviveFeature(event.data),
2017-06-28 10:17:14 +02:00
);
eventStore.on(FEATURE_IMPORT, event => this._importFeature(event.data));
eventStore.on(DROP_FEATURES, () => this._dropFeatures());
2016-11-05 10:16:48 +01:00
}
2020-09-18 09:05:09 +02:00
async getFeatures() {
const stopTimer = this.timer('getAll');
const rows = await this.db
.select(FEATURE_COLUMNS)
2016-12-09 14:50:30 +01:00
.from(TABLE)
.where({ archived: 0 })
2020-09-18 09:05:09 +02:00
.orderBy('name', 'asc');
stopTimer();
return rows.map(this.rowToFeature);
}
2020-09-18 09:05:09 +02:00
async count() {
return this.db
.count('*')
.from(TABLE)
.where({ archived: 0 })
.then(res => Number(res[0].count));
}
2020-09-18 09:05:09 +02:00
async getFeature(name) {
2016-11-05 10:16:48 +01:00
return this.db
.first(FEATURE_COLUMNS)
2016-12-09 14:50:30 +01:00
.from(TABLE)
.where({ name, archived: 0 })
2016-11-05 10:16:48 +01:00
.then(this.rowToFeature);
}
2020-09-18 09:05:09 +02:00
async hasFeature(name) {
return this.db
.first('name', 'archived')
.from(TABLE)
.where({ name })
.then(row => {
if (!row) {
throw new NotFoundError('No feature toggle found');
}
return {
name: row.name,
archived: row.archived === 1,
};
});
}
2020-09-18 09:05:09 +02:00
async getArchivedFeatures() {
const rows = await this.db
.select(FEATURE_COLUMNS)
2016-12-09 14:50:30 +01:00
.from(TABLE)
.where({ archived: 1 })
2020-09-18 09:05:09 +02:00
.orderBy('name', 'asc');
return rows.map(this.rowToFeature);
}
2017-06-28 10:17:14 +02:00
rowToFeature(row) {
if (!row) {
throw new NotFoundError('No feature toggle found');
}
return {
name: row.name,
description: row.description,
type: row.type,
enabled: row.enabled > 0,
stale: row.stale,
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,
};
}
2017-06-28 10:17:14 +02:00
eventDataToRow(data) {
return {
name: data.name,
description: data.description,
type: data.type,
enabled: data.enabled ? 1 : 0,
stale: data.stale,
2016-07-02 11:54:50 +02:00
archived: data.archived ? 1 : 0,
strategies: JSON.stringify(data.strategies),
2018-01-30 15:57:12 +01:00
variants: data.variants ? JSON.stringify(data.variants) : null,
created_at: data.createdAt, // eslint-disable-line
};
}
2020-09-18 09:05:09 +02:00
async _createFeature(data) {
try {
await this.db(TABLE).insert(this.eventDataToRow(data));
} catch (err) {
this.logger.error('Could not insert feature, error: ', err);
}
}
2020-09-18 09:05:09 +02:00
async _updateFeature(data) {
try {
await this.db(TABLE)
.where({ name: data.name })
.update(this.eventDataToRow(data));
} catch (err) {
this.logger.error('Could not update feature, error: ', err);
}
}
2020-09-18 09:05:09 +02:00
async _archiveFeature({ name }) {
try {
await this.db(TABLE)
.where({ name })
.update({ archived: 1, enabled: 0 });
} catch (err) {
this.logger.error('Could not archive feature, error: ', err);
}
}
2020-09-18 09:05:09 +02:00
async _reviveFeature({ name }) {
try {
await this.db(TABLE)
.where({ name })
.update({ archived: 0, enabled: 0 });
} catch (err) {
this.logger.error('Could not archive feature, error: ', err);
}
}
2020-09-18 09:05:09 +02:00
async _importFeature(data) {
const rowData = this.eventDataToRow(data);
2020-09-18 09:05:09 +02:00
try {
const result = await this.db(TABLE)
.where({ name: rowData.name })
.update(rowData);
if (result === 0) {
await this.db(TABLE).insert(rowData);
}
} catch (err) {
this.logger.error('Could not import feature, error: ', err);
}
}
2020-09-18 09:05:09 +02:00
async _dropFeatures() {
try {
await this.db(TABLE).delete();
} catch (err) {
this.logger.error('Could not drop features, error: ', err);
}
}
2017-06-28 10:17:14 +02:00
}
2016-11-05 10:16:48 +01:00
module.exports = FeatureToggleStore;