UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

88 lines 2.78 kB
import { DB_TIME } from '../metric-events.js'; import metricsHelper from '../util/metrics-helper.js'; import NotFoundError from '../error/notfound-error.js'; const COLUMNS = ['type', 'value']; const TABLE = 'tags'; export default class TagStore { constructor(db, eventBus, getLogger) { this.db = db; this.logger = getLogger('tag-store.ts'); this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'tag', action, }); } async getTagsByType(type) { const stopTimer = this.timer('getTagByType'); const rows = await this.db.select(COLUMNS).from(TABLE).where({ type }); stopTimer(); return rows.map(this.rowToTag); } async getAll() { const stopTimer = this.timer('getAll'); const rows = await this.db.select(COLUMNS).from(TABLE); stopTimer(); return rows.map(this.rowToTag); } async getTag(type, value) { const stopTimer = this.timer('getTag'); const tag = await this.db .first(COLUMNS) .from(TABLE) .where({ type, value }); stopTimer(); if (!tag) { throw new NotFoundError(`No tag with type: [${type}] and value [${value}]`); } return tag; } async exists(tag) { const stopTimer = this.timer('exists'); const result = await this.db.raw(`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE type = ? AND value = ?) AS present`, [tag.type, tag.value]); const { present } = result.rows[0]; stopTimer(); return present; } async createTag(tag) { const stopTimer = this.timer('createTag'); await this.db(TABLE).insert(tag); stopTimer(); } async delete(tag) { const stopTimer = this.timer('deleteTag'); await this.db(TABLE).where(tag).del(); stopTimer(); } async deleteAll() { const stopTimer = this.timer('deleteAll'); await this.db(TABLE).del(); stopTimer(); } async bulkImport(tags) { return this.db(TABLE) .insert(tags) .returning(COLUMNS) .onConflict(['type', 'value']) .ignore(); } destroy() { } async get({ type, value }) { const stopTimer = this.timer('getTag'); const tag = await this.db .first(COLUMNS) .from(TABLE) .where({ type, value }); stopTimer(); if (!tag) { throw new NotFoundError(`No tag with type: [${type}] and value [${value}]`); } return tag; } rowToTag(row) { return { type: row.type, value: row.value, }; } } //# sourceMappingURL=tag-store.js.map