Skip to content

Commit 26fdb2a

Browse files
authored
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.
1 parent bb86d30 commit 26fdb2a

40 files changed

+1276
-743
lines changed

.editorconfig

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*]
2+
quote_type = single

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist

.vscode/settings.json

-10
This file was deleted.

CONFIG.md

+33-47
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ const rows = await sql`SELECT * FROM posts WHERE id = ${postId}`;
2626

2727
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).
2828

29-
3029
### `arrayMode: boolean`
3130

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:
3332

3433
```typescript
3534
import { neon } from '@neondatabase/serverless';
@@ -43,11 +42,12 @@ Or, with the same effect:
4342
```typescript
4443
import { neon } from '@neondatabase/serverless';
4544
const sql = neon(process.env.DATABASE_URL);
46-
const rows = await sql('SELECT * FROM posts WHERE id = $1', [postId], { arrayMode: true });
45+
const rows = await sql('SELECT * FROM posts WHERE id = $1', [postId], {
46+
arrayMode: true,
47+
});
4748
// -> [[12, "My post", ...]]
4849
```
4950

50-
5151
### `fullResults: boolean`
5252

5353
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:
7575
```typescript
7676
import { neon } from '@neondatabase/serverless';
7777
const sql = neon(process.env.DATABASE_URL);
78-
const results = await sql('SELECT * FROM posts WHERE id = $1', [postId], { fullResults: true });
78+
const results = await sql('SELECT * FROM posts WHERE id = $1', [postId], {
79+
fullResults: true,
80+
});
7981
// -> { ... same as above ... }
8082
```
8183

82-
8384
### `fetchOptions: Record<string, any>`
8485

8586
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:
8889

8990
```typescript
9091
import { neon } from '@neondatabase/serverless';
91-
const sql = neon(process.env.DATABASE_URL, { fetchOptions: { priority: 'high' } });
92+
const sql = neon(process.env.DATABASE_URL, {
93+
fetchOptions: { priority: 'high' },
94+
});
9295
const rows = await sql`SELECT * FROM posts WHERE id = ${postId}`;
9396
```
9497

@@ -99,14 +102,12 @@ import { neon } from '@neondatabase/serverless';
99102
const sql = neon(process.env.DATABASE_URL);
100103
const abortController = new AbortController();
101104
const timeout = setTimeout(() => abortController.abort('timed out'), 10000);
102-
const rows = await sql(
103-
'SELECT * FROM posts WHERE id = $1', [postId],
104-
{ fetchOptions: { signal: abortController.signal } }
105-
); // throws an error if no result received within 10s
105+
const rows = await sql('SELECT * FROM posts WHERE id = $1', [postId], {
106+
fetchOptions: { signal: abortController.signal },
107+
}); // throws an error if no result received within 10s
106108
clearTimeout(timeout);
107109
```
108110

109-
110111
## `transaction(...)` function
111112

112113
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';
120121
const sql = neon(process.env.DATABASE_URL);
121122
const showLatestN = 10;
122123

123-
const [posts, tags] = await sql.transaction([
124-
sql`SELECT * FROM posts ORDER BY posted_at DESC LIMIT ${showLatestN}`,
125-
sql`SELECT * FROM tags`,
126-
], {
127-
isolationLevel: 'RepeatableRead',
128-
readOnly: true,
129-
});
124+
const [posts, tags] = await sql.transaction(
125+
[
126+
sql`SELECT * FROM posts ORDER BY posted_at DESC LIMIT ${showLatestN}`,
127+
sql`SELECT * FROM tags`,
128+
],
129+
{
130+
isolationLevel: 'RepeatableRead',
131+
readOnly: true,
132+
},
133+
);
130134
```
131135

132136
Or as an example of the function case:
133137

134138
```javascript
135-
const [authors, tags] = await neon(process.env.DATABASE_URL)
136-
.transaction(txn => [
137-
txn`SELECT * FROM authors`,
138-
txn`SELECT * FROM tags`,
139-
]);
139+
const [authors, tags] = await neon(process.env.DATABASE_URL).transaction(
140+
(txn) => [txn`SELECT * FROM authors`, txn`SELECT * FROM tags`],
141+
);
140142
```
141143

142144
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.
143145

144146
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).
145147

146-
147148
### `isolationMode`
148149

149150
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'`.
150151

151-
152152
### `readOnly`
153153

154154
If `true`, this option ensures that a `READ ONLY` transaction is used to execute the queries passed.
155155

156-
157156
### `deferrable`
158157

159158
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.
160159

161-
162160
## `neonConfig` configuration
163161

164162
In most cases, there are two ways to set configuration options:
165163

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.
167165
2. Set options on individual `Client` instances using their `neonConfig` property.
168166

169167
For example:
@@ -182,7 +180,6 @@ client.neonConfig.webSocketConstructor = ws;
182180

183181
A few configuration options can only be set globally, and these are noted as such below.
184182

185-
186183
#### `webSocketConstructor: typeof WebSocket | undefined`
187184

188185
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.
@@ -192,15 +189,13 @@ For example:
192189
```javascript
193190
import { neonConfig } from '@neondatabase/serverless';
194191
import ws from 'ws';
195-
neonConfig.webSocketConstructor = ws;
192+
neonConfig.webSocketConstructor = ws;
196193
```
197194

198-
199195
### Advanced configuration
200196

201197
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.
202198

203-
204199
#### `poolQueryViaFetch: boolean`
205200

206201
**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).
209204

210205
Note: this option can only be set globally, **not** on an individual `Client` instance.
211206

212-
213207
#### `fetchEndpoint: string | ((host: string, port: number | string) => string)`
214208

215-
Set `fetchEndpoint` to set the server endpoint to be sent queries via http fetch.
209+
Set `fetchEndpoint` to set the server endpoint to be sent queries via http fetch.
216210

217211
This may be useful for local development (e.g. to set a port that's not the default 443).
218212

@@ -222,7 +216,6 @@ Default: `host => 'https://' + host + '/sql'`
222216

223217
Note: this option can only be set globally, **not** on an individual `Client` instance.
224218

225-
226219
#### `fetchFunction: any`
227220

228221
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`.
231224

232225
Note: this option can only be set globally, **not** on an individual `Client` instance.
233226

234-
235227
#### `wsProxy: string | (host: string, port: number | string) => string`
236228

237229
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:
238230

239231
```javascript
240232
// either:
241-
neonConfig.wsProxy = (host, port) => `my-wsproxy.example.com/v1?address=${host}:${port}`
233+
neonConfig.wsProxy = (host, port) =>
234+
`my-wsproxy.example.com/v1?address=${host}:${port}`;
242235
// or (with identical effect):
243236
neonConfig.wsProxy = 'my-wsproxy.example.com/v1';
244237
```
245238

246239
Default: `host => host + '/v2'`.
247240

248-
249241
#### `pipelineConnect: "password" | false`
250242

251-
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.
252244

253245
If your connection doesn't support password authentication, set `pipelineConnect` to `false` instead.
254246

255247
Default: `"password"`.
256248

257-
258249
#### `coalesceWrites: boolean`
259250

260251
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.
261252

262253
Default: `true`.
263254

264-
265255
#### `forceDisablePgSSL: boolean`
266256

267257
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`.
268258

269259
Default: `true`.
270260

271-
272261
#### `useSecureWebSocket: boolean`
273262

274-
This option switches between secure (the default) and insecure WebSockets.
263+
This option switches between secure (the default) and insecure WebSockets.
275264

276265
To use experimental pure-JS encryption, set `useSecureWebSocket = false` and `forceDisablePgSSL = false`, and append `?sslmode=verify-full` to your database connection string.
277266

278267
**Remember that pure-JS encryption is currently experimental and not suitable for use in production.**
279268

280269
Default: `true`.
281270

282-
283271
#### `subtls: any`
284272

285273
**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;
292280

293281
Default: `undefined`.
294282

295-
296283
#### `rootCerts: string /* PEM format */`
297284

298285
**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.
301288

302289
Default: `""` (the empty string).
303290

304-
305291
#### `pipelineTLS: boolean`
306292

307293
**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.

DEPLOY.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ There are then two ways you can secure this:
1010

1111
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.
1212

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.**
1414

1515
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`.
1616

17-
1817
## Example shell commands
1918

2019
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.
@@ -88,7 +87,7 @@ apt install -y golang nginx certbot python3-certbot-nginx
8887

8988
echo "127.0.0.1 ${HOSTDOMAIN}" >> /etc/hosts
9089

91-
echo "
90+
echo "
9291
server {
9392
listen 80;
9493
listen [::]:80;
@@ -100,7 +99,7 @@ server {
10099
proxy_set_header Host \$host;
101100
}
102101
}
103-
" > /etc/nginx/sites-available/wsproxy
102+
" > /etc/nginx/sites-available/wsproxy
104103

105104
ln -s /etc/nginx/sites-available/wsproxy /etc/nginx/sites-enabled/wsproxy
106105

@@ -130,4 +129,4 @@ server {
130129
" > /etc/nginx/sites-available/wsproxy
131130

132131
service nginx restart
133-
```
132+
```

0 commit comments

Comments
 (0)