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
Adds Prettier configuration and Prettier scripts (#95)
TODO: Add a GitHub Action that checks this on every PR.
```
$ npm run prettier:fmt:check
$ npm run prettier:fmt:fix
```
**Once this commit lands, I will add a `.git-blame-ignore-revs` at the
top of the repo with the commit hash for this commit so that git blame
ignores this mega-commit.
Copy file name to clipboardexpand all lines: CONFIG.md
+33-47
Original file line number
Diff line number
Diff line change
@@ -26,10 +26,9 @@ const rows = await sql`SELECT * FROM posts WHERE id = ${postId}`;
26
26
27
27
However, you can customise the return format of the query function using the configuration options `fullResults` and `arrayMode`. These options are available both on the `neon(...)` function and on the query function it returns (but only when the query function is called as an ordinary function, not as a tagged-template function).
28
28
29
-
30
29
### `arrayMode: boolean`
31
30
32
-
When `arrayMode` is true, rows are returned as an array of arrays instead of an array of objects:
31
+
When `arrayMode` is true, rows are returned as an array of arrays instead of an array of objects:
33
32
34
33
```typescript
35
34
import { neon } from'@neondatabase/serverless';
@@ -43,11 +42,12 @@ Or, with the same effect:
43
42
```typescript
44
43
import { neon } from'@neondatabase/serverless';
45
44
const sql =neon(process.env.DATABASE_URL);
46
-
const rows =awaitsql('SELECT * FROM posts WHERE id = $1', [postId], { arrayMode: true });
45
+
const rows =awaitsql('SELECT * FROM posts WHERE id = $1', [postId], {
46
+
arrayMode: true,
47
+
});
47
48
// -> [[12, "My post", ...]]
48
49
```
49
50
50
-
51
51
### `fullResults: boolean`
52
52
53
53
When `fullResults` is true, additional metadata is returned alongside the result rows, which are then found in the `rows` property of the return value. The metadata matches what would be returned by node-postgres:
@@ -75,11 +75,12 @@ Or, with the same effect:
75
75
```typescript
76
76
import { neon } from'@neondatabase/serverless';
77
77
const sql =neon(process.env.DATABASE_URL);
78
-
const results =awaitsql('SELECT * FROM posts WHERE id = $1', [postId], { fullResults: true });
78
+
const results =awaitsql('SELECT * FROM posts WHERE id = $1', [postId], {
79
+
fullResults: true,
80
+
});
79
81
// -> { ... same as above ... }
80
82
```
81
83
82
-
83
84
### `fetchOptions: Record<string, any>`
84
85
85
86
The `fetchOptions` option can be passed to `neon(...)`, the `transaction` function, or the query function (if not within a `transaction` function). This option takes an object that is merged with the options to the `fetch` call.
@@ -88,7 +89,9 @@ For example, to increase the priority of every database `fetch` request:
); // throws an error if no result received within 10s
105
+
const rows =awaitsql('SELECT * FROM posts WHERE id = $1', [postId], {
106
+
fetchOptions: { signal: abortController.signal },
107
+
}); // throws an error if no result received within 10s
106
108
clearTimeout(timeout);
107
109
```
108
110
109
-
110
111
## `transaction(...)` function
111
112
112
113
The `transaction(queriesOrFn, options)` function is exposed as a property on the query function. It allows multiple queries to be executed within a single, non-interactive transaction.
@@ -120,50 +121,47 @@ import { neon } from '@neondatabase/serverless';
120
121
constsql=neon(process.env.DATABASE_URL);
121
122
constshowLatestN=10;
122
123
123
-
const [posts, tags] =awaitsql.transaction([
124
-
sql`SELECT*FROM posts ORDER BY posted_at DESCLIMIT${showLatestN}`,
125
-
sql`SELECT*FROM tags`,
126
-
], {
127
-
isolationLevel:'RepeatableRead',
128
-
readOnly:true,
129
-
});
124
+
const [posts, tags] =awaitsql.transaction(
125
+
[
126
+
sql`SELECT*FROM posts ORDER BY posted_at DESCLIMIT${showLatestN}`,
(txn) => [txn`SELECT * FROM authors`, txn`SELECT * FROM tags`],
141
+
);
140
142
```
141
143
142
144
The optional second argument to `transaction()`, `options`, has the same keys as the options to the ordinary query function -- `arrayMode`, `fullResults` and `fetchOptions` -- plus three additional keys that concern the transaction configuration. These transaction-related keys are: `isolationMode`, `readOnly` and `deferrable`. They are described below. Defaults for the transaction-related keys can also be set as options to the `neon` function.
143
145
144
146
The `fetchOptions` option cannot be supplied for individual queries inside `transaction()`, since only a single `fetch` is performed for the transaction as a whole. The TypeScript types also currently do not allow `arrayMode` or `fullResults` options to be supplied for individual queries within `transaction()` (although this does have the expected effect if the type errors are ignored).
145
147
146
-
147
148
### `isolationMode`
148
149
149
150
This option selects a Postgres [transaction isolation mode](https://www.postgresql.org/docs/current/transaction-iso.html). If present, it must be one of: `'ReadUncommitted'`, `'ReadCommitted'`, `'RepeatableRead'` or `'Serializable'`.
150
151
151
-
152
152
### `readOnly`
153
153
154
154
If `true`, this option ensures that a `READ ONLY` transaction is used to execute the queries passed.
155
155
156
-
157
156
### `deferrable`
158
157
159
158
If `true` (and if `readOnly` is also `true`, and `isolationMode` is `'Serializable'`), this option ensures that a `DEFERRABLE` transaction is used to execute the queries passed.
160
159
161
-
162
160
## `neonConfig` configuration
163
161
164
162
In most cases, there are two ways to set configuration options:
165
163
166
-
1. Import `neonConfig` from the package and set global default options on that.
164
+
1. Import `neonConfig` from the package and set global default options on that.
167
165
2. Set options on individual `Client` instances using their `neonConfig` property.
Set this parameter if you're using the driver in an environment where the `WebSocket` global is not defined, such as Node.js, and you need transaction or session support.
If you're using `@neondatabase/serverless` to connect to a Neon database, you usually **won't** need to touch the following configuration options. These options are intended for testing, troubleshooting, and supporting access to non-Neon Postgres instances via self-hosted WebSocket proxies.
202
198
203
-
204
199
#### `poolQueryViaFetch: boolean`
205
200
206
201
**Experimentally**, when `poolQueryViaFetch` is `true` and no listeners for the `"connect"`, `"acquire"`, `"release"` or `"remove"` events are set on the `Pool`, queries via `Pool.query()` will be sent by low-latency HTTP `fetch` request.
@@ -209,10 +204,9 @@ Default: currently `false` (but may be `true` in future).
209
204
210
205
Note: this option can only be set globally, **not** on an individual `Client` instance.
Note: this option can only be set globally, **not** on an individual `Client` instance.
224
218
225
-
226
219
#### `fetchFunction: any`
227
220
228
221
The `fetchFunction` option allows you to supply an alternative function for making http requests. The function must accept the same arguments as native `fetch`.
@@ -231,55 +224,50 @@ Default: `undefined`.
231
224
232
225
Note: this option can only be set globally, **not** on an individual `Client` instance.
If connecting to a non-Neon database, the `wsProxy` option should point to [your WebSocket proxy](DEPLOY.md). It can either be a string, which will have `?address=host:port` appended to it, or a function with the signature `(host: string, port: number | string) => string`. Either way, the protocol must _not_ be included, because this depends on other options. For example, when using the `wsproxy` proxy, the `wsProxy` option should look something like this:
To speed up connection times, the driver will pipeline the first three messages to the database (startup, authentication and first query) if `pipelineConnect` is set to `"password"`. Note that this will only work if you've configured cleartext password authentication for the relevant user and database.
243
+
To speed up connection times, the driver will pipeline the first three messages to the database (startup, authentication and first query) if `pipelineConnect` is set to `"password"`. Note that this will only work if you've configured cleartext password authentication for the relevant user and database.
252
244
253
245
If your connection doesn't support password authentication, set `pipelineConnect` to `false` instead.
254
246
255
247
Default: `"password"`.
256
248
257
-
258
249
#### `coalesceWrites: boolean`
259
250
260
251
When this option is `true`, multiple network writes generated in a single iteration of the JavaScript run-loop are coalesced into a single WebSocket message. Since node-postgres sends a lot of very short messages, this may reduce TCP/IP overhead.
261
252
262
253
Default: `true`.
263
254
264
-
265
255
#### `forceDisablePgSSL: boolean`
266
256
267
257
This option disables TLS encryption in the Postgres protocol (as set via e.g. `?sslmode=require` in the connection string). Security is not compromised if used in conjunction with `useSecureWebSocket = true`.
268
258
269
259
Default: `true`.
270
260
271
-
272
261
#### `useSecureWebSocket: boolean`
273
262
274
-
This option switches between secure (the default) and insecure WebSockets.
263
+
This option switches between secure (the default) and insecure WebSockets.
275
264
276
265
To use experimental pure-JS encryption, set `useSecureWebSocket = false` and `forceDisablePgSSL = false`, and append `?sslmode=verify-full` to your database connection string.
277
266
278
267
**Remember that pure-JS encryption is currently experimental and not suitable for use in production.**
279
268
280
269
Default: `true`.
281
270
282
-
283
271
#### `subtls: any`
284
272
285
273
**Only when using experimental pure-JS TLS encryption**, you must supply the [subtls](https://github.com/jawj/subtls) TLS library to the `subtls` option like so:
@@ -292,7 +280,6 @@ neonConfig.subtls = subtls;
292
280
293
281
Default: `undefined`.
294
282
295
-
296
283
#### `rootCerts: string /* PEM format */`
297
284
298
285
**Only when using experimental pure-JS TLS encryption**, the `rootCerts` option determines what root (certificate authority) certificates are trusted.
@@ -301,7 +288,6 @@ Its value is a string containing zero or more certificates in PEM format.
301
288
302
289
Default: `""` (the empty string).
303
290
304
-
305
291
#### `pipelineTLS: boolean`
306
292
307
293
**Only when using experimental pure-JS encryption**, the driver will pipeline the SSL request message and TLS Client Hello if `pipelineTLS` is set to `true`. Currently, this is only supported by Neon database hosts, and will fail when communicating with an ordinary Postgres or pgbouncer back-end.
Copy file name to clipboardexpand all lines: DEPLOY.md
+4-5
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,10 @@ There are then two ways you can secure this:
10
10
11
11
1. Set up nginx as a TLS proxy in front of `wsproxy`. Example shell commands to achieve this can be found below. Onward traffic to Postgres is not secured by this method, so Postgres should be running on the same machine or be reached over a private network.
12
12
13
-
2. Use experimental pure-JS Postgres connection encryption via [subtls](https://github.com/jawj/subtls). There's no need for nginx in this scenario, and the Postgres connection is encrypted end-to-end. You get this form of encryption if you set both `neonConfig.useSecureWebSocket`and `neonConfig.forceDisablePgSSL` to `false`, and append `?sslmode=verify-full` (or similar) to your connection string. TLS version 1.3 must be supported by the Postgres back-end. **Please note that subtls is experimental software and this configuration is not recommended for production use.**
13
+
2. Use experimental pure-JS Postgres connection encryption via [subtls](https://github.com/jawj/subtls). There's no need for nginx in this scenario, and the Postgres connection is encrypted end-to-end. You get this form of encryption if you set both `neonConfig.useSecureWebSocket` and `neonConfig.forceDisablePgSSL` to `false`, and append `?sslmode=verify-full` (or similar) to your connection string. TLS version 1.3 must be supported by the Postgres back-end. **Please note that subtls is experimental software and this configuration is not recommended for production use.**
14
14
15
15
Second, you'll need to set some [configuration options](CONFIG.md) on this package: at a minimum the `wsProxy` option and (if using experimental encryption) `subtls` and `rootCerts`.
16
16
17
-
18
17
## Example shell commands
19
18
20
19
To deploy `wsproxy` behind nginx (for TLS) on a host `ws.example.com` running Ubuntu 22.04 (and Postgres locally), you'd do something similar to the following.
0 commit comments