1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/packages/unleash-api/lib/db/feature.js
Ivar 096c86d58b Add support for aggregate_strategies in the API.
This commit changes the features-tables:
- drop columns 'strategy' and 'parameters'
- add column 'strategies' of type json.
- migrates existing strategy-mappings in to the new format.

The idea is that the 'strategies' column should contain a json-array
of strategy-configuration for the toggle:

```
[{
 "name" : "strategy1",
 "parameters": { "name": "vale" }
}]

```

To make sure to not break exiting clients the api is extended with a
mapping layer (adding old fields to the json-respons, and mapping
to the new format on create/update a feature toggle.

this commit is first step in solving #102
2016-09-06 21:31:28 +02:00

109 lines
3.1 KiB
JavaScript

'use strict';
const eventType = require('../eventType');
const logger = require('../logger');
const NotFoundError = require('../error/NotFoundError');
const FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategies'];
module.exports = function (db, eventStore) {
eventStore.on(eventType.featureCreated, event => createFeature(event.data));
eventStore.on(eventType.featureUpdated, event => updateFeature(event.data));
eventStore.on(eventType.featureArchived, event => archiveFeature(event.data));
eventStore.on(eventType.featureRevived, event => reviveFeature(event.data));
function getFeatures () {
return db
.select(FEATURE_COLUMNS)
.from('features')
.where({ archived: 0 })
.orderBy('name', 'asc')
.map(rowToFeature);
}
function getFeature (name) {
return db
.first(FEATURE_COLUMNS)
.from('features')
.where({ name })
.then(rowToFeature);
}
function getArchivedFeatures () {
return db
.select(FEATURE_COLUMNS)
.from('features')
.where({ archived: 1 })
.orderBy('name', 'asc')
.map(rowToFeature);
}
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,
};
}
function eventDataToRow (data) {
return {
name: data.name,
description: data.description,
enabled: data.enabled ? 1 : 0,
archived: data.archived ? 1 : 0,
strategies: JSON.stringify(data.strategies),
};
}
function createFeature (data) {
return db('features')
.insert(eventDataToRow(data))
.catch(err => {
logger.error('Could not insert feature, error was: ', err);
});
}
function updateFeature (data) {
console.log(data);
return db('features')
.where({ name: data.name })
.update(eventDataToRow(data))
.catch(err => {
logger.error('Could not update feature, error was: ', err);
});
}
function archiveFeature (data) {
return db('features')
.where({ name: data.name })
.update({ archived: 1 })
.catch(err => {
logger.error('Could not archive feature, error was: ', err);
});
}
function reviveFeature (data) {
return db('features')
.where({ name: data.name })
.update({ archived: 0, enabled: 0 })
.catch(err => {
logger.error('Could not archive feature, error was: ', err);
});
}
return {
getFeatures,
getFeature,
getArchivedFeatures,
_createFeature: createFeature, // visible for testing
_updateFeature: updateFeature, // visible for testing
};
};