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 | 69x 69x 69x 69x 69x 52x 52x 520x 211x 52x 69x 87x 87x 19x 19x 5x 5x 1x 1x 1x 11x 11x 11x 2x 2x 2x 2x 10x 10x 6x 6x 2x 1x 1x 1x | import EventEmitter from 'events'; import { Knex } from 'knex'; import NotFoundError from '../error/notfound-error'; import { IClientApplication, IClientApplicationsStore, } from '../types/stores/client-applications-store'; import { Logger, LogProvider } from '../logger'; import { IApplicationQuery } from '../types/query'; const COLUMNS = [ 'app_name', 'created_at', 'created_by', 'updated_at', 'description', 'strategies', 'url', 'color', 'icon', ]; const TABLE = 'client_applications'; const mapRow: (any) => IClientApplication = (row) => ({ appName: row.app_name, createdAt: row.created_at, updatedAt: row.updated_at, description: row.description, strategies: row.strategies || [], createdBy: row.created_by, url: row.url, color: row.color, icon: row.icon, lastSeen: row.last_seen, announced: row.announced, }); const remapRow = (input) => { const temp = { app_name: input.appName, updated_at: input.updatedAt || new Date(), seen_at: input.lastSeen || new Date(), description: input.description, created_by: input.createdBy, announced: input.announced, url: input.url, color: input.color, icon: input.icon, strategies: JSON.stringify(input.strategies), }; Object.keys(temp).forEach((k) => { if (temp[k] === undefined) { // not using !temp[k] to allow false and null values to get through delete temp[k]; } }); return temp; }; export default class ClientApplicationsStore implements IClientApplicationsStore { private db: Knex; private logger: Logger; constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { this.db = db; this.logger = getLogger('client-applications-store.ts'); } async upsert(details: Partial<IClientApplication>): Promise<void> { const row = remapRow(details); await this.db(TABLE).insert(row).onConflict('app_name').merge(); } async bulkUpsert(apps: Partial<IClientApplication>[]): Promise<void> { const rows = apps.map(remapRow); await this.db(TABLE).insert(rows).onConflict('app_name').merge(); } async exists(appName: string): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE app_name = ?) AS present`, [appName], ); const { present } = result.rows[0]; return present; } async getAll(): Promise<IClientApplication[]> { const rows = await this.db .select(COLUMNS) .from(TABLE) .orderBy('app_name', 'asc'); return rows.map(mapRow); } async getApplication(appName: string): Promise<IClientApplication> { const row = await this.db .select(COLUMNS) .where('app_name', appName) .from(TABLE) .first(); Iif (!row) { throw new NotFoundError(`Could not find appName=${appName}`); } return mapRow(row); } async deleteApplication(appName: string): Promise<void> { return this.db(TABLE).where('app_name', appName).del(); } /** * Could also be done in SQL: * (not sure if it is faster though) * * SELECT app_name from ( * SELECT app_name, json_array_elements(strategies)::text as strategyName from client_strategies * ) as foo * WHERE foo.strategyName = '"other"'; */ async getAppsForStrategy( query: IApplicationQuery, ): Promise<IClientApplication[]> { const rows = await this.db.select(COLUMNS).from(TABLE); const apps = rows.map(mapRow); Iif (query.strategyName) { return apps.filter((app) => app.strategies.includes(query.strategyName), ); } return apps; } async getUnannounced(): Promise<IClientApplication[]> { const rows = await this.db(TABLE) .select(COLUMNS) .where('announced', false); return rows.map(mapRow); } /** * * Updates all rows that have announced = false to announced =true and returns the rows altered * @return {[app]} - Apps that hadn't been announced */ async setUnannouncedToAnnounced(): Promise<IClientApplication[]> { const rows = await this.db(TABLE) .update({ announced: true }) .where('announced', false) .whereNotNull('announced') .returning(COLUMNS); return rows.map(mapRow); } async delete(key: string): Promise<void> { await this.db(TABLE).where('app_name', key).del(); } async deleteAll(): Promise<void> { await this.db(TABLE).del(); } destroy(): void {} async get(appName: string): Promise<IClientApplication> { const row = await this.db .select(COLUMNS) .where('app_name', appName) .from(TABLE) .first(); Iif (!row) { throw new NotFoundError(`Could not find appName=${appName}`); } return mapRow(row); } } |