@juit/pgproxy-persister 1.3.7 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/model.cjs +31 -45
- package/dist/model.cjs.map +1 -1
- package/dist/model.mjs +19 -33
- package/dist/model.mjs.map +1 -1
- package/dist/search.cjs +215 -0
- package/dist/search.cjs.map +6 -0
- package/dist/search.d.ts +211 -0
- package/dist/search.mjs +189 -0
- package/dist/search.mjs.map +6 -0
- package/dist/utils.cjs +53 -0
- package/dist/utils.cjs.map +6 -0
- package/dist/utils.d.ts +4 -0
- package/dist/utils.mjs +25 -0
- package/dist/utils.mjs.map +6 -0
- package/package.json +3 -3
- package/src/index.ts +1 -0
- package/src/model.ts +21 -51
- package/src/search.ts +587 -0
- package/src/utils.ts +33 -0
package/src/model.ts
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
1
|
import { escape } from '@juit/pgproxy-client'
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import { assert, assertArray, assertObject, encodeSchemaAndName } from './utils'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
* SIMPLE ASSERTIONS *
|
|
7
|
-
* ========================================================================== */
|
|
8
|
-
|
|
9
|
-
function assert(assertion: any, message: string): asserts assertion {
|
|
10
|
-
if (! assertion) throw new Error(message)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function assertArray(value: any, message: string): asserts value is any[] {
|
|
14
|
-
assert(Array.isArray(value), message)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function assertObject(value: any, message: string): asserts value is object {
|
|
18
|
-
assert(value && (typeof value === 'object'), message)
|
|
19
|
-
}
|
|
5
|
+
import type { PGQueryable } from '@juit/pgproxy-client'
|
|
20
6
|
|
|
21
7
|
/* ========================================================================== *
|
|
22
8
|
* TYPE INFERENCE: FROM SCHEMA->TABLE->COLUMN->... TO JS TYPES *
|
|
@@ -249,8 +235,7 @@ function where(
|
|
|
249
235
|
|
|
250
236
|
/** Prepare an `INSERT` statement for a table */
|
|
251
237
|
function insert(
|
|
252
|
-
|
|
253
|
-
table: string,
|
|
238
|
+
etable: string,
|
|
254
239
|
query: Record<string, any>,
|
|
255
240
|
unique: boolean = false,
|
|
256
241
|
): Query {
|
|
@@ -271,16 +256,15 @@ function insert(
|
|
|
271
256
|
|
|
272
257
|
return [
|
|
273
258
|
columns.length == 0 ?
|
|
274
|
-
`INSERT INTO ${
|
|
275
|
-
`INSERT INTO ${
|
|
259
|
+
`INSERT INTO ${etable} DEFAULT VALUES ${returning}` :
|
|
260
|
+
`INSERT INTO ${etable} (${columns.join()}) VALUES (${placeholders.join()}) ${returning}`,
|
|
276
261
|
values,
|
|
277
262
|
]
|
|
278
263
|
}
|
|
279
264
|
|
|
280
265
|
/** Prepare an _upsert_ (`INSERT ... ON CONFLICT`) statement for a table */
|
|
281
266
|
function upsert(
|
|
282
|
-
|
|
283
|
-
table: string,
|
|
267
|
+
etable: string,
|
|
284
268
|
keys: Record<string, any>,
|
|
285
269
|
data: Record<string, any>,
|
|
286
270
|
): Query {
|
|
@@ -320,7 +304,7 @@ function upsert(
|
|
|
320
304
|
|
|
321
305
|
/* Our "upsert" statement */
|
|
322
306
|
return [
|
|
323
|
-
`INSERT INTO ${
|
|
307
|
+
`INSERT INTO ${etable} (${columns.join()}) VALUES (${placeholders.join()}) ` +
|
|
324
308
|
`ON CONFLICT (${conflictKeys.join(',')}) ` +
|
|
325
309
|
`DO UPDATE SET ${updates.join(',')} RETURNING *`,
|
|
326
310
|
values,
|
|
@@ -329,8 +313,7 @@ function upsert(
|
|
|
329
313
|
|
|
330
314
|
/** Prepare a `SELECT` statement for a table */
|
|
331
315
|
function select(
|
|
332
|
-
|
|
333
|
-
table: string,
|
|
316
|
+
etable: string,
|
|
334
317
|
query: Record<string, any>,
|
|
335
318
|
sort: string | string[],
|
|
336
319
|
offset: number,
|
|
@@ -355,7 +338,7 @@ function select(
|
|
|
355
338
|
|
|
356
339
|
const orderby = order.length == 0 ? '' : ` ORDER BY ${order.join(',')}`
|
|
357
340
|
|
|
358
|
-
let sql = `SELECT * FROM ${
|
|
341
|
+
let sql = `SELECT * FROM ${etable}${conditions}${orderby}`
|
|
359
342
|
|
|
360
343
|
if (offset && (offset > 0)) {
|
|
361
344
|
sql += ` OFFSET $${values.length + 1}`
|
|
@@ -372,8 +355,7 @@ function select(
|
|
|
372
355
|
|
|
373
356
|
/** Prepare an `UPDATE` statement for a table */
|
|
374
357
|
function update(
|
|
375
|
-
|
|
376
|
-
table: string,
|
|
358
|
+
etable: string,
|
|
377
359
|
query: Record<string, any>,
|
|
378
360
|
patch: Record<string, any>,
|
|
379
361
|
): Query {
|
|
@@ -389,19 +371,18 @@ function update(
|
|
|
389
371
|
patches.push(`${escape(column)}=$${index}`)
|
|
390
372
|
}
|
|
391
373
|
|
|
392
|
-
if (patches.length === 0) return select(
|
|
374
|
+
if (patches.length === 0) return select(etable, query, [], 0, 0)
|
|
393
375
|
|
|
394
376
|
const [ conditions, , count ] = where(query, values)
|
|
395
377
|
assert(count > 0, 'Cowardly refusing to run UPDATE with empty query')
|
|
396
378
|
|
|
397
|
-
const statement = `UPDATE ${
|
|
379
|
+
const statement = `UPDATE ${etable} SET ${patches.join()}${conditions} RETURNING *`
|
|
398
380
|
return [ statement, values ]
|
|
399
381
|
}
|
|
400
382
|
|
|
401
383
|
/** Prepare a `DELETE` statement for a table */
|
|
402
384
|
function del(
|
|
403
|
-
|
|
404
|
-
table: string,
|
|
385
|
+
etable: string,
|
|
405
386
|
query: Record<string, any>,
|
|
406
387
|
): Query {
|
|
407
388
|
assertObject(query, 'Called DELETE with a non-object query')
|
|
@@ -410,29 +391,18 @@ function del(
|
|
|
410
391
|
|
|
411
392
|
assert(count > 0, 'Cowardly refusing to run DELETE with empty query')
|
|
412
393
|
|
|
413
|
-
return [ `DELETE FROM ${
|
|
394
|
+
return [ `DELETE FROM ${etable}${conditions} RETURNING *`, values ]
|
|
414
395
|
}
|
|
415
396
|
|
|
416
397
|
/* ===== MODEL IMPLEMENTATION =============================================== */
|
|
417
398
|
|
|
418
399
|
class ModelImpl<Table extends Record<string, ColumnDefinition>> implements Model<Table> {
|
|
419
400
|
private _connection: PGQueryable
|
|
420
|
-
private
|
|
421
|
-
private _table: string
|
|
401
|
+
private _etable: string
|
|
422
402
|
|
|
423
403
|
constructor(connection: PGQueryable, name: string) {
|
|
424
404
|
this._connection = connection
|
|
425
|
-
|
|
426
|
-
const [ schemaOrTable, maybeTable, ...extra ] = name.split('.')
|
|
427
|
-
assert(extra.length === 0, `Invalid table name "${name}"`)
|
|
428
|
-
|
|
429
|
-
const [ schema, table ] = maybeTable ?
|
|
430
|
-
[ schemaOrTable, maybeTable ] :
|
|
431
|
-
[ 'public', schemaOrTable ]
|
|
432
|
-
assert(table, `Invalid table name "${name}"`)
|
|
433
|
-
|
|
434
|
-
this._schema = schema || 'public'
|
|
435
|
-
this._table = table
|
|
405
|
+
this._etable = encodeSchemaAndName(name)
|
|
436
406
|
}
|
|
437
407
|
|
|
438
408
|
// Make typescript happy about overloads
|
|
@@ -443,7 +413,7 @@ class ModelImpl<Table extends Record<string, ColumnDefinition>> implements Model
|
|
|
443
413
|
data: InferInsertType<Table>,
|
|
444
414
|
unique: false = false,
|
|
445
415
|
): Promise<InferSelectType<Table> | undefined> {
|
|
446
|
-
const [ sql, params ] = insert(this.
|
|
416
|
+
const [ sql, params ] = insert(this._etable, data, unique)
|
|
447
417
|
const result = await this._connection.query<InferSelectType<Table>>(sql, params)
|
|
448
418
|
return result.rows[0]
|
|
449
419
|
}
|
|
@@ -452,7 +422,7 @@ class ModelImpl<Table extends Record<string, ColumnDefinition>> implements Model
|
|
|
452
422
|
keys: K,
|
|
453
423
|
data: Omit<InferInsertType<Table>, keyof K>,
|
|
454
424
|
): Promise<InferSelectType<Table>> {
|
|
455
|
-
const [ sql, params ] = upsert(this.
|
|
425
|
+
const [ sql, params ] = upsert(this._etable, keys, data)
|
|
456
426
|
const result = await this._connection.query<InferSelectType<Table>>(sql, params)
|
|
457
427
|
return result.rows[0]!
|
|
458
428
|
}
|
|
@@ -463,7 +433,7 @@ class ModelImpl<Table extends Record<string, ColumnDefinition>> implements Model
|
|
|
463
433
|
offset: number = 0,
|
|
464
434
|
limit: number = 0,
|
|
465
435
|
): Promise<InferSelectType<Table>[]> {
|
|
466
|
-
const [ sql, params ] = select(this.
|
|
436
|
+
const [ sql, params ] = select(this._etable, query, sort, offset, limit)
|
|
467
437
|
const result = await this._connection.query<InferSelectType<Table>>(sql, params)
|
|
468
438
|
return result.rows
|
|
469
439
|
}
|
|
@@ -480,7 +450,7 @@ class ModelImpl<Table extends Record<string, ColumnDefinition>> implements Model
|
|
|
480
450
|
query: InferQueryType<Table>,
|
|
481
451
|
patch: InferUpdateType<Table>,
|
|
482
452
|
): Promise<InferSelectType<Table>[]> {
|
|
483
|
-
const [ sql, params ] = update(this.
|
|
453
|
+
const [ sql, params ] = update(this._etable, query, patch)
|
|
484
454
|
const result = await this._connection.query<InferSelectType<Table>>(sql, params)
|
|
485
455
|
return result.rows
|
|
486
456
|
}
|
|
@@ -488,7 +458,7 @@ class ModelImpl<Table extends Record<string, ColumnDefinition>> implements Model
|
|
|
488
458
|
async delete(
|
|
489
459
|
query: InferQueryType<Table>,
|
|
490
460
|
): Promise<number> {
|
|
491
|
-
const [ sql, params ] = del(this.
|
|
461
|
+
const [ sql, params ] = del(this._etable, query)
|
|
492
462
|
const result = await this._connection.query(sql, params)
|
|
493
463
|
return result.rowCount
|
|
494
464
|
}
|