-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
Copy pathfeature-tag-store.ts
288 lines (259 loc) · 8.22 KB
/
feature-tag-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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import type { Logger, LogProvider } from '../logger';
import type { ITag } from '../types';
import type EventEmitter from 'events';
import metricsHelper from '../util/metrics-helper';
import { DB_TIME } from '../metric-events';
import type {
IFeatureAndTag,
IFeatureTag,
IFeatureTagInsert,
IFeatureTagStore,
} from '../types/stores/feature-tag-store';
import type { Db } from './db';
import NotFoundError from '../error/notfound-error';
const COLUMNS = ['feature_name', 'tag_type', 'tag_value'];
const TABLE = 'feature_tag';
interface FeatureTagTable {
feature_name: string;
tag_type: string;
tag_value: string;
created_by_user_id?: number;
}
class FeatureTagStore implements IFeatureTagStore {
private db: Db;
private logger: Logger;
private readonly timer: Function;
constructor(db: Db, eventBus: EventEmitter, getLogger: LogProvider) {
this.db = db;
this.logger = getLogger('feature-tag-store.ts');
this.timer = (action) =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'feature-tag-toggle',
action,
});
}
async delete({
featureName,
tagType,
tagValue,
}: IFeatureTag): Promise<void> {
await this.db(TABLE)
.where({
feature_name: featureName,
tag_type: tagType,
tag_value: tagValue,
})
.del();
}
destroy(): void {}
async exists({
featureName,
tagType,
tagValue,
}: IFeatureTag): Promise<boolean> {
const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE feature_name = ? AND tag_type = ? AND tag_value = ?) AS present`,
[featureName, tagType, tagValue],
);
const { present } = result.rows[0];
return present;
}
async get({
featureName,
tagType,
tagValue,
}: IFeatureTag): Promise<IFeatureTag> {
const row = await this.db(TABLE)
.where({
feature_name: featureName,
tag_type: tagType,
tag_value: tagValue,
})
.first();
return {
featureName: row.feature_name,
tagType: row.tag_type,
tagValue: row.tag_value,
createdByUserId: row.created_by_user_id,
};
}
async getAll(): Promise<IFeatureTag[]> {
const rows = await this.db(TABLE).select(COLUMNS);
return rows.map((row) => ({
featureName: row.feature_name,
tagType: row.tag_type,
tagValue: row.tag_value,
createdByUserId: row.created_by_user_id,
}));
}
async getAllTagsForFeature(featureName: string): Promise<ITag[]> {
const stopTimer = this.timer('getAllForFeature');
if (await this.featureExists(featureName)) {
const rows = await this.db
.select([...COLUMNS, 'tag_types.color as color'])
.from<FeatureTagTable>(TABLE)
.leftJoin('tag_types', 'tag_types.name', 'feature_tag.tag_type')
.where({ feature_name: featureName });
stopTimer();
return rows.map((row) => ({
type: row.tag_type,
value: row.tag_value,
color: row.color,
}));
} else {
throw new NotFoundError(
`Could not find feature with name ${featureName}`,
);
}
}
async getAllFeaturesForTag(tagValue: string): Promise<string[]> {
const rows = await this.db
.select('feature_name')
.from<FeatureTagTable>(TABLE)
.where({ tag_value: tagValue });
return rows.map(({ feature_name }) => feature_name);
}
async featureExists(featureName: string): Promise<boolean> {
const result = await this.db.raw(
'SELECT EXISTS (SELECT 1 FROM features WHERE name = ?) AS present',
[featureName],
);
const { present } = result.rows[0];
return present;
}
async getAllByFeatures(features: string[]): Promise<IFeatureTag[]> {
const query = this.db
.select(COLUMNS)
.from<FeatureTagTable>(TABLE)
.whereIn('feature_name', features)
.orderBy('feature_name', 'asc');
const rows = await query;
return rows.map((row) => ({
featureName: row.feature_name,
tagType: row.tag_type,
tagValue: row.tag_value,
createdByUserId: row.created_by_user_id,
}));
}
async tagFeature(
featureName: string,
tag: ITag,
createdByUserId: number,
): Promise<ITag> {
const stopTimer = this.timer('tagFeature');
await this.db<FeatureTagTable>(TABLE)
.insert(this.featureAndTagToRow(featureName, tag, createdByUserId))
.onConflict(COLUMNS)
.merge();
stopTimer();
return tag;
}
async untagFeatures(featureTags: IFeatureTag[]): Promise<void> {
const stopTimer = this.timer('untagFeatures');
try {
await this.db(TABLE)
.whereIn(COLUMNS, featureTags.map(this.featureTagArray))
.delete();
} catch (err) {
this.logger.error(err);
}
stopTimer();
}
/**
* Only gets tags for active feature flags.
*/
async getAllFeatureTags(): Promise<IFeatureTag[]> {
const rows = await this.db(TABLE)
.select(COLUMNS)
.whereIn(
'feature_name',
this.db('features').where({ archived: false }).select(['name']),
);
return rows.map((row) => ({
featureName: row.feature_name,
tagType: row.tag_type,
tagValue: row.tag_value,
createdByUserId: row.created_by_user_id,
}));
}
async deleteAll(): Promise<void> {
const stopTimer = this.timer('deleteAll');
await this.db(TABLE).del();
stopTimer();
}
async tagFeatures(
featureTags: IFeatureTagInsert[],
): Promise<IFeatureAndTag[]> {
if (featureTags.length !== 0) {
const rows = await this.db(TABLE)
.insert(featureTags.map(this.featureTagToRow))
.returning(COLUMNS)
.onConflict(COLUMNS)
.ignore();
if (rows) {
return rows.map(this.rowToFeatureAndTag);
}
}
return [];
}
async untagFeature(featureName: string, tag: ITag): Promise<void> {
const stopTimer = this.timer('untagFeature');
try {
await this.db(TABLE)
.where({
feature_name: featureName,
tag_type: tag.type,
tag_value: tag.value,
})
.delete();
} catch (err) {
this.logger.error(err);
}
stopTimer();
}
featureTagRowToTag(row: FeatureTagTable): ITag {
return {
value: row.tag_value,
type: row.tag_type,
};
}
rowToFeatureAndTag(row: FeatureTagTable): IFeatureAndTag {
return {
featureName: row.feature_name,
tag: {
type: row.tag_type,
value: row.tag_value,
},
};
}
featureTagToRow({
featureName,
tagType,
tagValue,
createdByUserId,
}: IFeatureTagInsert): FeatureTagTable {
return {
feature_name: featureName,
tag_type: tagType,
tag_value: tagValue,
created_by_user_id: createdByUserId,
};
}
featureTagArray({ featureName, tagType, tagValue }: IFeatureTag): string[] {
return [featureName, tagType, tagValue];
}
featureAndTagToRow(
featureName: string,
{ type, value }: ITag,
createdByUserId: number,
): FeatureTagTable {
return {
feature_name: featureName,
tag_type: type,
tag_value: value,
created_by_user_id: createdByUserId,
};
}
}
module.exports = FeatureTagStore;
export default FeatureTagStore;