Skip to content

Commit

Permalink
add docs and types for .reserve()
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Aug 28, 2023
1 parent 838c8da commit 77e0dd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,22 @@ prexit(async () => {
})
```

## Reserving connections

### `await sql.reserve()`

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.

```ts
const reserved = await sql.reserve()
await reserved`select * from users`
await reserved.release()
```

### `reserved.release()`

Once you have finished with the reserved connection, call `release` to add it back to the pool.

## Error handling

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.
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,8 @@ declare namespace postgres {
file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;
file<T extends readonly any[] = Row[]>(path: string | Buffer | URL | number, args: (ParameterOrJSON<TTypes[keyof TTypes]>)[], options?: { cache?: boolean | undefined } | undefined): PendingQuery<T>;
json(value: JSONValue): Parameter;

reserve(): Promise<ReservedSql<TTypes>>
}

interface UnsafeQueryOptions {
Expand All @@ -699,6 +701,10 @@ declare namespace postgres {

prepare<T>(name: string): Promise<UnwrapPromiseArray<T>>;
}

interface ReservedSql<TTypes extends Record<string, unknown> = {}> extends Sql<TTypes> {
release(): void;
}
}

export = postgres;

0 comments on commit 77e0dd0

Please sign in to comment.