pg-sql2 4.12.1 → 5.0.0-0.1
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/CHANGELOG.md +13 -0
- package/LICENSE.md +14 -18
- package/README.md +74 -57
- package/dist/index.d.ts +199 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +988 -0
- package/dist/index.js.map +1 -0
- package/package.json +26 -31
- package/node8plus/index.d.ts +0 -55
- package/node8plus/index.js +0 -280
- package/node8plus/index.js.map +0 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# pg-sql2
|
|
2
|
+
|
|
3
|
+
## 5.0.0-0.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#125](https://github.com/benjie/postgraphile-private/pull/125)
|
|
8
|
+
[`91f2256b3`](https://github.com/benjie/postgraphile-private/commit/91f2256b3fd699bec19fc86f1ca79df057e58639)
|
|
9
|
+
Thanks [@benjie](https://github.com/benjie)! - Initial changesets release
|
|
10
|
+
|
|
11
|
+
- Updated dependencies
|
|
12
|
+
[[`91f2256b3`](https://github.com/benjie/postgraphile-private/commit/91f2256b3fd699bec19fc86f1ca79df057e58639)]:
|
|
13
|
+
- @graphile/lru@5.0.0-0.1
|
package/LICENSE.md
CHANGED
|
@@ -2,23 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Copyright © `2019` Benjie Gillam
|
|
4
4
|
|
|
5
|
-
Permission is hereby granted, free of charge, to any person
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
Software is furnished to do so, subject to the following
|
|
12
|
-
conditions:
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the “Software”), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
13
11
|
|
|
14
|
-
The above copyright notice and this permission notice shall be
|
|
15
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
16
14
|
|
|
17
|
-
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
24
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
# pg-sql2
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
[](https://github.com/sponsors/benjie)
|
|
4
|
+
[](https://patreon.com/benjie)
|
|
5
|
+
[](http://discord.gg/graphile)
|
|
6
|
+
[](https://twitter.com/GraphileHQ)
|
|
7
|
+
|
|
8
|
+
Create highly dynamic SQL in a powerful and flexible manner without opening
|
|
9
|
+
yourself to SQL injection attacks.
|
|
5
10
|
|
|
6
11
|
A key aim of this library is to be very fast, if you think you can improve
|
|
7
12
|
performance further please open a PR!
|
|
@@ -10,19 +15,26 @@ performance further please open a PR!
|
|
|
10
15
|
|
|
11
16
|
## Crowd-funded open-source software
|
|
12
17
|
|
|
13
|
-
To help us develop this software sustainably under the MIT license, we ask
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
To help us develop this software sustainably under the MIT license, we ask all
|
|
19
|
+
individuals and businesses that use it to help support its ongoing maintenance
|
|
20
|
+
and development via sponsorship.
|
|
16
21
|
|
|
17
22
|
### [Click here to find out more about sponsors and sponsorship.](https://www.graphile.org/sponsor/)
|
|
18
23
|
|
|
19
24
|
And please give some love to our featured sponsors 🤩:
|
|
20
25
|
|
|
21
26
|
<table><tr>
|
|
27
|
+
<td align="center"><a href="https://surge.io/"><img src="https://graphile.org/images/sponsors/surge.png" width="90" height="90" alt="Surge" /><br />Surge</a> *</td>
|
|
28
|
+
<td align="center"><a href="https://www.netflix.com/"><img src="https://graphile.org/images/sponsors/Netflix.png" width="90" height="90" alt="Netflix" /><br />Netflix</a> *</td>
|
|
29
|
+
<td align="center"><a href="https://qwick.com/"><img src="https://graphile.org/images/sponsors/qwick.png" width="90" height="90" alt="Qwick" /><br />Qwick</a> *</td>
|
|
30
|
+
<td align="center"><a href="https://www.the-guild.dev/"><img src="https://graphile.org/images/sponsors/theguild.png" width="90" height="90" alt="The Guild" /><br />The Guild</a> *</td>
|
|
31
|
+
</tr><tr>
|
|
22
32
|
<td align="center"><a href="http://chads.website"><img src="https://graphile.org/images/sponsors/chadf.png" width="90" height="90" alt="Chad Furman" /><br />Chad Furman</a> *</td>
|
|
23
|
-
<td align="center"><a href="https://
|
|
24
|
-
<td align="center"><a href="https://
|
|
25
|
-
<td align="center"><a href="https://
|
|
33
|
+
<td align="center"><a href="https://www.fanatics.com/"><img src="https://graphile.org/images/sponsors/fanatics.png" width="90" height="90" alt="Fanatics" /><br />Fanatics</a> *</td>
|
|
34
|
+
<td align="center"><a href="https://dovetailapp.com/"><img src="https://graphile.org/images/sponsors/dovetail.png" width="90" height="90" alt="Dovetail" /><br />Dovetail</a> *</td>
|
|
35
|
+
<td align="center"><a href="https://www.enzuzo.com/"><img src="https://graphile.org/images/sponsors/enzuzo.png" width="90" height="90" alt="Enzuzo" /><br />Enzuzo</a> *</td>
|
|
36
|
+
</tr><tr>
|
|
37
|
+
<td align="center"><a href="https://stellate.co/"><img src="https://graphile.org/images/sponsors/Stellate.png" width="90" height="90" alt="Stellate" /><br />Stellate</a> *</td>
|
|
26
38
|
</tr></table>
|
|
27
39
|
|
|
28
40
|
<em>\* Sponsors the entire Graphile suite</em>
|
|
@@ -32,7 +44,7 @@ And please give some love to our featured sponsors 🤩:
|
|
|
32
44
|
## Usage
|
|
33
45
|
|
|
34
46
|
```js
|
|
35
|
-
const sql = require("pg-sql2");
|
|
47
|
+
const { default: sql } = require("pg-sql2");
|
|
36
48
|
// or import sql from 'pg-sql2';
|
|
37
49
|
|
|
38
50
|
const tableName = "user";
|
|
@@ -41,25 +53,25 @@ const fields = ["name", "age", "height"];
|
|
|
41
53
|
// sql.join is used to join fragments with a common separator, NOT to join tables!
|
|
42
54
|
const sqlFields = sql.join(
|
|
43
55
|
// sql.identifier safely escapes arguments and joins them with dots
|
|
44
|
-
fields.map(fieldName => sql.identifier(tableName, fieldName)),
|
|
45
|
-
", "
|
|
56
|
+
fields.map((fieldName) => sql.identifier(tableName, fieldName)),
|
|
57
|
+
", ",
|
|
46
58
|
);
|
|
47
59
|
|
|
48
60
|
// sql.value will store the value and instead add a placeholder to the SQL
|
|
49
61
|
// statement, to ensure that no SQL injection can occur.
|
|
50
|
-
const sqlConditions = sql
|
|
51
|
-
22
|
|
62
|
+
const sqlConditions = sql`created_at > NOW() - interval '3 years' and age > ${sql.value(
|
|
63
|
+
22,
|
|
52
64
|
)}`;
|
|
53
65
|
|
|
54
66
|
// This could be a full query, but we're going to embed it in another query safely
|
|
55
|
-
const innerQuery = sql
|
|
56
|
-
tableName
|
|
67
|
+
const innerQuery = sql`select ${sqlFields} from ${sql.identifier(
|
|
68
|
+
tableName,
|
|
57
69
|
)} where ${sqlConditions}`;
|
|
58
70
|
|
|
59
71
|
// Symbols are automatically assigned unique identifiers
|
|
60
72
|
const sqlAlias = sql.identifier(Symbol());
|
|
61
73
|
|
|
62
|
-
const query = sql
|
|
74
|
+
const query = sql`
|
|
63
75
|
with ${sqlAlias} as (${innerQuery})
|
|
64
76
|
select
|
|
65
77
|
(select json_agg(row_to_json(${sqlAlias})) from ${sqlAlias}) as all_data,
|
|
@@ -85,17 +97,19 @@ console.log(values); // [ 22 ]
|
|
|
85
97
|
|
|
86
98
|
## API
|
|
87
99
|
|
|
88
|
-
### `` sql
|
|
100
|
+
### `` sql`...` ``
|
|
89
101
|
|
|
90
|
-
Builds part of (or the whole of) an SQL query, safely
|
|
102
|
+
Builds part of (or the whole of) an SQL query, safely interpreting the embedded
|
|
103
|
+
expressions. If a non `sql` expression is passed in, e.g.:
|
|
91
104
|
|
|
92
105
|
<!-- skip-example -->
|
|
93
106
|
|
|
94
107
|
```js
|
|
95
|
-
sql
|
|
108
|
+
sql`select ${1}`;
|
|
96
109
|
```
|
|
97
110
|
|
|
98
|
-
then an error will be thrown.
|
|
111
|
+
then an error will be thrown. This prevents SQL injection, as all values must go
|
|
112
|
+
through an allowed API.
|
|
99
113
|
|
|
100
114
|
### `sql.identifier(ident, ...)`
|
|
101
115
|
|
|
@@ -105,82 +119,85 @@ then each will be escaped and then they will be joined with dots (e.g.
|
|
|
105
119
|
|
|
106
120
|
### `sql.value(val)`
|
|
107
121
|
|
|
108
|
-
Represents an SQL value, will be replaced with a placeholder and the value
|
|
122
|
+
Represents an SQL value, will be replaced with a placeholder and the value
|
|
123
|
+
collected up at compile time.
|
|
109
124
|
|
|
110
125
|
### `sql.literal(val)`
|
|
111
126
|
|
|
112
|
-
As `sql.value`, but in the case of very simple values may write them directly
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
As `sql.value`, but in the case of very simple values may write them directly to
|
|
128
|
+
the SQL statement rather than using a placeholder. Should only be used with data
|
|
129
|
+
that is not sensitive and is trusted (not user-provided data), e.g. for the key
|
|
130
|
+
arguments to `json_build_object(key, val, key, val, ...)` which you have
|
|
131
|
+
produced.
|
|
117
132
|
|
|
118
|
-
### `sql.join(arrayOfFragments,
|
|
133
|
+
### `sql.join(arrayOfFragments, delimiter)`
|
|
119
134
|
|
|
120
|
-
Joins an array of sql
|
|
135
|
+
Joins an array of `sql` values using the delimiter (which is treated as a raw
|
|
136
|
+
SQL string); e.g.
|
|
121
137
|
|
|
122
138
|
```js
|
|
123
|
-
const arrayOfSqlFields = ["a", "b", "c", "d"].map(n => sql.identifier(n));
|
|
124
|
-
sql
|
|
139
|
+
const arrayOfSqlFields = ["a", "b", "c", "d"].map((n) => sql.identifier(n));
|
|
140
|
+
sql`select ${sql.join(arrayOfSqlFields, ", ")}`; // -> select "a", "b", "c", "d"
|
|
125
141
|
|
|
126
|
-
const arrayOfSqlConditions = [
|
|
127
|
-
|
|
128
|
-
sql.query`b = 2`,
|
|
129
|
-
sql.query`c = 3`,
|
|
130
|
-
];
|
|
131
|
-
sql.query`where (${sql.join(arrayOfSqlConditions, ") and (")})`; // -> where (a = 1) and (b = 2) and (c = 3)
|
|
142
|
+
const arrayOfSqlConditions = [sql`a = 1`, sql`b = 2`, sql`c = 3`];
|
|
143
|
+
sql`where (${sql.join(arrayOfSqlConditions, ") and (")})`; // -> where (a = 1) and (b = 2) and (c = 3)
|
|
132
144
|
|
|
133
145
|
const fragments = [
|
|
134
146
|
{ alias: "name", sqlFragment: sql.identifier("user", "name") },
|
|
135
147
|
{ alias: "age", sqlFragment: sql.identifier("user", "age") },
|
|
136
148
|
];
|
|
137
|
-
sql
|
|
149
|
+
sql`
|
|
138
150
|
json_build_object(
|
|
139
151
|
${sql.join(
|
|
140
152
|
fragments.map(
|
|
141
|
-
({ sqlFragment, alias }) =>
|
|
142
|
-
sql.query`${sql.literal(alias)}, ${sqlFragment}`
|
|
153
|
+
({ sqlFragment, alias }) => sql`${sql.literal(alias)}, ${sqlFragment}`,
|
|
143
154
|
),
|
|
144
|
-
",\n"
|
|
155
|
+
",\n",
|
|
145
156
|
)}
|
|
146
157
|
)`;
|
|
147
158
|
|
|
148
159
|
const arrayOfSqlInnerJoins = [
|
|
149
|
-
sql
|
|
150
|
-
sql
|
|
160
|
+
sql`inner join bar on (bar.foo_id = foo.id)`,
|
|
161
|
+
sql`inner join baz on (baz.bar_id = bar.id)`,
|
|
151
162
|
];
|
|
152
|
-
sql
|
|
163
|
+
sql`select * from foo ${sql.join(arrayOfSqlInnerJoins, " ")}`;
|
|
153
164
|
// select * from foo inner join bar on (bar.foo_id = foo.id) inner join baz on (baz.bar_id = bar.id)
|
|
154
165
|
```
|
|
155
166
|
|
|
156
167
|
### `sql.compile(query)`
|
|
157
168
|
|
|
158
|
-
Compiles the query into an SQL statement and a list of values, ready to be
|
|
169
|
+
Compiles the query into an SQL statement and a list of values, ready to be
|
|
170
|
+
executed
|
|
159
171
|
|
|
160
172
|
```js
|
|
161
|
-
const query = sql
|
|
173
|
+
const query = sql`...`;
|
|
162
174
|
const { text, values } = sql.compile(query);
|
|
163
175
|
|
|
164
176
|
// const { rows } = await pg.query(text, values);
|
|
165
177
|
```
|
|
166
178
|
|
|
179
|
+
### `sql.compile(query, options)`
|
|
180
|
+
|
|
181
|
+
An advanced form of `sql.compile` that can be used to provide the placeholders
|
|
182
|
+
when you're using `sql.placeholder`.
|
|
183
|
+
|
|
167
184
|
## History
|
|
168
185
|
|
|
169
|
-
This is a replacement for
|
|
170
|
-
`pg-sql`](https://www.npmjs.com/package/pg-sql), combining the
|
|
171
|
-
that was done to it
|
|
172
|
-
postgraphql](https://github.com/postgraphql/postgraphql/blob/9c36d7e9b9ad74e665de18964fd2554f9f639903/src/postgres/utils/sql.ts)
|
|
186
|
+
This is a replacement for
|
|
187
|
+
[@calebmer's `pg-sql`](https://www.npmjs.com/package/pg-sql), combining the
|
|
188
|
+
additional work that was done to it
|
|
189
|
+
[in postgraphql](https://github.com/postgraphql/postgraphql/blob/9c36d7e9b9ad74e665de18964fd2554f9f639903/src/postgres/utils/sql.ts)
|
|
173
190
|
and offering the following enhancements:
|
|
174
191
|
|
|
175
|
-
- Better development experience for people not using
|
|
176
|
-
|
|
192
|
+
- Better development experience for people not using TypeScript (throws errors a
|
|
193
|
+
lot earlier allowing you to catch issues at the source)
|
|
177
194
|
- Slightly more helpful error messages
|
|
178
|
-
- Uses a symbol-key on the query nodes to protect against an object
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
195
|
+
- Uses a symbol-key on the query nodes to protect against an object accidentally
|
|
196
|
+
being inserted verbatim and being treated as valid (because every Symbol is
|
|
197
|
+
unique an attacker would need control of the code to get a reference to the
|
|
198
|
+
Symbol in order to set it on an object (it cannot be serialised/deserialised
|
|
199
|
+
via JSON or any other medium), and if the attacker has control of the code
|
|
200
|
+
then you've already lost)
|
|
184
201
|
- Adds `sql.literal` which is similar to `sql.value` but when used with simple
|
|
185
202
|
values can write the valid direct to the SQL statement. **USE WITH CAUTION**.
|
|
186
203
|
The purpose for this is if you are using _trusted_ values (e.g. for the keys
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is the secret to our safety; since this is a symbol it cannot be faked
|
|
3
|
+
* in a JSON payload and it cannot be constructed with a new Symbol (even with
|
|
4
|
+
* the same argument), so external data cannot make itself trusted.
|
|
5
|
+
*/
|
|
6
|
+
declare const $$type: unique symbol;
|
|
7
|
+
/**
|
|
8
|
+
* Represents raw SQL, the text will be output verbatim into the compiled query.
|
|
9
|
+
*/
|
|
10
|
+
export interface SQLRawNode {
|
|
11
|
+
readonly [$$type]: "RAW";
|
|
12
|
+
/** text */
|
|
13
|
+
readonly t: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Represents an SQL identifier such as table, column, function, etc name. These
|
|
17
|
+
* identifiers will be automatically escaped when compiled, respecting any
|
|
18
|
+
* reserved words.
|
|
19
|
+
*/
|
|
20
|
+
export interface SQLIdentifierNode {
|
|
21
|
+
readonly [$$type]: "IDENTIFIER";
|
|
22
|
+
/** symbol */
|
|
23
|
+
readonly s: symbol;
|
|
24
|
+
/** name */
|
|
25
|
+
readonly n: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A value that can be used in `sql.value(...)`; note that objects are **NOT**
|
|
29
|
+
* valid values; you must `JSON.stringify(obj)` or similar.
|
|
30
|
+
*/
|
|
31
|
+
export declare type SQLRawValue = string | number | boolean | null | ReadonlyArray<SQLRawValue>;
|
|
32
|
+
/**
|
|
33
|
+
* Represents an SQL value that will be replaced with a placeholder in the
|
|
34
|
+
* compiled SQL statement.
|
|
35
|
+
*/
|
|
36
|
+
export interface SQLValueNode {
|
|
37
|
+
readonly [$$type]: "VALUE";
|
|
38
|
+
/** value */
|
|
39
|
+
readonly v: SQLRawValue;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Represents that the SQL inside this should be indented when pretty printed.
|
|
43
|
+
*/
|
|
44
|
+
export interface SQLIndentNode {
|
|
45
|
+
readonly [$$type]: "INDENT";
|
|
46
|
+
/** content */
|
|
47
|
+
readonly c: SQLQuery;
|
|
48
|
+
/** flags */
|
|
49
|
+
readonly f: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Informs pg-sql2 to treat symbol2 as if it were the same as symbol1
|
|
53
|
+
*/
|
|
54
|
+
export interface SQLSymbolAliasNode {
|
|
55
|
+
readonly [$$type]: "SYMBOL_ALIAS";
|
|
56
|
+
readonly a: symbol;
|
|
57
|
+
readonly b: symbol;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A placeholder that should be replaced at compile time using one of the
|
|
61
|
+
* replacements provided.
|
|
62
|
+
*/
|
|
63
|
+
export interface SQLPlaceholderNode {
|
|
64
|
+
readonly [$$type]: "PLACEHOLDER";
|
|
65
|
+
/** symbol */
|
|
66
|
+
readonly s: symbol;
|
|
67
|
+
/** fallback */
|
|
68
|
+
readonly k?: SQL;
|
|
69
|
+
}
|
|
70
|
+
/** @internal */
|
|
71
|
+
export declare type SQLNode = SQLRawNode | SQLValueNode | SQLIdentifierNode | SQLIndentNode | SQLSymbolAliasNode | SQLPlaceholderNode;
|
|
72
|
+
/** @internal */
|
|
73
|
+
export interface SQLQuery {
|
|
74
|
+
readonly [$$type]: "QUERY";
|
|
75
|
+
/** nodes */
|
|
76
|
+
readonly n: ReadonlyArray<SQLNode>;
|
|
77
|
+
/** flags */
|
|
78
|
+
readonly f: number;
|
|
79
|
+
/** checksum - for faster isEquivalent checks */
|
|
80
|
+
readonly c: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Representation of SQL, identifiers, values, etc; to generate a query that
|
|
84
|
+
* can be issued to the database it needs to be fed to `sql.compile`.
|
|
85
|
+
*/
|
|
86
|
+
export declare type SQL = SQLNode | SQLQuery;
|
|
87
|
+
export declare function escapeSqlIdentifier(str: string): string;
|
|
88
|
+
declare function isSQL(node: unknown): node is SQL;
|
|
89
|
+
/**
|
|
90
|
+
* Accepts an sql`...` expression and compiles it out to SQL text with
|
|
91
|
+
* placeholders, and the values to substitute for these values.
|
|
92
|
+
*/
|
|
93
|
+
export declare function compile(sql: SQL, options?: {
|
|
94
|
+
placeholderValues?: Map<symbol, SQL>;
|
|
95
|
+
}): {
|
|
96
|
+
text: string;
|
|
97
|
+
values: SQLRawValue[];
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Creates a SQL item for some raw SQL text. Just plain ol‘ raw SQL. This
|
|
101
|
+
* method is dangerous though because it involves no escaping, so proceed with
|
|
102
|
+
* caution! It's very very rarely warranted - there is likely a safer way of
|
|
103
|
+
* achieving your goal.
|
|
104
|
+
*/
|
|
105
|
+
export declare function raw(text: string): SQL;
|
|
106
|
+
/**
|
|
107
|
+
* Creates a SQL item for a SQL identifier. A SQL identifier is anything like
|
|
108
|
+
* a table, schema, or column name. An identifier may also have a namespace,
|
|
109
|
+
* thus why many names are accepted.
|
|
110
|
+
*/
|
|
111
|
+
export declare function identifier(...names: Array<string | symbol>): SQL;
|
|
112
|
+
/**
|
|
113
|
+
* Creates a SQL item for a value that will be included in our final query.
|
|
114
|
+
* This value will be added in a way which avoids SQL injection.
|
|
115
|
+
*/
|
|
116
|
+
export declare function value(val: SQLRawValue): SQL;
|
|
117
|
+
declare const trueNode: SQLRawNode;
|
|
118
|
+
declare const falseNode: SQLRawNode;
|
|
119
|
+
declare const nullNode: SQLRawNode;
|
|
120
|
+
export declare const blank: SQLRawNode;
|
|
121
|
+
export declare const dot: SQLRawNode;
|
|
122
|
+
/**
|
|
123
|
+
* If the value is simple will inline it into the query, otherwise will defer
|
|
124
|
+
* to `sql.value`.
|
|
125
|
+
*/
|
|
126
|
+
export declare function literal(val: string | number | boolean | null): SQL;
|
|
127
|
+
/**
|
|
128
|
+
* Join some SQL items together, optionally separated by a string. Useful when
|
|
129
|
+
* dealing with lists of SQL items, for example a dynamic list of columns or
|
|
130
|
+
* variadic SQL function arguments.
|
|
131
|
+
*/
|
|
132
|
+
export declare function join(items: Array<SQL>, separator?: string): SQL;
|
|
133
|
+
export declare function indent(fragment: SQL): SQL;
|
|
134
|
+
export declare function indent(strings: TemplateStringsArray, ...values: Array<SQL>): SQL;
|
|
135
|
+
export declare function indentIf(condition: boolean, fragment: SQL): SQL;
|
|
136
|
+
/**
|
|
137
|
+
* Wraps the given fragment in parens if necessary (or if forced, e.g. for a
|
|
138
|
+
* subquery or maybe stylistically a join condition).
|
|
139
|
+
*
|
|
140
|
+
* Returns the input SQL fragment if it does not need parenthesis to be
|
|
141
|
+
* inserted into another expression, otherwise a parenthesised fragment if not
|
|
142
|
+
* doing so could cause ambiguity. We're relying on the user to be sensible
|
|
143
|
+
* here, this is not fool-proof.
|
|
144
|
+
*
|
|
145
|
+
* @remarks The following are all parens safe:
|
|
146
|
+
*
|
|
147
|
+
* - A placeholder `$1`
|
|
148
|
+
* - A number `0.123456`
|
|
149
|
+
* - A string `'Foo bar'` / `E'Foo bar'`
|
|
150
|
+
* - An identifier `table.column` / `"MyTaBlE"."MyCoLuMn"`
|
|
151
|
+
*
|
|
152
|
+
* The following might seem but are not parens safe:
|
|
153
|
+
*
|
|
154
|
+
* - A function call `schema.func(param)` - reason: `schema.func(param).*`
|
|
155
|
+
* should be `(schema.func(param)).*`
|
|
156
|
+
* - A simple expression `1 = 2` - reason: `1 = 2 = false` is invalid; whereas
|
|
157
|
+
* `(1 = 2) = false` is fine. Similarly `1 = 2::text` differs from `(1 = 2)::text`.
|
|
158
|
+
* - An identifier `table.column.attribute` / `"MyTaBlE"."MyCoLuMn"."MyAtTrIbUtE"` (this needs to be `(table.column).attribute`)
|
|
159
|
+
*/
|
|
160
|
+
export declare function parens(frag: SQL, force?: boolean): SQL;
|
|
161
|
+
export declare function symbolAlias(symbol1: symbol, symbol2: symbol): SQL;
|
|
162
|
+
export declare function placeholder(symbol: symbol, fallback?: SQL): SQLPlaceholderNode;
|
|
163
|
+
export declare function arraysMatch<T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>, comparator?: (val1: T, val2: T) => boolean): boolean;
|
|
164
|
+
export declare function isEquivalent(sql1: SQL, sql2: SQL, options?: {
|
|
165
|
+
symbolSubstitutes?: Map<symbol, symbol>;
|
|
166
|
+
}): boolean;
|
|
167
|
+
/**
|
|
168
|
+
* @experimental
|
|
169
|
+
*/
|
|
170
|
+
export declare function replaceSymbol(frag: SQL, needle: symbol, replacement: symbol): SQL;
|
|
171
|
+
export declare const sql: PgSQL;
|
|
172
|
+
export default sql;
|
|
173
|
+
export { falseNode as false, sql as fragment, isSQL, nullNode as null, sql as query, trueNode as true, };
|
|
174
|
+
export interface PgSQL {
|
|
175
|
+
(strings: TemplateStringsArray, ...values: Array<SQL>): SQL;
|
|
176
|
+
escapeSqlIdentifier: typeof escapeSqlIdentifier;
|
|
177
|
+
compile: typeof compile;
|
|
178
|
+
isEquivalent: typeof isEquivalent;
|
|
179
|
+
query: PgSQL;
|
|
180
|
+
raw: typeof raw;
|
|
181
|
+
identifier: typeof identifier;
|
|
182
|
+
value: typeof value;
|
|
183
|
+
literal: typeof literal;
|
|
184
|
+
join: typeof join;
|
|
185
|
+
indent: typeof indent;
|
|
186
|
+
indentIf: typeof indentIf;
|
|
187
|
+
parens: typeof parens;
|
|
188
|
+
symbolAlias: typeof symbolAlias;
|
|
189
|
+
placeholder: typeof placeholder;
|
|
190
|
+
blank: typeof blank;
|
|
191
|
+
fragment: PgSQL;
|
|
192
|
+
true: typeof trueNode;
|
|
193
|
+
false: typeof falseNode;
|
|
194
|
+
null: typeof nullNode;
|
|
195
|
+
isSQL: typeof isSQL;
|
|
196
|
+
replaceSymbol: typeof replaceSymbol;
|
|
197
|
+
sql: PgSQL;
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAsBA;;;;GAIG;AACH,QAAA,MAAM,MAAM,eAAyB,CAAC;AAEtC;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC;IACzB,WAAW;IACX,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;IAChC,aAAa;IACb,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW;IACX,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;GAGG;AACH,oBAAY,WAAW,GACnB,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,aAAa,CAAC,WAAW,CAAC,CAAC;AAE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