1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/db/strategy-store.ts

191 lines
5.2 KiB
TypeScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
import { Knex } from 'knex';
import { Logger, LogProvider } from '../logger';
2016-10-27 20:51:21 +02:00
const NotFoundError = require('../error/notfound-error');
const STRATEGY_COLUMNS = [
'name',
'description',
'parameters',
'built_in',
'deprecated',
'display_name',
];
2016-11-05 10:16:48 +01:00
const TABLE = 'strategies';
2014-11-17 22:39:04 +01:00
export interface IStrategy {
name: string;
editable: boolean;
description: string;
parameters: object;
deprecated: boolean;
displayName: string;
}
export interface IEditableStrategy {
name: string;
description: string;
parameters: object;
deprecated: boolean;
}
export interface IMinimalStrategy {
name: string;
description: string;
parameters: string;
}
interface IStrategyRow {
name: string;
built_in: number;
description: string;
parameters: object;
deprecated: boolean;
display_name: string;
}
export default class StrategyStore {
private db: Knex;
private logger: Logger;
constructor(db: Knex, getLogger: LogProvider) {
2016-11-05 10:16:48 +01:00
this.db = db;
this.logger = getLogger('strategy-store.js');
2016-11-05 10:16:48 +01:00
}
async getStrategies(): Promise<IStrategy[]> {
2020-09-18 09:05:09 +02:00
const rows = await this.db
.select(STRATEGY_COLUMNS)
2016-11-05 10:16:48 +01:00
.from(TABLE)
.orderBy('sort_order', 'asc')
2020-09-18 09:05:09 +02:00
.orderBy('name', 'asc');
return rows.map(this.rowToStrategy);
}
async getEditableStrategies(): Promise<IEditableStrategy[]> {
2020-09-18 09:05:09 +02:00
const rows = await this.db
.select(STRATEGY_COLUMNS)
.from(TABLE)
.where({ built_in: 0 }) // eslint-disable-line
.orderBy('sort_order', 'asc')
2020-09-18 09:05:09 +02:00
.orderBy('name', 'asc');
2020-09-18 09:05:09 +02:00
return rows.map(this.rowToEditableStrategy);
}
async getStrategy(name: string): Promise<IStrategy> {
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
rowToStrategy(row: IStrategyRow): IStrategy {
if (!row) {
throw new NotFoundError('No strategy found');
}
return {
displayName: row.display_name,
name: row.name,
editable: row.built_in !== 1,
description: row.description,
2016-12-12 17:09:44 +01:00
parameters: row.parameters,
deprecated: row.deprecated,
};
2014-11-17 22:39:04 +01:00
}
rowToEditableStrategy(row: IStrategyRow): IEditableStrategy {
if (!row) {
throw new NotFoundError('No strategy found');
}
return {
name: row.name,
description: row.description,
parameters: row.parameters,
deprecated: row.deprecated,
};
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
eventDataToRow(data): IMinimalStrategy {
return {
name: data.name,
description: data.description,
2016-12-12 17:09:44 +01:00
parameters: JSON.stringify(data.parameters),
};
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async createStrategy(data): Promise<void> {
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 =>
this.logger.error('Could not insert strategy, error: ', err),
2017-06-28 10:17:14 +02:00
);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async updateStrategy(data): Promise<void> {
this.db(TABLE)
.where({ name: data.name })
.update(this.eventDataToRow(data))
2017-06-28 10:17:14 +02:00
.catch(err =>
this.logger.error('Could not update strategy, error: ', err),
2017-06-28 10:17:14 +02:00
);
}
async deprecateStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void> {
this.db(TABLE)
.where({ name })
.update({ deprecated: true })
.catch(err =>
this.logger.error('Could not deprecate strategy, error: ', err),
);
}
async reactivateStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void> {
this.db(TABLE)
.where({ name })
.update({ deprecated: false })
.catch(err =>
this.logger.error(
'Could not reactivate strategy, error: ',
err,
),
);
}
async deleteStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void> {
await this.db(TABLE)
.where({ name })
.del()
.catch(err => {
this.logger.error('Could not delete strategy, error: ', err);
});
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async importStrategy(data): Promise<void> {
const rowData = this.eventDataToRow(data);
await this.db(TABLE)
.insert(rowData)
.onConflict(['name'])
.merge();
}
async dropStrategies(): Promise<void> {
await this.db(TABLE)
.where({ built_in: 0 }) // eslint-disable-line
.delete()
.catch(err =>
this.logger.error('Could not drop strategies, error: ', 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;