Skip to content

Commit

Permalink
Document using keyword syntax (#475)
Browse files Browse the repository at this point in the history
* doc: Pool connection handling using `using` keyword

* 0.19.3
  • Loading branch information
langpavel authored Mar 11, 2024
1 parent 499143f commit 2606e50
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lock": false,
"name": "@bartlomieju/postgres",
"version": "0.19.2",
"version": "0.19.3",
"exports": "./mod.ts"
}
37 changes: 30 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,12 @@ const dbPool = new Pool(
POOL_CONNECTIONS,
);

const client = await dbPool.connect(); // 19 connections are still available
await client.queryArray`UPDATE X SET Y = 'Z'`;
client.release(); // This connection is now available for use again
// Note the `using` keyword in block scope
{
using client = await dbPool.connect();
// 19 connections are still available
await client.queryArray`UPDATE X SET Y = 'Z'`;
} // This connection is now available for use again
```

The number of pools is up to you, but a pool of 20 is good for small
Expand Down Expand Up @@ -515,9 +518,9 @@ await client_3.release();

#### Pools made simple

The following example is a simple abstraction over pools that allows you to
execute one query and release the used client after returning the result in a
single function call
Because of `using` keyword there is no need for manually releasing pool client.

Legacy code like this

```ts
async function runQuery(query: string) {
Expand All @@ -532,7 +535,27 @@ async function runQuery(query: string) {
}

await runQuery("SELECT ID, NAME FROM USERS"); // [{id: 1, name: 'Carlos'}, {id: 2, name: 'John'}, ...]
await runQuery("SELECT ID, NAME FROM USERS WHERE ID = '1'"); // [{id: 1, name: 'Carlos'}, {id: 2, name: 'John'}, ...]
await runQuery("SELECT ID, NAME FROM USERS WHERE ID = '1'"); // [{id: 1, name: 'Carlos'}]
```

Can now be written simply as

```ts
async function runQuery(query: string) {
using client = await pool.connect();
return await client.queryObject(query);
}

await runQuery("SELECT ID, NAME FROM USERS"); // [{id: 1, name: 'Carlos'}, {id: 2, name: 'John'}, ...]
await runQuery("SELECT ID, NAME FROM USERS WHERE ID = '1'"); // [{id: 1, name: 'Carlos'}]
```

But you can release pool client manually if you wish

```ts
const client = await dbPool.connect(); // note the `const` instead of `using` keyword
await client.queryArray`UPDATE X SET Y = 'Z'`;
client.release(); // This connection is now available for use again
```

## Executing queries
Expand Down

2 comments on commit 2606e50

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No typecheck tests failure

This error was most likely caused by incorrect type stripping from the SWC crate

Please report the following failure to https://github.com/denoland/deno with a reproduction of the current commit

Failure log

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No typecheck tests failure

This error was most likely caused by incorrect type stripping from the SWC crate

Please report the following failure to https://github.com/denoland/deno with a reproduction of the current commit

Failure log

Please sign in to comment.