Skip to content

Commit 26c368e

Browse files
authored
add docs and types for .reserve() (#667)
1 parent bf082a5 commit 26c368e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ async function insertUser({ name, age }) {
7979
* [Teardown / Cleanup](#teardown--cleanup)
8080
* [Error handling](#error-handling)
8181
* [TypeScript support](#typescript-support)
82+
* [Reserving connections](#reserving-connections)
8283
* [Changelog](./CHANGELOG.md)
8384

8485

@@ -1151,6 +1152,22 @@ prexit(async () => {
11511152
})
11521153
```
11531154

1155+
## Reserving connections
1156+
1157+
### `await sql.reserve()`
1158+
1159+
The `reserve` method pulls out a connection from the pool, and returns a client that wraps the single connection. This can be used for running queries on an isolated connection.
1160+
1161+
```ts
1162+
const reserved = await sql.reserve()
1163+
await reserved`select * from users`
1164+
await reserved.release()
1165+
```
1166+
1167+
### `reserved.release()`
1168+
1169+
Once you have finished with the reserved connection, call `release` to add it back to the pool.
1170+
11541171
## Error handling
11551172

11561173
Errors are all thrown to related queries and never globally. Errors coming from database itself are always in the [native Postgres format](https://www.postgresql.org/docs/current/errcodes-appendix.html), and the same goes for any [Node.js errors](https://nodejs.org/api/errors.html#errors_common_system_errors) eg. coming from the underlying connection.

types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,8 @@ declare namespace postgres {
683683
file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;
684684
file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, args: (ParameterOrJSON<TTypes[keyof TTypes]>)[], options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;
685685
json(value: JSONValue): Parameter;
686+
687+
reserve(): Promise<ReservedSql<TTypes>>
686688
}
687689

688690
interface UnsafeQueryOptions {
@@ -699,6 +701,10 @@ declare namespace postgres {
699701

700702
prepare<T>(name: string): Promise<UnwrapPromiseArray<T>>;
701703
}
704+
705+
interface ReservedSql<TTypes extends Record<string, unknown> = {}> extends Sql<TTypes> {
706+
release(): void;
707+
}
702708
}
703709

704710
export = postgres;

0 commit comments

Comments
 (0)