You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: utils/website/pg-sql2/api/sql-comment.md
+51-34Lines changed: 51 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,12 +3,17 @@ sidebar_position: 10
3
3
title: "sql.comment()"
4
4
---
5
5
6
-
# `sql.comment(text)`
6
+
# `sql.comment(text, include)`
7
7
8
8
Creates an SQL comment fragment that can be embedded in queries. Comments are
9
9
useful for documenting query logic, adding debugging information, or providing
10
-
context for complex operations. Comments are ignored during SQL execution but
11
-
can be valuable for debugging and maintenance.
10
+
context for complex operations.
11
+
12
+
Comments are ignored during SQL execution by default but
13
+
can be valuable for debugging and maintenance. By default, comments only appear when
14
+
[`GRAPHILE_ENV=development`](../development-mode.md) is set, but you can force
15
+
them to appear by setting `include` to `true` (or force them to be hidden via
16
+
`include` set to `false`).
12
17
13
18
:::warning[Do not include user-generated content!]
14
19
@@ -21,20 +26,21 @@ vulnerable SQL.
21
26
## Syntax
22
27
23
28
```typescript
24
-
sql.comment(text: string): SQL
29
+
sql.comment(text: string, include?:boolean): SQL
25
30
```
26
31
27
32
## Parameters
28
33
29
34
-`text` - The comment text to include
35
+
-`include` - Optional boolean to force inclusion (`true`) or exclusion (`false`) of the comment regardless of environment. Defaults to `undefined`, which means comments are included only in development mode.
30
36
31
37
## Return value
32
38
33
39
Returns a `SQL` fragment containing the properly formatted SQL comment.
34
40
35
41
## Examples
36
42
37
-
### Document query
43
+
### Leading comment
38
44
39
45
```js
40
46
import { sql } from"pg-sql2";
@@ -44,36 +50,15 @@ ${sql.comment("Fetch active users with their order counts")}
44
50
SELECTu.id, COUNT(o.id) as order_count ...
45
51
`;
46
52
47
-
// `sql.compile(query).text` will be something like:
53
+
console.log(sql.compile(query).text);
48
54
// /* Fetch active users with their order counts */
49
55
// SELECT u.id, COUNT(o.id) as order_count ...
50
56
```
51
57
52
-
### Inline comment
58
+
### Dynamic comment content
53
59
54
60
```js
55
-
constcomplexCalculation=sql`
56
-
${sql.comment(`Calculate total with shipping and tax at ${parseFloat(taxPercentage)}%`)}
0 commit comments