deno.land / x / pg_mem@2.8.1 / adapters / adapters.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { LibAdapters, IMemoryDb, NotSupported, QueryResult } from '../interfaces.ts';import lru from 'https://deno.land/x/lru_cache@6.0.0-deno.4/mod.ts';import { compareVersions } from '../utils.ts';import { toLiteral } from '../misc/pg-utils.ts';import { _IType } from '../interfaces-private.ts';import { TYPE_SYMBOL } from '../execution/select.ts';import { ArrayType } from '../datatypes/index.ts';import { CustomEnumType } from '../datatypes/t-custom-enum.ts';declare var __non_webpack_require__: any;

// setImmediate does not exist in Denodeclare var setImmediate: any;
// see https://github.com/oguimbal/pg-mem/issues/170function timeoutOrImmediate(fn: () => void, time: number) { if (time || typeof setImmediate === 'undefined') { return setTimeout(fn, time); } // nothing to wait for, but still executing "later" // in case calling code relies on some actual async behavior return setImmediate(fn);}
const delay = (time: number | undefined) => new Promise<void>(done => timeoutOrImmediate(done, time ?? 0));
function replaceQueryArgs$(this: void, sql: string, values: any[]) { return sql.replace(/\$(\d+)/g, (str: any, istr: any) => { const i = Number.parseInt(istr); if (i > values.length) { throw new Error('Unmatched parameter in query ' + str); } const val = values[i - 1]; return toLiteral(val); });}

export class Adapters implements LibAdapters { private _mikroPatched?: boolean;
constructor(private db: IMemoryDb) { }
createPg(queryLatency?: number): { Pool: any; Client: any } { const that = this; // https://node-postgres.com/features/queries interface PgQuery { text: string; values?: any[]; rowMode?: 'array'; types?: any; } class MemPg {
connection = this;
on() { // nop }
release() { }
removeListener() { }
once(what: string, handler: () => void) { if (what === 'connect') { timeoutOrImmediate(handler, queryLatency ?? 0); } }
end(callback: any) { if (callback) { callback(); return null; } else { return Promise.resolve(); } }
connect(callback: any) { if (callback) { callback(null, this, () => { }); return null; } else { return Promise.resolve(this); } } query(query: any, valuesOrCallback: any, callback: any) { let values: any = null; if (Array.isArray(valuesOrCallback)) { values = valuesOrCallback; } if (callback == null && typeof valuesOrCallback === 'function') { callback = valuesOrCallback; }
// adapt results
const pgquery = this.adaptQuery(query, values); try { const result = this.adaptResults(query, that.db.public.query(pgquery.text)); if (callback) { timeoutOrImmediate(() => callback(null, result), queryLatency ?? 0); return null; } else { return new Promise(res => timeoutOrImmediate(() => res(result), queryLatency ?? 0)); } } catch (e) { if (callback) { timeoutOrImmediate(() => callback(e), queryLatency ?? 0); return null; } else { return new Promise((_, rej) => timeoutOrImmediate(() => rej(e), queryLatency ?? 0)); } } }
private adaptResults(query: PgQuery, res: QueryResult) { if (query.rowMode) { throw new NotSupported('pg rowMode'); } return { ...res, // clone rows to avoid leaking symbols rows: res.rows.map(row => { const rowCopy: any = {}; // copy all for (const [k, v] of Object.entries(row)) { rowCopy[k] = v; } // ...but amend fields based on their types for (const f of res.fields) { const type = (f as any)[TYPE_SYMBOL] as _IType; const value = row[f.name]; // enum arrays are returned as strings... see #224 if (type instanceof ArrayType && type.of instanceof CustomEnumType && Array.isArray(value)) { rowCopy[f.name] = `{${value.join(',')}}`; } } return rowCopy; }), get fields() { // to implement if needed ? (never seen a lib that uses it) return []; } }; }
private adaptQuery(query: string | PgQuery, values: any): PgQuery { if (typeof query === 'string') { query = { text: query, values, }; } else { // clean copy to avoid mutating things outside our scope query = { ...query }; } if (!query.values?.length) { return query; }
if (query.types?.getTypeParser) { throw new NotSupported('getTypeParser is not supported'); }
// console.log(query); // console.log('\n');
query.text = replaceQueryArgs$(query.text, query.values); return query; } } return { Pool: MemPg, Client: MemPg, }; }
/** * @deprecated Use `createTypeormDataSource` instead. */ createTypeormConnection(postgresOptions: any, queryLatency?: number) { const that = this; (postgresOptions as any).postgres = that.createPg(queryLatency); if (postgresOptions?.type !== 'postgres') { throw new NotSupported('Only postgres supported, found ' + postgresOptions?.type ?? '<null>') }
const { getConnectionManager } = __non_webpack_require__('typeorm') const created = getConnectionManager().create(postgresOptions); created.driver.postgres = that.createPg(queryLatency); return created.connect(); }
createTypeormDataSource(postgresOptions: any, queryLatency?: number) { const that = this; (postgresOptions as any).postgres = that.createPg(queryLatency); if (postgresOptions?.type !== 'postgres') { throw new NotSupported('Only postgres supported, found ' + postgresOptions?.type ?? '<null>') }
const nr = __non_webpack_require__('typeorm'); const { DataSource } = nr; const created = new DataSource(postgresOptions); created.driver.postgres = that.createPg(queryLatency); return created; }
createSlonik(queryLatency?: number) { const { createMockPool, createMockQueryResult } = __non_webpack_require__('slonik'); return createMockPool({ query: async (sql: string, args: any[]) => { await delay(queryLatency ?? 0); const formatted = replaceQueryArgs$(sql, args); const ret = this.db.public.many(formatted); return createMockQueryResult(ret); }, }); }

createPgPromise(queryLatency?: number) { // https://vitaly-t.github.io/pg-promise/module-pg-promise.html // https://github.com/vitaly-t/pg-promise/issues/743#issuecomment-756110347 const pgp = __non_webpack_require__('pg-promise')(); pgp.pg = this.createPg(queryLatency); const db = pgp('pg-mem'); if (compareVersions('10.8.7', db.$config.version) < 0) { throw new Error(`💀 pg-mem cannot be used with pg-promise@${db.$config.version},
👉 you must install version pg-promise@10.8.7 or newer:
npm i pg-promise@latest -S
See https://github.com/vitaly-t/pg-promise/issues/743 for details`); } return db; }
createPgNative(queryLatency?: number) { queryLatency = queryLatency ?? 0; const prepared = new lru<string, string>({ max: 1000, maxAge: 5000, }); function handlerFor(a: any, b: any) { return typeof a === 'function' ? a : b; } const that = this; return class Client { async connect(a: any, b: any) { const handler = handlerFor(a, b); await delay(queryLatency); handler?.(); }
connectSync() { // nop }
async prepare(name: string, sql: string, npar: number, callback: any) { await delay(queryLatency); this.prepareSync(name, sql, npar); callback(); }
prepareSync(name: string, sql: string, npar: number) { prepared.set(name, sql); }
async execute(name: string, a: any, b: any) { const handler = handlerFor(a, b); const pars = Array.isArray(a) ? a : []; await delay(queryLatency); try { const rows = this.executeSync(name, pars); handler(null, rows); } catch (e) { handler(e); } } executeSync(name: string, pars?: any) { pars = Array.isArray(pars) ? pars : []; const prep = prepared.get(name); if (!prep) { throw new Error('Unkown prepared statement ' + name); } return this.querySync(prep, pars); }

async query(sql: string, b: any, c: any) { const handler = handlerFor(b, c); const params = Array.isArray(b) ? b : []; try { await delay(queryLatency); const result = this.querySync(sql, params); handler(null, result); } catch (e) { handler?.(e); } }
querySync(sql: string, params: any[]) { sql = replaceQueryArgs$(sql, params); const ret = that.db.public.many(sql); return ret; } } }
createKnex(queryLatency?: number, knexConfig?: object): any { const knex = __non_webpack_require__('knex')({ connection: {}, ...knexConfig, client: 'pg', }); knex.client.driver = this.createPg(queryLatency); knex.client.version = 'pg-mem'; return knex; }
createKysely(queryLatency?: number, kyselyConfig?: object): any { const { Kysely, PostgresDialect } = __non_webpack_require__('kysely'); const pg = this.createPg(queryLatency); return new Kysely({ ...kyselyConfig, dialect: new PostgresDialect({ pool: new pg.Pool(), }), }); }
async createMikroOrm(mikroOrmOptions: any, queryLatency?: number) {
const { MikroORM } = __non_webpack_require__('@mikro-orm/core'); const { AbstractSqlDriver, PostgreSqlConnection, PostgreSqlPlatform } = __non_webpack_require__('@mikro-orm/postgresql'); const that = this;
// see https://github.com/mikro-orm/mikro-orm/blob/aa71065d0727920db7da9bfdecdb33e6b8165cb5/packages/postgresql/src/PostgreSqlConnection.ts#L5 class PgMemConnection extends PostgreSqlConnection { protected createKnexClient(type: string) { return that.createKnex(); }
} // see https://github.com/mikro-orm/mikro-orm/blob/master/packages/postgresql/src/PostgreSqlDriver.ts class PgMemDriver extends AbstractSqlDriver<PgMemConnection> { constructor(config: any) { super(config, new PostgreSqlPlatform(), PgMemConnection, ['knex', 'pg']); } }
const orm = await MikroORM.init({ ...mikroOrmOptions, dbName: 'public', driver: PgMemDriver, }); return orm; }
}
pg_mem

Version Info

Tagged at
4 months ago