mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-04 00:18:01 +01:00
c17a1980a2
This simplifies stores to just be storage interaction, they no longer react to events. Controllers now call services and awaits the result from the call. When the service calls are returned the database is updated. This simplifies testing dramatically, cause you know that your state is updated when returned from a call, rather than hoping the store has picked up the event (which really was a command) and reacted to it. Events are still emitted from eventStore, so other parts of the app can react to events as they're being sent out. As part of the move to services, we now also emit an application-created event when we see a new client application. Fixes: #685 Fixes: #595
121 lines
3.2 KiB
JavaScript
121 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
const NotFoundError = require('../error/notfound-error');
|
|
|
|
const STRATEGY_COLUMNS = ['name', 'description', 'parameters', 'built_in'];
|
|
const TABLE = 'strategies';
|
|
|
|
class StrategyStore {
|
|
constructor(db, getLogger) {
|
|
this.db = db;
|
|
this.logger = getLogger('strategy-store.js');
|
|
}
|
|
|
|
async getStrategies() {
|
|
const rows = await this.db
|
|
.select(STRATEGY_COLUMNS)
|
|
.from(TABLE)
|
|
.orderBy('name', 'asc');
|
|
|
|
return rows.map(this.rowToStrategy);
|
|
}
|
|
|
|
async getEditableStrategies() {
|
|
const rows = await this.db
|
|
.select(STRATEGY_COLUMNS)
|
|
.from(TABLE)
|
|
.where({ built_in: 0 }) // eslint-disable-line
|
|
.orderBy('name', 'asc');
|
|
return rows.map(this.rowToEditableStrategy);
|
|
}
|
|
|
|
async getStrategy(name) {
|
|
return this.db
|
|
.first(STRATEGY_COLUMNS)
|
|
.from(TABLE)
|
|
.where({ name })
|
|
.then(this.rowToStrategy);
|
|
}
|
|
|
|
rowToStrategy(row) {
|
|
if (!row) {
|
|
throw new NotFoundError('No strategy found');
|
|
}
|
|
return {
|
|
name: row.name,
|
|
editable: row.built_in !== 1,
|
|
description: row.description,
|
|
parameters: row.parameters,
|
|
};
|
|
}
|
|
|
|
rowToEditableStrategy(row) {
|
|
if (!row) {
|
|
throw new NotFoundError('No strategy found');
|
|
}
|
|
return {
|
|
name: row.name,
|
|
description: row.description,
|
|
parameters: row.parameters,
|
|
};
|
|
}
|
|
|
|
eventDataToRow(data) {
|
|
return {
|
|
name: data.name,
|
|
description: data.description,
|
|
parameters: JSON.stringify(data.parameters),
|
|
};
|
|
}
|
|
|
|
async createStrategy(data) {
|
|
this.db(TABLE)
|
|
.insert(this.eventDataToRow(data))
|
|
.catch(err =>
|
|
this.logger.error('Could not insert strategy, error: ', err),
|
|
);
|
|
}
|
|
|
|
async updateStrategy(data) {
|
|
this.db(TABLE)
|
|
.where({ name: data.name })
|
|
.update(this.eventDataToRow(data))
|
|
.catch(err =>
|
|
this.logger.error('Could not update strategy, error: ', err),
|
|
);
|
|
}
|
|
|
|
async deleteStrategy({ name }) {
|
|
return this.db(TABLE)
|
|
.where({ name })
|
|
.del()
|
|
.catch(err => {
|
|
this.logger.error('Could not delete strategy, error: ', err);
|
|
});
|
|
}
|
|
|
|
async 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 =>
|
|
this.logger.error('Could not import strategy, error: ', err),
|
|
);
|
|
}
|
|
|
|
async dropStrategies() {
|
|
return this.db(TABLE)
|
|
.where({ built_in: 0 }) // eslint-disable-line
|
|
.delete()
|
|
.catch(err =>
|
|
this.logger.error('Could not drop strategies, error: ', err),
|
|
);
|
|
}
|
|
}
|
|
|
|
module.exports = StrategyStore;
|