UNPKG

unleash-server

Version:

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

68 lines 2.29 kB
import metricsHelper from '../util/metrics-helper.js'; import { DB_TIME } from '../metric-events.js'; const T = { FAVORITE_PROJECTS: 'favorite_projects', }; const rowToFavorite = (row) => { return { userId: row.user_id, project: row.project, createdAt: row.created_at, }; }; export class FavoriteProjectsStore { constructor(db, eventBus, _getLogger) { this.db = db; this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'favorite_projects', action, }); } async addFavoriteProject({ userId, project, }) { const stop = this.timer('insertFavoriteProject'); const insertedProject = await this.db(T.FAVORITE_PROJECTS) .insert({ project, user_id: userId }) .onConflict(['user_id', 'project']) .merge() .returning('*'); stop(); return rowToFavorite(insertedProject[0]); } async delete({ userId, project }) { const stop = this.timer('deleteFavoriteProject'); await this.db(T.FAVORITE_PROJECTS) .where({ project, user_id: userId }) .del(); stop(); } async deleteAll() { const stop = this.timer('deleteAll'); await this.db(T.FAVORITE_PROJECTS).del(); stop(); } destroy() { } async exists({ userId, project }) { const stop = this.timer('favoriteProjectExists'); const result = await this.db.raw(`SELECT EXISTS(SELECT 1 FROM ${T.FAVORITE_PROJECTS} WHERE user_id = ? AND project = ?) AS present`, [userId, project]); const { present } = result.rows[0]; stop(); return present; } async get({ userId, project, }) { const stop = this.timer('getFavoriteProject'); const favorite = await this.db .table(T.FAVORITE_PROJECTS) .select() .where({ project, user_id: userId }) .first(); stop(); return rowToFavorite(favorite); } async getAll() { const stop = this.timer('getAll'); const groups = await this.db(T.FAVORITE_PROJECTS).select(); stop(); return groups.map(rowToFavorite); } } //# sourceMappingURL=favorite-projects-store.js.map