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

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2014-11-17 22:39:04 +01:00
var eventStore = require('./eventStore');
var eventType = require('./eventType');
var logger = require('./logger');
var knex = require('./dbPool');
var NotFoundError = require('./error/NotFoundError');
2014-11-17 22:39:04 +01:00
var STRATEGY_COLUMNS = ['name', 'description', 'parameters_template'];
eventStore.on(eventType.strategyCreated, function (event) {
return createStrategy(event.data);
2014-11-17 22:39:04 +01:00
});
eventStore.on(eventType.strategyDeleted, function (event) {
knex('strategies')
.where('name', event.data.name)
.del()
.catch(function (err) {
logger.error('Could not delete strategy, error was: ', err);
});
});
2014-11-17 22:39:04 +01:00
function getStrategies() {
return knex
.select(STRATEGY_COLUMNS)
.from('strategies')
.orderBy('created_at', 'asc')
2014-11-17 22:39:04 +01:00
.map(rowToStrategy);
}
function getStrategy(name) {
return knex
.first(STRATEGY_COLUMNS)
.from('strategies')
.where({name: name})
.then(rowToStrategy);
}
function rowToStrategy(row) {
if (!row) {
throw new NotFoundError('No strategy found');
2014-11-17 22:39:04 +01:00
}
return {
name: row.name,
description: row.description,
parametersTemplate: row.parameters_template // jshint ignore: line
};
}
function eventDataToRow(data) {
2014-11-17 22:39:04 +01:00
return {
name: data.name,
description: data.description,
parameters_template: data.parametersTemplate // jshint ignore: line
2014-11-17 22:39:04 +01:00
};
}
function createStrategy(data) {
knex('strategies')
.insert(eventDataToRow(data))
.catch(function (err) {
logger.error('Could not insert strategy, error was: ', err);
});
}
2014-11-17 22:39:04 +01:00
module.exports = {
getStrategies: getStrategies,
getStrategy: getStrategy,
_createStrategy: createStrategy // visible for testing
2014-11-17 22:39:04 +01:00
};