pg-promise 11.7.7 → 11.8.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/lib/formatting.js CHANGED
@@ -376,14 +376,14 @@ const $as = {
376
376
  * Property name for the $[Custom Type Formatting] flag `rawType`.
377
377
  *
378
378
  * @example
379
- * const ctf = pgp.as.ctf; // Custom Type Formatting symbols
379
+ * const {toPostgres, rawType} = pgp.as.ctf; // Custom Type Formatting symbols
380
380
  *
381
381
  * class MyType {
382
382
  * constructor() {
383
- * this[ctf.rawType] = true; // set it only when toPostgres returns a pre-formatted result
383
+ * this[rawType] = true; // set it only when toPostgres returns a pre-formatted result
384
384
  * }
385
385
  *
386
- * [ctf.toPostgres](self) {
386
+ * [toPostgres](self) {
387
387
  * // self = this
388
388
  *
389
389
  * // return the custom/actual value here
@@ -43,7 +43,7 @@ const npm = {
43
43
  *
44
44
  * @param {object} [options]
45
45
  *
46
- * @param {helpers.TableName|string|{table,schema}} [options.table]
46
+ * @param {helpers.TableName|Table|string} [options.table]
47
47
  * Table details.
48
48
  *
49
49
  * When it is a non-null value, and not a {@link helpers.TableName TableName} object, a new {@link helpers.TableName TableName} is constructed from the value.
@@ -86,8 +86,9 @@ const npm = {
86
86
  * // 6. Property 'amount' needs to be set to 100, if it is 0
87
87
  * // 7. Property 'total' must be skipped during updates, if 'amount' was 0, plus its
88
88
  * // column name is 'total-val'
89
+ * const {ColumnSet} = pgp.helpers;
89
90
  *
90
- * const cs = new pgp.helpers.ColumnSet([
91
+ * const cs = new ColumnSet([
91
92
  * '?id', // ColumnConfig equivalent: {name: 'id', cnd: true}
92
93
  * 'list:csv', // ColumnConfig equivalent: {name: 'list', mod: ':csv'}
93
94
  * {
@@ -386,14 +387,14 @@ ColumnSet.prototype.assign = function (options) {
386
387
  *
387
388
  * @example
388
389
  *
389
- * const cs = new pgp.helpers.ColumnSet(['id', 'city', 'street']);
390
+ * const cs = new ColumnSet(['id', 'city', 'street']);
390
391
  *
391
392
  * cs.assignColumns({from: 'EXCLUDED', skip: 'id'})
392
393
  * //=> "city"=EXCLUDED."city","street"=EXCLUDED."street"
393
394
  *
394
395
  * @example
395
396
  *
396
- * const cs = new pgp.helpers.ColumnSet(['?id', 'city', 'street']);
397
+ * const cs = new ColumnSet(['?id', 'city', 'street']);
397
398
  *
398
399
  * cs.assignColumns({from: 'source', to: 'target', skip: c => c.cnd})
399
400
  * //=> target."city"=source."city",target."street"=source."street"
@@ -495,8 +496,9 @@ ColumnSet.prototype.extend = function (columns) {
495
496
  * @example
496
497
  *
497
498
  * const pgp = require('pg-promise')();
499
+ * const {ColumnSet} = pgp.helpers;
498
500
  *
499
- * const cs = new pgp.helpers.ColumnSet(['?one', 'two:json'], {table: 'my-table'});
501
+ * const cs = new ColumnSet(['?one', 'two:json'], {table: 'my-table'});
500
502
  * console.log(cs);
501
503
  * //=>
502
504
  * // ColumnSet {
@@ -91,7 +91,7 @@ const npm = {
91
91
  * capSQL: true // if you want all generated SQL capitalized
92
92
  * });
93
93
  *
94
- * const Column = pgp.helpers.Column;
94
+ * const {Column} = pgp.helpers;
95
95
  *
96
96
  * // creating a column from just a name:
97
97
  * const col1 = new Column('colName');
@@ -207,7 +207,7 @@ class Column extends InnerState {
207
207
  *
208
208
  * @example
209
209
  *
210
- * const cs = new pgp.helpers.ColumnSet([
210
+ * const cs = new ColumnSet([
211
211
  * 'id',
212
212
  * 'coordinate:json',
213
213
  * {
@@ -42,7 +42,7 @@ const npm = {
42
42
  * When the final {@link helpers.ColumnSet ColumnSet} is empty (no columns in it), the method will throw
43
43
  * {@link external:Error Error} = `Cannot generate an INSERT without any columns.`
44
44
  *
45
- * @param {helpers.TableName|string|{table,schema}} [table]
45
+ * @param {helpers.TableName|Table|string} [table]
46
46
  * Destination table.
47
47
  *
48
48
  * It is normally a required parameter. But when `columns` is passed in as a {@link helpers.ColumnSet ColumnSet} object
@@ -62,29 +62,32 @@ const npm = {
62
62
  * const pgp = require('pg-promise')({
63
63
  * capSQL: true // if you want all generated SQL capitalized
64
64
  * });
65
+ * const {insert} = pgp.helpers;
65
66
  *
66
67
  * const dataSingle = {val: 123, msg: 'hello'};
67
68
  * const dataMulti = [{val: 123, msg: 'hello'}, {val: 456, msg: 'world!'}];
68
69
  *
69
70
  * // Column details can be taken from the data object:
70
71
  *
71
- * pgp.helpers.insert(dataSingle, null, 'my-table');
72
+ * insert(dataSingle, null, 'my-table');
72
73
  * //=> INSERT INTO "my-table"("val","msg") VALUES(123,'hello')
73
74
  *
74
75
  * @example
75
76
  *
76
77
  * // Column details are required for a multi-row `INSERT`:
78
+ * const {insert} = pgp.helpers;
77
79
  *
78
- * pgp.helpers.insert(dataMulti, ['val', 'msg'], 'my-table');
80
+ * insert(dataMulti, ['val', 'msg'], 'my-table');
79
81
  * //=> INSERT INTO "my-table"("val","msg") VALUES(123,'hello'),(456,'world!')
80
82
  *
81
83
  * @example
82
84
  *
83
85
  * // Column details from a reusable ColumnSet (recommended for performance):
86
+ * const {ColumnSet, insert} = pgp.helpers;
84
87
  *
85
- * const cs = new pgp.helpers.ColumnSet(['val', 'msg'], {table: 'my-table'});
88
+ * const cs = new ColumnSet(['val', 'msg'], {table: 'my-table'});
86
89
  *
87
- * pgp.helpers.insert(dataMulti, cs);
90
+ * insert(dataMulti, cs);
88
91
  * //=> INSERT INTO "my-table"("val","msg") VALUES(123,'hello'),(456,'world!')
89
92
  *
90
93
  */
@@ -57,10 +57,11 @@ const npm = {
57
57
  *
58
58
  * // Column details from a reusable ColumnSet (recommended for performance);
59
59
  * // NOTE: Conditional columns (start with '?') are skipped:
60
+ * const {ColumnSet, sets} = pgp.helpers;
60
61
  *
61
- * const cs = new pgp.helpers.ColumnSet(['?id','val', 'msg']);
62
+ * const cs = new ColumnSet(['?id','val', 'msg']);
62
63
  *
63
- * pgp.helpers.sets(data, cs);
64
+ * sets(data, cs);
64
65
  * //=> "val"=123,"msg"='hello'
65
66
  *
66
67
  */
@@ -46,7 +46,7 @@ const npm = {
46
46
  * When the final {@link helpers.ColumnSet ColumnSet} is empty (no columns in it), the method will throw
47
47
  * {@link external:Error Error} = `Cannot generate an UPDATE without any columns.`, unless option `emptyUpdate` was specified.
48
48
  *
49
- * @param {helpers.TableName|string|{table,schema}} [table]
49
+ * @param {helpers.TableName|Table|string} [table]
50
50
  * Table to be updated.
51
51
  *
52
52
  * It is normally a required parameter. But when `columns` is passed in as a {@link helpers.ColumnSet ColumnSet} object
@@ -87,6 +87,7 @@ const npm = {
87
87
  * const pgp = require('pg-promise')({
88
88
  * capSQL: true // if you want all generated SQL capitalized
89
89
  * });
90
+ * const {update} = pgp.helpers;
90
91
  *
91
92
  * const dataSingle = {id: 1, val: 123, msg: 'hello'};
92
93
  * const dataMulti = [{id: 1, val: 123, msg: 'hello'}, {id: 2, val: 456, msg: 'world!'}];
@@ -94,7 +95,7 @@ const npm = {
94
95
  * // Although column details can be taken from the data object, it is not
95
96
  * // a likely scenario for an update, unless updating the whole table:
96
97
  *
97
- * pgp.helpers.update(dataSingle, null, 'my-table');
98
+ * update(dataSingle, null, 'my-table');
98
99
  * //=> UPDATE "my-table" SET "id"=1,"val"=123,"msg"='hello'
99
100
  *
100
101
  * @example
@@ -104,7 +105,7 @@ const npm = {
104
105
  * // Dynamic conditions must be escaped/formatted properly:
105
106
  * const condition = pgp.as.format(' WHERE id = ${id}', dataSingle);
106
107
  *
107
- * pgp.helpers.update(dataSingle, ['val', 'msg'], 'my-table') + condition;
108
+ * update(dataSingle, ['val', 'msg'], 'my-table') + condition;
108
109
  * //=> UPDATE "my-table" SET "val"=123,"msg"='hello' WHERE id = 1
109
110
  *
110
111
  * @example
@@ -112,17 +113,18 @@ const npm = {
112
113
  * // Column details are required for a multi-row `UPDATE`;
113
114
  * // Adding '?' in front of a column name means it is only for a WHERE condition:
114
115
  *
115
- * pgp.helpers.update(dataMulti, ['?id', 'val', 'msg'], 'my-table') + ' WHERE v.id = t.id';
116
+ * update(dataMulti, ['?id', 'val', 'msg'], 'my-table') + ' WHERE v.id = t.id';
116
117
  * //=> UPDATE "my-table" AS t SET "val"=v."val","msg"=v."msg" FROM (VALUES(1,123,'hello'),(2,456,'world!'))
117
118
  * // AS v("id","val","msg") WHERE v.id = t.id
118
119
  *
119
120
  * @example
120
121
  *
121
122
  * // Column details from a reusable ColumnSet (recommended for performance):
123
+ * const {ColumnSet, update} = pgp.helpers;
122
124
  *
123
- * const cs = new pgp.helpers.ColumnSet(['?id', 'val', 'msg'], {table: 'my-table'});
125
+ * const cs = new ColumnSet(['?id', 'val', 'msg'], {table: 'my-table'});
124
126
  *
125
- * pgp.helpers.update(dataMulti, cs) + ' WHERE v.id = t.id';
127
+ * update(dataMulti, cs) + ' WHERE v.id = t.id';
126
128
  * //=> UPDATE "my-table" AS t SET "val"=v."val","msg"=v."msg" FROM (VALUES(1,123,'hello'),(2,456,'world!'))
127
129
  * // AS v("id","val","msg") WHERE v.id = t.id
128
130
  *
@@ -130,16 +132,17 @@ const npm = {
130
132
  *
131
133
  * // Using parameter `options` to change the default alias names:
132
134
  *
133
- * pgp.helpers.update(dataMulti, cs, null, {tableAlias: 'X', valueAlias: 'Y'}) + ' WHERE Y.id = X.id';
135
+ * update(dataMulti, cs, null, {tableAlias: 'X', valueAlias: 'Y'}) + ' WHERE Y.id = X.id';
134
136
  * //=> UPDATE "my-table" AS X SET "val"=Y."val","msg"=Y."msg" FROM (VALUES(1,123,'hello'),(2,456,'world!'))
135
137
  * // AS Y("id","val","msg") WHERE Y.id = X.id
136
138
  *
137
139
  * @example
138
140
  *
139
141
  * // Handling an empty update
142
+ * const {ColumnSet, update} = pgp.helpers;
140
143
  *
141
- * const cs = new pgp.helpers.ColumnSet(['?id', '?name'], {table: 'tt'}); // no actual update-able columns
142
- * const result = pgp.helpers.update(dataMulti, cs, null, {emptyUpdate: 123});
144
+ * const cs = new ColumnSet(['?id', '?name'], {table: 'tt'}); // no actual update-able columns
145
+ * const result = update(dataMulti, cs, null, {emptyUpdate: 123});
143
146
  * if(result === 123) {
144
147
  * // We know the update is empty, i.e. no columns that can be updated;
145
148
  * // And it didn't throw because we specified `emptyUpdate` option.
@@ -72,10 +72,11 @@ const npm = {
72
72
  * @example
73
73
  *
74
74
  * // Column details from a reusable ColumnSet (recommended for performance):
75
+ * const {ColumnSet, values} = pgp.helpers;
75
76
  *
76
- * const cs = new pgp.helpers.ColumnSet(['val', 'msg']);
77
+ * const cs = new ColumnSet(['val', 'msg']);
77
78
  *
78
- * pgp.helpers.values(dataMulti, cs);
79
+ * values(dataMulti, cs);
79
80
  * //=> (123,'hello'),(456,'world!')
80
81
  *
81
82
  */
@@ -26,11 +26,11 @@ const npm = {
26
26
  *
27
27
  * Filter `:alias` is an alternative approach to splitting an SQL name into multiple ones.
28
28
  *
29
- * @param {string|object} table
29
+ * @param {string|Table} table
30
30
  * Table name details, depending on the type:
31
31
  *
32
32
  * - table name, if `table` is a string
33
- * - object `{table, [schema]}`
33
+ * - {@link Table} object
34
34
  *
35
35
  * @property {string} name
36
36
  * Formatted/escaped full table name, combining `schema` + `table`.
@@ -89,8 +89,8 @@ class TableName {
89
89
  * $[Custom Type Formatting], based on $[Symbolic CTF], i.e. the actual method is available only via {@link external:Symbol Symbol}:
90
90
  *
91
91
  * ```js
92
- * const ctf = pgp.as.ctf; // Custom Type Formatting symbols namespace
93
- * const fullName = tn[ctf.toPostgres]; // tn = an object of type TableName
92
+ * const {toPostgres} = pgp.as.ctf; // Custom Type Formatting symbols namespace
93
+ * const fullName = tn[toPostgres]; // tn = an object of type TableName
94
94
  * ```
95
95
  *
96
96
  * This is a raw formatting type (`rawType = true`), i.e. when used as a query-formatting parameter, type `TableName`
@@ -127,13 +127,21 @@ npm.utils.addInspection(TableName, function () {
127
127
  });
128
128
 
129
129
  /**
130
- *
131
130
  * @interface Table
132
131
  * @description
133
132
  * Structure for any table name/path.
134
133
  *
134
+ * Function {@link helpers._TN _TN} can help you construct it from a string.
135
+ *
135
136
  * @property {string} [schema] - schema name, if specified
136
137
  * @property {string} table - table name
138
+ *
139
+ * @see {@link helpers.TableName TableName}, {@link helpers._TN _TN}
140
+ */
141
+
142
+ /**
143
+ * @external TemplateStringsArray
144
+ * @see https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_es5_d_.templatestringsarray.html
137
145
  */
138
146
 
139
147
  /**
@@ -142,6 +150,9 @@ npm.utils.addInspection(TableName, function () {
142
150
  * Table-Name helper function, to convert any `"schema.table"` string
143
151
  * into `{schema, table}` object. It also works as a template-tag function.
144
152
  *
153
+ * @param {string|TemplateStringsArray} path
154
+ * Table-name path, as a simple string or a template string (with parameters).
155
+ *
145
156
  * @example
146
157
  * const {ColumnSet, _TN} = pgp.helpers;
147
158
  *
@@ -153,12 +164,14 @@ npm.utils.addInspection(TableName, function () {
153
164
  * const cs2 = new ColumnSet(['id', 'name'], {table: _TN`${schema}.table`});
154
165
  *
155
166
  * @returns {Table}
167
+ *
168
+ * @see {@link helpers.TableName TableName}, {@link external:TemplateStringsArray TemplateStringsArray}
156
169
  */
157
- function _TN(a, ...args) {
158
- if (Array.isArray(a) && a.raw) {
159
- a = a.map((b, i) => b + (i < args.length ? args[i] ?? '' : '')).join('');
160
- } // else 'a' is a string
161
- const [schema, table] = a.split('.');
170
+ function _TN(path, ...args) {
171
+ if (Array.isArray(path)) {
172
+ path = path.reduce((a, c, i) => a + c + (args[i] ?? ''), '');
173
+ } // else 'path' is a string
174
+ const [schema, table] = path.split('.');
162
175
  if (table === undefined) {
163
176
  return {table: schema};
164
177
  }
package/lib/main.js CHANGED
@@ -36,7 +36,7 @@ let originalClientConnect;
36
36
  * @module pg-promise
37
37
  *
38
38
  * @description
39
- * ## pg-promise v11.7
39
+ * ## pg-promise v11.8
40
40
  * All documentation here is for the latest official release only.
41
41
  *
42
42
  * ### Initialization Options
package/lib/query-file.js CHANGED
@@ -287,8 +287,8 @@ QueryFile.$query = file$query;
287
287
  * $[Custom Type Formatting], based on $[Symbolic CTF], i.e. the actual method is available only via {@link external:Symbol Symbol}:
288
288
  *
289
289
  * ```js
290
- * const ctf = pgp.as.ctf; // Custom Type Formatting symbols namespace
291
- * const query = qf[ctf.toPostgres](); // qf = an object of type QueryFile
290
+ * const {toPostgres} = pgp.as.ctf; // Custom Type Formatting symbols namespace
291
+ * const query = qf[toPostgres](); // qf = an object of type QueryFile
292
292
  * ```
293
293
  *
294
294
  * This is a raw formatting type (`rawType = true`), i.e. when used as a query-formatting parameter, type `QueryFile` injects SQL as raw text.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pg-promise",
3
- "version": "11.7.7",
3
+ "version": "11.8.0",
4
4
  "description": "PostgreSQL interface for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "typings": "typescript/pg-promise.d.ts",
@@ -47,12 +47,12 @@
47
47
  "spex": "3.3.0"
48
48
  },
49
49
  "devDependencies": {
50
- "@eslint/js": "9.3.0",
51
- "@types/node": "20.12.12",
50
+ "@eslint/js": "9.4.0",
51
+ "@types/node": "20.13.0",
52
52
  "bluebird": "3.7.2",
53
53
  "coveralls": "3.1.1",
54
54
  "cspell": "8.8.3",
55
- "eslint": "9.3.0",
55
+ "eslint": "9.4.0",
56
56
  "globals": "15.3.0",
57
57
  "istanbul": "0.4.5",
58
58
  "jasmine-node": "3.0.0",
@@ -349,6 +349,16 @@ declare namespace pgPromise {
349
349
  readonly ctx: ITaskContext
350
350
  }
351
351
 
352
+ interface ITaskIfOptions<Ext = {}> {
353
+ cnd?: boolean | ((t: ITask<Ext> & Ext) => boolean)
354
+ tag?: any
355
+ }
356
+
357
+ interface ITxIfOptions<Ext = {}> extends ITaskIfOptions<Ext> {
358
+ mode?: _txMode.TransactionMode | null
359
+ reusable?: boolean | ((t: ITask<Ext> & Ext) => boolean)
360
+ }
361
+
352
362
  // Base database protocol
353
363
  // API: https://vitaly-t.github.io/pg-promise/Database.html
354
364
  interface IBaseProtocol<Ext> {
@@ -405,46 +415,38 @@ declare namespace pgPromise {
405
415
 
406
416
  // Tasks;
407
417
  // API: https://vitaly-t.github.io/pg-promise/Database.html#task
408
- task<T = any>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
418
+ task<T>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
409
419
 
410
- task<T = any>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
420
+ task<T>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
411
421
 
412
- task<T = any>(options: { tag?: any }, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
422
+ task<T>(options: { tag?: any }, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
413
423
 
414
424
  // Conditional Tasks;
415
425
  // API: https://vitaly-t.github.io/pg-promise/Database.html#taskIf
416
- taskIf<T = any>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
426
+ taskIf<T>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
417
427
 
418
- taskIf<T = any>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
428
+ taskIf<T>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
419
429
 
420
- taskIf<T = any>(options: {
421
- tag?: any,
422
- cnd?: boolean | ((t: ITask<Ext> & Ext) => boolean)
423
- }, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
430
+ taskIf<T>(options: ITaskIfOptions<Ext>, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
424
431
 
425
432
  // Transactions;
426
433
  // API: https://vitaly-t.github.io/pg-promise/Database.html#tx
427
- tx<T = any>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
434
+ tx<T>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
428
435
 
429
- tx<T = any>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
436
+ tx<T>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
430
437
 
431
- tx<T = any>(options: {
438
+ tx<T>(options: {
432
439
  tag?: any,
433
440
  mode?: _txMode.TransactionMode | null
434
441
  }, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
435
442
 
436
443
  // Conditional Transactions;
437
444
  // API: https://vitaly-t.github.io/pg-promise/Database.html#txIf
438
- txIf<T = any>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
445
+ txIf<T>(cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
439
446
 
440
- txIf<T = any>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
447
+ txIf<T>(tag: string | number, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
441
448
 
442
- txIf<T = any>(options: {
443
- tag?: any,
444
- mode?: _txMode.TransactionMode | null,
445
- reusable?: boolean | ((t: ITask<Ext> & Ext) => boolean),
446
- cnd?: boolean | ((t: ITask<Ext> & Ext) => boolean)
447
- }, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
449
+ txIf<T>(options: ITxIfOptions<Ext>, cb: (t: ITask<Ext> & Ext) => T | XPromise<T>): XPromise<T>
448
450
  }
449
451
 
450
452
  // Database object in connected state;
@@ -708,8 +710,9 @@ declare namespace pgPromise {
708
710
  ColumnSet: typeof ColumnSet
709
711
  TableName: typeof TableName
710
712
 
711
- _TN(data: TemplateStringsArray, ...args: Array<any>): ITable
712
- _TN(s: string) :ITable
713
+ _TN(path: TemplateStringsArray, ...args: Array<any>): ITable
714
+
715
+ _TN(path: string): ITable
713
716
  }
714
717
 
715
718
  interface IGenericPromise {