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 | 69x 69x 69x 69x 69x 380x 9x 69x 69x 87x 87x 87x 3x 3x 3x 5x 5x 3x 2x 128x 128x 40x 128x 128x 12x 12x 12x 10x 2x 158x 158x 3x 14x 3x 3x 3x | import EventEmitter from 'events'; import { Knex } from 'knex'; import { Logger, LogProvider } from '../logger'; import metricsHelper from '../util/metrics-helper'; import { DB_TIME } from '../metric-events'; import { IEnvironment, IEnvironmentCreate } from '../types/model'; import NotFoundError from '../error/notfound-error'; import { IEnvironmentStore } from '../types/stores/environment-store'; import { snakeCaseKeys } from '../util/snakeCase'; interface IEnvironmentsTable { name: string; created_at?: Date; type: string; sort_order: number; enabled: boolean; protected: boolean; } const COLUMNS = [ 'type', 'name', 'created_at', 'sort_order', 'enabled', 'protected', ]; function mapRow(row: IEnvironmentsTable): IEnvironment { return { name: row.name, type: row.type, sortOrder: row.sort_order, enabled: row.enabled, protected: row.protected, }; } function fieldToRow(env: IEnvironment): IEnvironmentsTable { return { name: env.name, type: env.type, sort_order: env.sortOrder, enabled: env.enabled, protected: env.protected, }; } const TABLE = 'environments'; export default class EnvironmentStore implements IEnvironmentStore { private logger: Logger; private db: Knex; private timer: (string) => any; constructor(db: Knex, eventBus: EventEmitter, getLogger: LogProvider) { this.db = db; this.logger = getLogger('db/environment-store.ts'); this.timer = (action) => metricsHelper.wrapTimer(eventBus, DB_TIME, { store: 'environment', action, }); } async importEnvironments( environments: IEnvironment[], ): Promise<IEnvironment[]> { const rows = await this.db(TABLE) .insert(environments.map(fieldToRow)) .returning(COLUMNS) .onConflict('name') .ignore(); return rows.map(mapRow); } async deleteAll(): Promise<void> { await this.db(TABLE).del(); } async get(key: string): Promise<IEnvironment> { const row = await this.db<IEnvironmentsTable>(TABLE) .where({ name: key }) .first(); if (row) { return mapRow(row); } throw new NotFoundError(`Could not find environment with name: ${key}`); } async getAll(query?: Object): Promise<IEnvironment[]> { let qB = this.db<IEnvironmentsTable>(TABLE) .select('*') .orderBy('sort_order', 'created_at'); if (query) { qB = qB.where(query); } const rows = await qB; return rows.map(mapRow); } async exists(name: string): Promise<boolean> { const result = await this.db.raw( `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`, [name], ); const { present } = result.rows[0]; return present; } async getByName(name: string): Promise<IEnvironment> { const row = await this.db<IEnvironmentsTable>(TABLE) .where({ name }) .first(); Iif (!row) { throw new NotFoundError( `Could not find environment with name ${name}`, ); } return mapRow(row); } async updateProperty( id: string, field: string, value: string | number, ): Promise<void> { await this.db<IEnvironmentsTable>(TABLE) .update({ [field]: value, }) .where({ name: id, protected: false }); } async updateSortOrder(id: string, value: number): Promise<void> { await this.db<IEnvironmentsTable>(TABLE) .update({ sort_order: value, }) .where({ name: id }); } async update( env: Pick<IEnvironment, 'type' | 'protected'>, name: string, ): Promise<IEnvironment> { const updatedEnv = await this.db<IEnvironmentsTable>(TABLE) .update(snakeCaseKeys(env)) .where({ name, protected: false }) .returning<IEnvironmentsTable>(COLUMNS); return mapRow(updatedEnv[0]); } async create(env: IEnvironmentCreate): Promise<IEnvironment> { const row = await this.db<IEnvironmentsTable>(TABLE) .insert(snakeCaseKeys(env)) .returning<IEnvironmentsTable>(COLUMNS); return mapRow(row[0]); } async disable(environments: IEnvironment[]): Promise<void> { await this.db(TABLE) .update({ enabled: false, }) .whereIn( 'name', environments.map((env) => env.name), ); } async enable(environments: IEnvironment[]): Promise<void> { await this.db(TABLE) .update({ enabled: true, }) .whereIn( 'name', environments.map((env) => env.name), ); } async delete(name: string): Promise<void> { await this.db(TABLE).where({ name, protected: false }).del(); } destroy(): void {} } |