2021-04-20 12:32:02 +02:00
|
|
|
import { Logger, LogProvider } from '../logger';
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
import NotFoundError from '../error/notfound-error';
|
2021-08-12 15:04:37 +02:00
|
|
|
import {
|
|
|
|
IEditableStrategy,
|
|
|
|
IMinimalStrategyRow,
|
|
|
|
IStrategy,
|
2022-12-21 13:33:41 +01:00
|
|
|
IStrategyImport,
|
2021-08-12 15:04:37 +02:00
|
|
|
IStrategyStore,
|
|
|
|
} from '../types/stores/strategy-store';
|
2023-01-30 09:02:44 +01:00
|
|
|
import { Db } from './db';
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2021-01-21 13:47:08 +01:00
|
|
|
const STRATEGY_COLUMNS = [
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'parameters',
|
|
|
|
'built_in',
|
|
|
|
'deprecated',
|
2021-04-22 13:33:35 +02:00
|
|
|
'display_name',
|
2021-01-21 13:47:08 +01:00
|
|
|
];
|
2016-11-05 10:16:48 +01:00
|
|
|
const TABLE = 'strategies';
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
interface IStrategyRow {
|
|
|
|
name: string;
|
|
|
|
built_in: number;
|
|
|
|
description: string;
|
2021-08-12 15:04:37 +02:00
|
|
|
parameters: object[];
|
2021-04-20 12:32:02 +02:00
|
|
|
deprecated: boolean;
|
2021-04-22 13:33:35 +02:00
|
|
|
display_name: string;
|
2021-04-20 12:32:02 +02:00
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
export default class StrategyStore implements IStrategyStore {
|
2023-01-30 09:02:44 +01:00
|
|
|
private db: Db;
|
2021-04-20 12:32:02 +02:00
|
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
2023-01-30 09:02:44 +01:00
|
|
|
constructor(db: Db, getLogger: LogProvider) {
|
2016-11-05 10:16:48 +01:00
|
|
|
this.db = db;
|
2021-08-12 15:04:37 +02:00
|
|
|
this.logger = getLogger('strategy-store.ts');
|
2016-11-05 10:16:48 +01:00
|
|
|
}
|
2014-12-08 20:56:22 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async getAll(): Promise<IStrategy[]> {
|
2020-09-18 09:05:09 +02:00
|
|
|
const rows = await this.db
|
2016-05-01 22:53:09 +02:00
|
|
|
.select(STRATEGY_COLUMNS)
|
2016-11-05 10:16:48 +01:00
|
|
|
.from(TABLE)
|
2021-04-22 13:33:35 +02:00
|
|
|
.orderBy('sort_order', 'asc')
|
2020-09-18 09:05:09 +02:00
|
|
|
.orderBy('name', 'asc');
|
|
|
|
|
|
|
|
return rows.map(this.rowToStrategy);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
async getEditableStrategies(): Promise<IEditableStrategy[]> {
|
2020-09-18 09:05:09 +02:00
|
|
|
const rows = await this.db
|
2019-03-14 17:56:02 +01:00
|
|
|
.select(STRATEGY_COLUMNS)
|
|
|
|
.from(TABLE)
|
|
|
|
.where({ built_in: 0 }) // eslint-disable-line
|
2021-04-22 13:33:35 +02:00
|
|
|
.orderBy('sort_order', 'asc')
|
2020-09-18 09:05:09 +02:00
|
|
|
.orderBy('name', 'asc');
|
2021-04-22 13:33:35 +02:00
|
|
|
|
2020-09-18 09:05:09 +02:00
|
|
|
return rows.map(this.rowToEditableStrategy);
|
2019-03-14 17:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
async getStrategy(name: string): Promise<IStrategy> {
|
2016-11-05 10:16:48 +01:00
|
|
|
return this.db
|
2016-05-01 22:53:09 +02:00
|
|
|
.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);
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
2014-11-17 22:39:04 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async delete(name: string): Promise<void> {
|
|
|
|
await this.db(TABLE).where({ name }).del();
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAll(): Promise<void> {
|
|
|
|
await this.db(TABLE).del();
|
|
|
|
}
|
|
|
|
|
2022-10-25 13:10:27 +02:00
|
|
|
async count(): Promise<number> {
|
|
|
|
return this.db
|
|
|
|
.from(TABLE)
|
|
|
|
.count('*')
|
|
|
|
.then((res) => Number(res[0].count));
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
destroy(): void {}
|
|
|
|
|
|
|
|
async exists(name: string): Promise<boolean> {
|
|
|
|
const result = await this.db.raw(
|
|
|
|
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
|
|
|
|
[name],
|
|
|
|
);
|
|
|
|
const { present } = result.rows[0];
|
|
|
|
return present;
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(name: string): Promise<IStrategy> {
|
|
|
|
const row = await this.db(TABLE).where({ name }).first();
|
|
|
|
return this.rowToStrategy(row);
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
rowToStrategy(row: IStrategyRow): IStrategy {
|
2016-05-01 22:53:09 +02:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No strategy found');
|
|
|
|
}
|
|
|
|
return {
|
2021-04-22 13:33:35 +02:00
|
|
|
displayName: row.display_name,
|
2016-05-01 22:53:09 +02:00
|
|
|
name: row.name,
|
2017-06-28 21:10:43 +02:00
|
|
|
editable: row.built_in !== 1,
|
2016-05-01 22:53:09 +02:00
|
|
|
description: row.description,
|
2016-12-12 17:09:44 +01:00
|
|
|
parameters: row.parameters,
|
2021-01-21 13:47:08 +01:00
|
|
|
deprecated: row.deprecated,
|
2016-05-01 22:53:09 +02:00
|
|
|
};
|
2014-11-17 22:39:04 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 12:32:02 +02:00
|
|
|
rowToEditableStrategy(row: IStrategyRow): IEditableStrategy {
|
2019-03-14 17:56:02 +01:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError('No strategy found');
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: row.name,
|
|
|
|
description: row.description,
|
|
|
|
parameters: row.parameters,
|
2021-01-21 13:47:08 +01:00
|
|
|
deprecated: row.deprecated,
|
2019-03-14 17:56:02 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-08-12 15:04:37 +02:00
|
|
|
eventDataToRow(data): IMinimalStrategyRow {
|
2016-05-01 22:53:09 +02:00
|
|
|
return {
|
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
2016-12-12 17:09:44 +01:00
|
|
|
parameters: JSON.stringify(data.parameters),
|
2016-05-01 22:53:09 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async createStrategy(data): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.db(TABLE).insert(this.eventDataToRow(data));
|
2016-05-01 22:53:09 +02:00
|
|
|
}
|
2016-12-17 12:31:23 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-04-20 12:32:02 +02:00
|
|
|
async updateStrategy(data): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.db(TABLE)
|
2016-12-17 12:31:23 +01:00
|
|
|
.where({ name: data.name })
|
2021-08-12 15:04:37 +02:00
|
|
|
.update(this.eventDataToRow(data));
|
2021-01-21 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async deprecateStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.db(TABLE).where({ name }).update({ deprecated: true });
|
2021-01-21 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async reactivateStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.db(TABLE).where({ name }).update({ deprecated: false });
|
2016-12-17 12:31:23 +01:00
|
|
|
}
|
2019-03-13 19:10:13 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
async deleteStrategy({ name }: Pick<IStrategy, 'name'>): Promise<void> {
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.db(TABLE).where({ name }).del();
|
2019-03-13 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 13:33:41 +01:00
|
|
|
async importStrategy(data: IStrategyImport): Promise<void> {
|
|
|
|
const rowData = {
|
|
|
|
name: data.name,
|
|
|
|
description: data.description,
|
|
|
|
deprecated: data.deprecated || false,
|
|
|
|
parameters: JSON.stringify(data.parameters || []),
|
|
|
|
built_in: data.builtIn ? 1 : 0,
|
|
|
|
sort_order: data.sortOrder || 9999,
|
|
|
|
display_name: data.displayName,
|
|
|
|
};
|
2021-08-12 15:04:37 +02:00
|
|
|
await this.db(TABLE).insert(rowData).onConflict(['name']).merge();
|
2019-03-13 19:10:13 +01:00
|
|
|
}
|
|
|
|
|
2021-08-26 22:41:51 +02:00
|
|
|
async dropCustomStrategies(): Promise<void> {
|
2021-04-20 12:32:02 +02:00
|
|
|
await this.db(TABLE)
|
2019-03-13 19:10:13 +01:00
|
|
|
.where({ built_in: 0 }) // eslint-disable-line
|
2021-08-26 22:41:51 +02:00
|
|
|
.delete();
|
2019-03-13 19:10:13 +01:00
|
|
|
}
|
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;
|