-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
segment-store.ts
261 lines (225 loc) · 7.62 KB
/
segment-store.ts
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { ISegmentStore } from '../types/stores/segment-store';
import { IConstraint, IFeatureStrategySegment, ISegment } from '../types/model';
import { Logger, LogProvider } from '../logger';
import EventEmitter from 'events';
import NotFoundError from '../error/notfound-error';
import { PartialSome } from '../types/partial';
import User from '../types/user';
import { Db } from './db';
import { IFlagResolver } from '../types';
const T = {
segments: 'segments',
featureStrategies: 'feature_strategies',
featureStrategySegment: 'feature_strategy_segment',
};
const COLUMNS = [
'id',
'name',
'description',
'segment_project_id',
'created_by',
'created_at',
'constraints',
];
interface ISegmentRow {
id: number;
name: string;
description?: string;
segment_project_id?: string;
created_by?: string;
created_at: Date;
used_in_projects?: number;
used_in_features?: number;
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: Db;
private flagResolver: IFlagResolver;
constructor(
db: Db,
eventBus: EventEmitter,
getLogger: LogProvider,
flagResolver: IFlagResolver,
) {
this.db = db;
this.eventBus = eventBus;
this.flagResolver = flagResolver;
this.logger = getLogger('lib/db/segment-store.ts');
}
async count(): Promise<number> {
return this.db
.from(T.segments)
.count('*')
.then((res) => Number(res[0].count));
}
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,
segment_project_id: segment.project || null,
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,
segment_project_id: segment.project || null,
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[]> {
if (this.flagResolver.isEnabled('segmentContextFieldUsage')) {
const rows: ISegmentRow[] = await this.db
.select(
this.prefixColumns(),
'used_in_projects',
'used_in_features',
)
.countDistinct(
`${T.featureStrategies}.project_name AS used_in_projects`,
)
.countDistinct(
`${T.featureStrategies}.feature_name AS used_in_features`,
)
.from(T.segments)
.leftJoin(
T.featureStrategySegment,
`${T.segments}.id`,
`${T.featureStrategySegment}.segment_id`,
)
.leftJoin(
T.featureStrategies,
`${T.featureStrategies}.id`,
`${T.featureStrategySegment}.feature_strategy_id`,
)
.groupBy(this.prefixColumns())
.orderBy('name', 'asc');
return rows.map(this.mapRow);
} else {
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 {
if (!row) {
throw new NotFoundError('No row');
}
return {
id: row.id,
name: row.name,
description: row.description,
project: row.segment_project_id || undefined,
constraints: row.constraints,
createdBy: row.created_by,
createdAt: row.created_at,
...(row.used_in_projects && {
usedInProjects: Number(row.used_in_projects),
}),
...(row.used_in_features && {
usedInFeatures: Number(row.used_in_features),
}),
};
}
destroy(): void {}
}