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

139 lines
3.7 KiB
JavaScript
Raw Normal View History

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 {
STRATEGY_CREATED,
STRATEGY_DELETED,
STRATEGY_UPDATED,
STRATEGY_IMPORT,
DROP_STRATEGIES,
2017-06-28 10:17:14 +02:00
} = require('../event-type');
const logger = require('../logger')('strategy-store.js');
2016-10-27 20:51:21 +02:00
const NotFoundError = require('../error/notfound-error');
const STRATEGY_COLUMNS = ['name', 'description', 'parameters', 'built_in'];
2016-11-05 10:16:48 +01:00
const TABLE = 'strategies';
2014-11-17 22:39:04 +01:00
2016-11-05 10:16:48 +01:00
class StrategyStore {
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(STRATEGY_CREATED, event =>
this._createStrategy(event.data)
);
eventStore.on(STRATEGY_UPDATED, event =>
this._updateStrategy(event.data)
);
eventStore.on(STRATEGY_DELETED, event =>
this._deleteStrategy(event.data)
);
eventStore.on(STRATEGY_IMPORT, event =>
this._importStrategy(event.data)
);
eventStore.on(DROP_STRATEGIES, () => this._dropStrategies());
2016-11-05 10:16:48 +01:00
}
2017-06-28 10:17:14 +02:00
getStrategies() {
2016-11-05 10:16:48 +01:00
return this.db
.select(STRATEGY_COLUMNS)
2016-11-05 10:16:48 +01:00
.from(TABLE)
2016-12-10 12:12:00 +01:00
.orderBy('name', 'asc')
2016-11-05 10:16:48 +01:00
.map(this.rowToStrategy);
}
getEditableStrategies() {
return this.db
.select(STRATEGY_COLUMNS)
.from(TABLE)
.where({ built_in: 0 }) // eslint-disable-line
.orderBy('name', 'asc')
.map(this.rowToEditableStrategy);
}
2017-06-28 10:17:14 +02:00
getStrategy(name) {
2016-11-05 10:16:48 +01:00
return this.db
.first(STRATEGY_COLUMNS)
2016-11-05 10:16:48 +01:00
.from(TABLE)
2016-06-18 21:53:18 +02:00
.where({ name })
2016-11-05 10:16:48 +01:00
.then(this.rowToStrategy);
}
2014-11-17 22:39:04 +01:00
2017-06-28 10:17:14 +02:00
rowToStrategy(row) {
if (!row) {
throw new NotFoundError('No strategy found');
}
return {
name: row.name,
editable: row.built_in !== 1,
description: row.description,
2016-12-12 17:09:44 +01:00
parameters: row.parameters,
};
2014-11-17 22:39:04 +01:00
}
rowToEditableStrategy(row) {
if (!row) {
throw new NotFoundError('No strategy found');
}
return {
name: row.name,
description: row.description,
parameters: row.parameters,
};
}
2017-06-28 10:17:14 +02:00
eventDataToRow(data) {
return {
name: data.name,
description: data.description,
2016-12-12 17:09:44 +01:00
parameters: JSON.stringify(data.parameters),
};
}
2017-06-28 10:17:14 +02:00
_createStrategy(data) {
2016-11-05 10:16:48 +01:00
this.db(TABLE)
.insert(this.eventDataToRow(data))
2017-06-28 10:17:14 +02:00
.catch(err =>
logger.error('Could not insert strategy, error was: ', err)
);
}
2017-06-28 10:17:14 +02:00
_updateStrategy(data) {
this.db(TABLE)
.where({ name: data.name })
.update(this.eventDataToRow(data))
2017-06-28 10:17:14 +02:00
.catch(err =>
logger.error('Could not update strategy, error was: ', err)
);
}
_deleteStrategy({ name }) {
return this.db(TABLE)
.where({ name })
.del()
.catch(err => {
logger.error('Could not delete strategy, error was: ', err);
});
}
_importStrategy(data) {
const rowData = this.eventDataToRow(data);
return this.db(TABLE)
.where({ name: rowData.name, built_in: 0 }) // eslint-disable-line
.update(rowData)
.then(result =>
result === 0 ? this.db(TABLE).insert(rowData) : result
)
.catch(err =>
logger.error('Could not import strategy, error was: ', err)
);
}
_dropStrategies() {
return this.db(TABLE)
.where({ built_in: 0 }) // eslint-disable-line
.delete()
.catch(err =>
logger.error('Could not drop strategies, error was: ', err)
);
}
2017-06-28 10:17:14 +02:00
}
2014-11-17 22:39:04 +01:00
2016-11-05 10:16:48 +01:00
module.exports = StrategyStore;