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 | 70x 70x 70x 70x 89x 89x 89x 665x 665x 641x 641x 24x 19x 19x 19x 19x 47x 1x 1x 1x 1x 1x 1x 1x 11x 18x 18x 18x 6x 18x 17x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 905x 712x 70x | import { EventEmitter } from 'events'; import { Knex } from 'knex'; import { DROP_FEATURES, IEvent, IBaseEvent } from '../types/events'; import { LogProvider, Logger } from '../logger'; import { IEventStore } from '../types/stores/event-store'; import { ITag } from '../types/model'; const EVENT_COLUMNS = [ 'id', 'type', 'created_by', 'created_at', 'data', 'pre_data', 'tags', 'feature_name', 'project', 'environment', ]; export interface IEventTable { id: number; type: string; created_by: string; created_at: Date; data?: any; pre_data?: any; feature_name?: string; project?: string; environment?: string; tags: ITag[]; } const TABLE = 'events'; class EventStore extends EventEmitter implements IEventStore { private db: Knex; private logger: Logger; constructor(db: Knex, getLogger: LogProvider) { super(); this.db = db; this.logger = getLogger('lib/db/event-store.ts'); } async store(event: IBaseEvent): Promise<void> { try { const rows = await this.db(TABLE) .insert(this.eventToDbRow(event)) .returning(EVENT_COLUMNS); const savedEvent = this.rowToEvent(rows[0]); process.nextTick(() => this.emit(event.type, savedEvent)); } catch (e) { this.logger.warn(`Failed to store event ${e}`); } } async batchStore(events: IBaseEvent[]): Promise<void> { try { const savedRows = await this.db(TABLE) .insert(events.map(this.eventToDbRow)) .returning(EVENT_COLUMNS); const savedEvents = savedRows.map(this.rowToEvent); process.nextTick(() => savedEvents.forEach((e) => this.emit(e.type, e)), ); } catch (e) { this.logger.warn('Failed to store events'); } } async delete(key: number): Promise<void> { await this.db(TABLE).where({ id: key }).del(); } async deleteAll(): Promise<void> { await this.db(TABLE).del(); } destroy(): void {} async exists(key: number): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`, [key], ); const { present } = result.rows[0]; return present; } async get(key: number): Promise<IEvent> { const row = await this.db(TABLE).where({ id: key }).first(); return this.rowToEvent(row); } async getAll(query?: Object): Promise<IEvent[]> { return this.getEvents(query); } async getEvents(query?: Object): Promise<IEvent[]> { try { let qB = this.db .select(EVENT_COLUMNS) .from(TABLE) .limit(100) .orderBy('created_at', 'desc'); if (query) { qB = qB.where(query); } const rows = await qB; return rows.map(this.rowToEvent); } catch (err) { return []; } } async getEventsFilterByType(name: string): Promise<IEvent[]> { try { const rows = await this.db .select(EVENT_COLUMNS) .from(TABLE) .limit(100) .whereRaw("data ->> 'name' = ?", [name]) .andWhere( 'id', '>=', this.db .select(this.db.raw('coalesce(max(id),0) as id')) .from(TABLE) .where({ type: DROP_FEATURES }), ) .orderBy('created_at', 'desc'); return rows.map(this.rowToEvent); } catch (err) { this.logger.error(err); return []; } } async getEventsFilterByProject(project: string): Promise<IEvent[]> { try { const rows = await this.db .select(EVENT_COLUMNS) .from(TABLE) .where({ project }) .orderBy('created_at', 'desc'); return rows.map(this.rowToEvent); } catch (err) { return []; } } async getEventsForFeature(featureName: string): Promise<IEvent[]> { try { const rows = await this.db .select(EVENT_COLUMNS) .from(TABLE) .where({ feature_name: featureName }) .orderBy('created_at', 'desc'); return rows.map(this.rowToEvent); } catch (err) { return []; } } rowToEvent(row: IEventTable): IEvent { return { id: row.id, type: row.type, createdBy: row.created_by, createdAt: row.created_at, data: row.data, preData: row.pre_data, tags: row.tags || [], featureName: row.feature_name, project: row.project, environment: row.environment, }; } eventToDbRow(e: IBaseEvent): Omit<IEventTable, 'id' | 'created_at'> { return { type: e.type, created_by: e.createdBy, data: e.data, pre_data: e.preData, //@ts-ignore workaround for json-array tags: JSON.stringify(e.tags), feature_name: e.featureName, project: e.project, environment: e.environment, }; } } export default EventStore; |