pg-promise 11.7.7 → 11.7.8
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 +3 -3
- package/lib/helpers/column-set.js +7 -5
- package/lib/helpers/column.js +2 -2
- package/lib/helpers/methods/insert.js +8 -5
- package/lib/helpers/methods/sets.js +3 -2
- package/lib/helpers/methods/update.js +12 -9
- package/lib/helpers/methods/values.js +3 -2
- package/lib/helpers/table-name.js +24 -10
- package/lib/query-file.js +2 -2
- package/package.json +1 -1
- package/typescript/pg-promise.d.ts +2 -2
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
|
|
379
|
+
* const {toPostgres, rawType} = pgp.as.ctf; // Custom Type Formatting symbols
|
|
380
380
|
*
|
|
381
381
|
* class MyType {
|
|
382
382
|
* constructor() {
|
|
383
|
-
* this[
|
|
383
|
+
* this[rawType] = true; // set it only when toPostgres returns a pre-formatted result
|
|
384
384
|
* }
|
|
385
385
|
*
|
|
386
|
-
* [
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
501
|
+
* const cs = new ColumnSet(['?one', 'two:json'], {table: 'my-table'});
|
|
500
502
|
* console.log(cs);
|
|
501
503
|
* //=>
|
|
502
504
|
* // ColumnSet {
|
package/lib/helpers/column.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
|
88
|
+
* const cs = new ColumnSet(['val', 'msg'], {table: 'my-table'});
|
|
86
89
|
*
|
|
87
|
-
*
|
|
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
|
|
62
|
+
* const cs = new ColumnSet(['?id','val', 'msg']);
|
|
62
63
|
*
|
|
63
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
|
125
|
+
* const cs = new ColumnSet(['?id', 'val', 'msg'], {table: 'my-table'});
|
|
124
126
|
*
|
|
125
|
-
*
|
|
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
|
-
*
|
|
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
|
|
142
|
-
* const result =
|
|
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
|
|
77
|
+
* const cs = new ColumnSet(['val', 'msg']);
|
|
77
78
|
*
|
|
78
|
-
*
|
|
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|
|
|
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
|
-
* -
|
|
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
|
|
93
|
-
* const fullName = tn[
|
|
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,15 @@ 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(
|
|
158
|
-
if (Array.isArray(
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
170
|
+
function _TN(path, ...args) {
|
|
171
|
+
if (Array.isArray(path) && path.raw) {
|
|
172
|
+
const v = i => i < args.length ? args[i] ?? '' : '';
|
|
173
|
+
path = path.reduce((a, c, i) => a + c + v(i), '');
|
|
174
|
+
} // else 'path' is a string
|
|
175
|
+
const [schema, table] = path.split('.');
|
|
162
176
|
if (table === undefined) {
|
|
163
177
|
return {table: schema};
|
|
164
178
|
}
|
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
|
|
291
|
-
* const query = qf[
|
|
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
|
@@ -708,8 +708,8 @@ declare namespace pgPromise {
|
|
|
708
708
|
ColumnSet: typeof ColumnSet
|
|
709
709
|
TableName: typeof TableName
|
|
710
710
|
|
|
711
|
-
_TN(
|
|
712
|
-
_TN(
|
|
711
|
+
_TN(path: TemplateStringsArray, ...args: Array<any>): ITable
|
|
712
|
+
_TN(path: string) :ITable
|
|
713
713
|
}
|
|
714
714
|
|
|
715
715
|
interface IGenericPromise {
|