Skip to content

Commit

Permalink
Merge pull request #110 from malthe/async-dispose-prep-stmt
Browse files Browse the repository at this point in the history
Add async dispose to prepared statement
  • Loading branch information
malthe authored Feb 29, 2024
2 parents e889223 + a6d9952 commit f9f6f5c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ In next release ...
type instead of a value.

- Add `[Symbol.asyncDispose]` method to support [Explicit Resource
Management](https://github.com/tc39/proposal-explicit-resource-management).
Management](https://github.com/tc39/proposal-explicit-resource-management). This
works on the client object as well as prepared statements.

- Add `off` method to disable event listening.

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ When the prepared statement is no longer needed, it should be closed to release
```typescript
await statement.close();
```
With TypeScript 5.2+ it's also possible to use the `await using` construct which automatically closes the statement at the end of the block.


Prepared statements can be used (executed) multiple times, even concurrently.

Expand Down
23 changes: 13 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface Notification {
payload: string;
}

export interface PreparedStatement<T = ResultRecord> {
export interface PreparedStatement<T = ResultRecord> extends AsyncDisposable {
close: (portal?: string) => Promise<void>;
execute: (
values?: any[],
Expand Down Expand Up @@ -535,16 +535,19 @@ export class ClientImpl {
const types = this.parameterDescriptionQueue.shift();
this.cleanupQueue.expect(Cleanup.ParameterDescription);

const close = () => {
return new Promise<void>((resolve) => {
this.writer.close(providedNameOrGenerated, 'S');
this.closeHandlerQueue.push(resolve);
this.cleanupQueue.push(Cleanup.Close);
this.writer.flush();
this.send();
});
};

resolve({
close: () => {
return new Promise<void>((resolve) => {
this.writer.close(providedNameOrGenerated, 'S');
this.closeHandlerQueue.push(resolve);
this.cleanupQueue.push(Cleanup.Close);
this.writer.flush();
this.send();
});
},
[Symbol.asyncDispose]: close,
close,
execute: (
values?: any[],
portal?: string,
Expand Down
1 change: 1 addition & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ describe('Query', () => {
deepEqual(result1, { names: ['i'], rows: [[1]], status: 'SELECT 1' });
const result2 = await stmt.execute([2]);
deepEqual(result2.rows, [[2]]);
deepEqual(stmt.close, stmt[Symbol.asyncDispose]);
await stmt.close();
});

Expand Down

0 comments on commit f9f6f5c

Please sign in to comment.