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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 69x 69x 69x 69x 69x 69x 87x 87x 87x 434x 1x 1x 1x 1x 1x 1x 14x 14x 396x 396x 396x 396x 26x 26x 4x 4x 22x 22x 10x 10x 10x 3x 3x 3x 2x 2x 2x 2x 6x 6x 6x 6x 28x 69x 69x | import { Knex } from 'knex'; import { EventEmitter } from 'stream'; import { Logger, LogProvider } from '../logger'; import { ITag } from '../types/model'; import metricsHelper from '../util/metrics-helper'; import { DB_TIME } from '../metric-events'; import { UNIQUE_CONSTRAINT_VIOLATION } from '../error/db-error'; import FeatureHasTagError from '../error/feature-has-tag-error'; import { IFeatureAndTag, IFeatureTag, IFeatureTagStore, } from '../types/stores/feature-tag-store'; const COLUMNS = ['feature_name', 'tag_type', 'tag_value']; const TABLE = 'feature_tag'; interface FeatureTagTable { feature_name: string; tag_type: string; tag_value: string; } class FeatureTagStore implements IFeatureTagStore { private db: Knex; private logger: Logger; private readonly timer: Function; constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { this.db = db; this.logger = getLogger('feature-tag-store.ts'); this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'feature-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, }; } 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, })); } async getAllTagsForFeature(featureName: string): Promise<ITag[]> { const stopTimer = this.timer('getAllForFeature'); const rows = await this.db .select(COLUMNS) .from<FeatureTagTable>(TABLE) .where({ feature_name: featureName }); stopTimer(); return rows.map(this.featureTagRowToTag); } async tagFeature(featureName: string, tag: ITag): Promise<ITag> { const stopTimer = this.timer('tagFeature'); await this.db<FeatureTagTable>(TABLE) .insert(this.featureAndTagToRow(featureName, tag)) .catch((err) => { if (err.code === UNIQUE_CONSTRAINT_VIOLATION) { throw new FeatureHasTagError( `${featureName} already had the tag: [${tag.type}:${tag.value}]`, ); } else E{ throw err; } }); stopTimer(); return tag; } /** * Only gets tags for active feature toggles. */ 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, })); } async deleteAll(): Promise<void> { const stopTimer = this.timer('deleteAll'); await this.db(TABLE).del(); stopTimer(); } async importFeatureTags( featureTags: IFeatureTag[], ): Promise<IFeatureAndTag[]> { const rows = await this.db(TABLE) .insert(featureTags.map(this.importToRow)) .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(this.featureAndTagToRow(featureName, tag)) .delete(); } catch (err) { this.logger.error(err); } stopTimer(); } featureTagRowToTag(row: FeatureTagTable): ITag { if (row) { return { value: row.tag_value, type: row.tag_type, }; } return null; } rowToFeatureAndTag(row: FeatureTagTable): IFeatureAndTag { return { featureName: row.feature_name, tag: { type: row.tag_type, value: row.tag_value, }, }; } importToRow({ featureName, tagType, tagValue, }: IFeatureTag): FeatureTagTable { return { feature_name: featureName, tag_type: tagType, tag_value: tagValue, }; } featureAndTagToRow( featureName: string, { type, value }: ITag, ): FeatureTagTable { return { feature_name: featureName, tag_type: type, tag_value: value, }; } } module.exports = FeatureTagStore; export default FeatureTagStore; |