-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
2,112 additions
and
3,089 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ A Postgres `pg` client based helper which: | |
- provides async query functions explicitly returning multiple rows, a single row, a scalar value or nothing | ||
- allows you to write SQL queries with named parameters (instead of positional parameters) | ||
- handles your connections | ||
- supports transactions (since v1.1.0) | ||
|
||
## Logging | ||
|
||
|
@@ -141,6 +142,41 @@ PG.query(query, ["John Doe", "[email protected]", true]); | |
|
||
**pg-client-helper** manages your database connection by utilizing connection pooling. It also supports connections to AWS RDS via IAM using AWS Signer. | ||
|
||
If you want to use transactions, please use the following approach: | ||
|
||
```ts | ||
import * as PG from "pg-client-helper"; | ||
|
||
async function myfunc() { | ||
const client: any = PG.beginTransaction(); // begins the transaction and returns a client to be used for ALL | ||
|
||
try { | ||
// 1st query | ||
const $id_mytable1 = await PG.query( | ||
`INSERT INTO mytable1 (val1) VALUES 'foo' RETURNING id_mytable1` | ||
); | ||
|
||
// 2nd query | ||
const $id_mytable2 = await PG.query( | ||
`INSERT INTO mytable2 (id_mytable1, val2) VALUES ($id_mytable1, 'bar')`, | ||
{ $id_mytable1 } | ||
); | ||
|
||
// 3rd query | ||
await PG.query( | ||
`UPDATE mytable3 SET val3 = 'baz' WHERE id_mytable1 = $id_mytable1 AND id_mytable2 = $id_mytable2`, | ||
{ $id_mytable1, $id_mytable2 } | ||
); | ||
|
||
await PG.commitTransaction(client); // commits all changes made since beginTransaction | ||
} catch (error) { | ||
if (client) { | ||
await PG.rollbackTransaction(client); // we faced an error after beginTransaction, roll back all changes since then | ||
} | ||
} | ||
} | ||
``` | ||
|
||
| Environment Variable | Description | | ||
| ----------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| PGHOST | **(Mandatory)** The hostname of your PostgreSQL server. This could be a local hostname, IP address, or a remote server address. | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.