UNPKG

unleash-server

Version:

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

115 lines 4 kB
import { ulid } from 'ulidx'; import { CRUDStore } from '../../db/crud/crud-store.js'; import NotFoundError from '../../error/notfound-error.js'; const TABLE = 'milestone_strategies'; const fromRow = (row) => { return { id: row.id, milestoneId: row.milestone_id, sortOrder: row.sort_order, title: row.title, name: row.strategy_name, strategyName: row.strategy_name, parameters: row.parameters, constraints: JSON.parse(row.constraints), variants: JSON.parse(row.variants), segments: [], disabled: row.disabled, }; }; const fromDatabaseRow = (row) => { return { id: row.id, milestoneId: row.milestone_id, sortOrder: row.sort_order, title: row.title, name: row.strategy_name, strategyName: row.strategy_name, parameters: row.parameters, constraints: row.constraints, variants: row.variants, disabled: row.disabled, }; }; const toRow = (item) => { return { id: ulid(), milestone_id: item.milestoneId, sort_order: item.sortOrder, title: item.title, strategy_name: item.name, parameters: item.parameters ?? {}, constraints: JSON.stringify(item.constraints ?? []), variants: JSON.stringify(item.variants ?? []), disabled: item.disabled ?? false, }; }; const toUpdateRow = (item) => { return { sort_order: item.sortOrder, title: item.title, parameters: item.parameters ?? {}, constraints: JSON.stringify(item.constraints ?? []), variants: JSON.stringify(item.variants ?? []), disabled: item.disabled ?? false, }; }; export class ReleasePlanMilestoneStrategyStore extends CRUDStore { constructor(db, config) { super(TABLE, db, config); } async get(id) { const row = await this.db(TABLE).where({ id }).first(); if (!row) { throw new NotFoundError(`Milestone strategy with id ${id} not found`); } const strategy = fromDatabaseRow(row); const segmentRows = await this.db('milestone_strategy_segments') .where('milestone_strategy_id', id) .select('segment_id'); return { ...strategy, segments: segmentRows.map((row) => row.segment_id), }; } async insert({ segments, ...strategy }) { const row = toRow(strategy); await this.db(TABLE).insert(row); segments?.forEach(async (segmentId) => { const segmentRow = { milestone_strategy_id: row.id, segment_id: segmentId, }; await this.db('milestone_strategy_segments').insert(segmentRow); }); return fromRow(row); } async updateStrategy(strategyId, { segments, ...strategy }) { const rows = await this.db(this.tableName) .where({ id: strategyId }) .update(toUpdateRow(strategy)) .returning('*'); return fromDatabaseRow(rows[0]); } async upsert(strategyId, { segments, ...strategy }) { const releasePlanMilestoneStrategy = await this.updateStrategy(strategyId, strategy); // now delete await this.db('milestone_strategy_segments') .where('milestone_strategy_id', strategyId) .delete(); for (const segmentId of segments ?? []) { const segmentRow = { milestone_strategy_id: strategyId, segment_id: segmentId, }; await this.db('milestone_strategy_segments').insert(segmentRow); } return { ...releasePlanMilestoneStrategy, segments: segments ?? [] }; } async deleteStrategiesForMilestone(milestoneId) { await this.db('milestone_strategies') .where('milestone_id', milestoneId) .delete(); } } //# sourceMappingURL=release-plan-milestone-strategy-store.js.map