Skip to content

Commit 28249fb

Browse files
authored
Merge pull request #150 from ebebbington/update-docs
[update-docs] Add further documentation
2 parents a460cad + 1d62518 commit 28249fb

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

docs/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,47 @@ async function main() {
2929
main();
3030
```
3131

32+
## Connection Management
33+
34+
You are free to create your 'clients' like so:
35+
36+
```typescript
37+
const client = new Client({
38+
...
39+
})
40+
await client.connect()
41+
```
42+
43+
But for stronger management and scalability, you can use **pools**:
44+
```typescript
45+
import { Pool } from "https://deno.land/x/[email protected]/mod.ts";
46+
import { PoolClient } from "https://deno.land/x/[email protected]/client.ts";
47+
48+
const POOL_CONNECTIONS = 20;
49+
const dbPool = new Pool({
50+
user: "user",
51+
password: "password",
52+
database: "database",
53+
hostname: "hostname",
54+
port: 5432,
55+
}, POOL_CONNECTIONS);
56+
57+
function runQuery (query: string) {
58+
const client: PoolClient = await dbPool.connect();
59+
const dbResult = await client.query(query);
60+
client.release();
61+
return dbResult
62+
}
63+
64+
runQuery("SELECT * FROM users;");
65+
runQuery("SELECT * FROM users WHERE id = '1';");
66+
```
67+
68+
This improves performance, as creating a whole new connection for each query can be an expensive operation.
69+
With pools, you can keep the connections open to be re-used when requested (`const client = dbPool.connect()`). So one of the active connections will be used instead of creating a new one.
70+
71+
The number of pools is up to you, but I feel a pool of 20 is good for small applications. Though remember this can differ based on how active your application is. Increase or decrease where necessary.
72+
3273
## API
3374

3475
`deno-postgres` follows `node-postgres` API to make transition for Node devs as easy as possible.
@@ -83,3 +124,16 @@ const result = await client.query({
83124
});
84125
console.log(result.rows);
85126
```
127+
128+
Interface for query result
129+
130+
```typescript
131+
import { QueryResult } from "https://deno.land/x/[email protected]/query.ts";
132+
133+
const result: QueryResult = await client.query(...)
134+
if (result.rowCount > 0) {
135+
console.log("Success")
136+
} else {
137+
console.log("A new row should have been added but wasnt")
138+
}
139+
```

0 commit comments

Comments
 (0)