Skip to content

Commit a0620d3

Browse files
committed
Add async dispose support
1 parent 57cef65 commit a0620d3

File tree

9 files changed

+28
-9
lines changed

9 files changed

+28
-9
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ jobs:
8989
strategy:
9090
matrix:
9191
version:
92-
- 18.x
93-
- 20.x
92+
- 18
93+
- 20
9494
runs-on: ubuntu-latest
9595
needs: [build]
9696
timeout-minutes: 3
@@ -138,10 +138,12 @@ jobs:
138138
set -o pipefail
139139
PGSSLMODE=disable node main.cjs | tee /dev/stderr | grep -q false
140140
PGSSLMODE=require node main.cjs | tee /dev/stderr | grep -q true
141+
[[ -d v$NODE_VERSION ]] && cd v$NODE_VERSION
141142
npx tsc
142143
PGSSLMODE=disable node main.mjs | tee /dev/stderr | grep -q false
143144
PGSSLMODE=require node main.mjs | tee /dev/stderr | grep -q true
144145
env:
146+
NODE_VERSION: ${{ matrix.version }}
145147
NODE_EXTRA_CA_CERTS: ${{ github.workspace }}/ssl-cert-snakeoil.pem
146148
PGPORT: ${{ job.services.postgres.ports[5432] }}
147149
PGUSER: postgres

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ In next release ...
44
connected when the promise returns. The `Client` symbol is now a
55
type instead of a value.
66

7+
- Add `[Symbol.asyncDispose]` method to support [Explicit Resource
8+
Management](https://github.com/tc39/proposal-explicit-resource-management).
9+
710
- Add `off` method to disable event listening.
811

912
- Remove dependency on

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ try {
5858
}
5959
```
6060

61+
With TypeScript 5.2+ there is also support for `await using`:
62+
```typescript
63+
await using client = await connect();
64+
// Will be disposed of automatically at the end of the block.
65+
```
66+
6167
Waiting on the result (i.e., result iterator) returns the complete query result.
6268

6369
```typescript

examples/connect/main.cjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { connect } = require('ts-postgres');
2-
module.exports = connect().then((client) => {
2+
module.exports = (async () => {
3+
const client = await connect();
34
console.log('Encrypted: ' + client.encrypted);
4-
return client.end();
5-
});
5+
await client.end();
6+
})();

examples/connect/main.mts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
import { connect } from 'ts-postgres';
2-
const client = await connect();
2+
await using client = await connect();
33
console.log('Encrypted: ' + client.encrypted);
4-
await client.end();

examples/connect/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"devDependencies": {
33
"@types/node": "^20.0",
4-
"typescript": "^4.8"
4+
"typescript": "^5.2"
55
},
66
"dependencies": {
77
"ts-postgres": "^2.0"

examples/connect/v18/main.mts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { connect } from 'ts-postgres';
2+
const client = await connect();
3+
console.log('Encrypted: ' + client.encrypted);
4+
await client.end();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"license": "MIT",
2929
"engines": {
30-
"node": ">=18.0.0"
30+
"node": ">=18.0"
3131
},
3232
"scripts": {
3333
"lint": "eslint -c .eslintrc.json --ext .ts src test",

src/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,10 @@ export class ClientImpl {
759759
}
760760
}
761761

762+
[Symbol.asyncDispose]() {
763+
return this.end();
764+
}
765+
762766
private send() {
763767
if (this.mustDrain || !this.connected) return;
764768
this.sendUsing(this.writer);

0 commit comments

Comments
 (0)