Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | 69x 69x 69x 69x 69x 69x 87x 87x 87x 51x 39x 39x 39x 39x 5x 5x 5x 5x 2x 3x 7x 7x 7x 7x 7x 1x 1x 1x 5x 5x 1x 12x 8x | import { Knex } from 'knex'; import EventEmitter from 'events'; import { Logger, LogProvider } from '../logger'; import { IAddon, IAddonDto, IAddonStore } from '../types/stores/addon-store'; import metricsHelper from '../util/metrics-helper'; import { DB_TIME } from '../metric-events'; import NotFoundError from '../error/notfound-error'; const COLUMNS = [ 'id', 'provider', 'enabled', 'description', 'parameters', 'events', ]; const TABLE = 'addons'; export default class AddonStore implements IAddonStore { private db: Knex; private logger: Logger; private readonly timer: Function; constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { this.db = db; this.logger = getLogger('addons-store.ts'); this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'addons', action, }); } destroy(): void {} async getAll(query = {}): Promise<IAddon[]> { const stopTimer = this.timer('getAll'); const rows = await this.db.select(COLUMNS).where(query).from(TABLE); stopTimer(); return rows.map(this.rowToAddon); } async get(id: number): Promise<IAddon> { const stopTimer = this.timer('get'); return this.db .first(COLUMNS) .from(TABLE) .where({ id }) .then((row) => { stopTimer(); if (!row) { throw new NotFoundError('Could not find addon'); } else { return this.rowToAddon(row); } }); } async insert(addon: IAddonDto): Promise<IAddon> { const stopTimer = this.timer('insert'); // eslint-disable-next-line @typescript-eslint/naming-convention const rows = await this.db(TABLE).insert(this.addonToRow(addon), [ 'id', 'created_at', ]); stopTimer(); // eslint-disable-next-line @typescript-eslint/naming-convention const { id, created_at } = rows[0]; return { id, createdAt: created_at, ...addon }; } async update(id: number, addon: IAddonDto): Promise<IAddon> { const rows = await this.db(TABLE) .where({ id }) .update(this.addonToRow(addon)); Iif (!rows) { throw new NotFoundError('Could not find addon'); } return rows[0]; } async delete(id: number): Promise<void> { const rows = await this.db(TABLE).where({ id }).del(); if (!rows) { throw new NotFoundError('Could not find addon'); } } async deleteAll(): Promise<void> { await this.db(TABLE).del(); } async exists(id: number): Promise<boolean> { const stopTimer = this.timer('exists'); const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`, [id], ); const { present } = result.rows[0]; stopTimer(); return present; } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types rowToAddon(row): IAddon { return { id: row.id, provider: row.provider, enabled: row.enabled, description: row.description, parameters: row.parameters, events: row.events, createdAt: row.created_at, }; } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types addonToRow(addon: IAddonDto) { return { provider: addon.provider, enabled: addon.enabled, description: addon.description, parameters: JSON.stringify(addon.parameters), events: JSON.stringify(addon.events), }; } } |