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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 69x 69x 69x 69x 87x 87x 87x 14x 14x 11x 11x 2x 2x 15x 15x 8x 15x 7x 7x 12x 12x 240x 51x 51x | import { ISegmentStore } from '../types/stores/segment-store'; import { IConstraint, IFeatureStrategySegment, ISegment } from '../types/model'; import { Logger, LogProvider } from '../logger'; import { Knex } from 'knex'; import EventEmitter from 'events'; import NotFoundError from '../error/notfound-error'; import User from '../types/user'; import { PartialSome } from '../types/partial'; const T = { segments: 'segments', featureStrategies: 'feature_strategies', featureStrategySegment: 'feature_strategy_segment', }; const COLUMNS = [ 'id', 'name', 'description', 'created_by', 'created_at', 'constraints', ]; interface ISegmentRow { id: number; name: string; description?: string; created_by?: string; created_at?: Date; constraints: IConstraint[]; } interface IFeatureStrategySegmentRow { feature_strategy_id: string; segment_id: number; created_at?: Date; } export default class SegmentStore implements ISegmentStore { private logger: Logger; private eventBus: EventEmitter; private db: Knex; constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { this.db = db; this.eventBus = eventBus; this.logger = getLogger('lib/db/segment-store.ts'); } async create( segment: PartialSome<ISegment, 'id'>, user: Partial<Pick<User, 'username' | 'email'>>, ): Promise<ISegment> { const rows = await this.db(T.segments) .insert({ id: segment.id, name: segment.name, description: segment.description, constraints: JSON.stringify(segment.constraints), created_by: user.username || user.email, }) .returning(COLUMNS); return this.mapRow(rows[0]); } async update(id: number, segment: Omit<ISegment, 'id'>): Promise<ISegment> { const rows = await this.db(T.segments) .where({ id }) .update({ name: segment.name, description: segment.description, constraints: JSON.stringify(segment.constraints), }) .returning(COLUMNS); return this.mapRow(rows[0]); } delete(id: number): Promise<void> { return this.db(T.segments).where({ id }).del(); } async getAll(): Promise<ISegment[]> { const rows: ISegmentRow[] = await this.db .select(this.prefixColumns()) .from(T.segments) .orderBy('name', 'asc'); return rows.map(this.mapRow); } async getActive(): Promise<ISegment[]> { const rows: ISegmentRow[] = await this.db .distinct(this.prefixColumns()) .from(T.segments) .orderBy('name', 'asc') .join( T.featureStrategySegment, `${T.featureStrategySegment}.segment_id`, `${T.segments}.id`, ); return rows.map(this.mapRow); } async getByStrategy(strategyId: string): Promise<ISegment[]> { const rows = await this.db .select(this.prefixColumns()) .from<ISegmentRow>(T.segments) .join( T.featureStrategySegment, `${T.featureStrategySegment}.segment_id`, `${T.segments}.id`, ) .where( `${T.featureStrategySegment}.feature_strategy_id`, '=', strategyId, ); return rows.map(this.mapRow); } deleteAll(): Promise<void> { return this.db(T.segments).del(); } async exists(id: number): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS(SELECT 1 FROM ${T.segments} WHERE id = ?) AS present`, [id], ); return result.rows[0].present; } async get(id: number): Promise<ISegment> { const rows: ISegmentRow[] = await this.db .select(this.prefixColumns()) .from(T.segments) .where({ id }); return this.mapRow(rows[0]); } async addToStrategy(id: number, strategyId: string): Promise<void> { await this.db(T.featureStrategySegment).insert({ segment_id: id, feature_strategy_id: strategyId, }); } async removeFromStrategy(id: number, strategyId: string): Promise<void> { await this.db(T.featureStrategySegment) .where({ segment_id: id, feature_strategy_id: strategyId }) .del(); } async getAllFeatureStrategySegments(): Promise<IFeatureStrategySegment[]> { const rows: IFeatureStrategySegmentRow[] = await this.db .select(['segment_id', 'feature_strategy_id']) .from(T.featureStrategySegment); return rows.map((row) => ({ featureStrategyId: row.feature_strategy_id, segmentId: row.segment_id, })); } async existsByName(name: string): Promise<boolean> { const rows: ISegmentRow[] = await this.db .select(this.prefixColumns()) .from(T.segments) .where({ name }); return Boolean(rows[0]); } prefixColumns(): string[] { return COLUMNS.map((c) => `${T.segments}.${c}`); } mapRow(row?: ISegmentRow): ISegment { Iif (!row) { throw new NotFoundError('No row'); } return { id: row.id, name: row.name, description: row.description, constraints: row.constraints, createdBy: row.created_by, createdAt: row.created_at, }; } destroy(): void {} } |