psychopiggy
v3.0.1
Published
Psychopiggy is a thin wrapper around the excellent 'pg' module.
Downloads
18
Readme
psychopiggy
Psychopiggy is a thin wrapper around the excellent 'pg' module.
Adds these features:
- Named parameters
- Releases clients automatically
- Avoids transaction boilerplate
Installation
npm install psychopiggy
Usage
Here's how to run a simple query
import * as pg from "psychopiggy";
// connection config
const config = {
database: "dbname",
host: "hostname",
user: "dbusername",
password: "password",
port: 5432
};
// Using pools.
async function createAccount() {
pg.createPool(config);
const pool = pg.getPool(config);
const { rows } = await pool.query(
`INSERT INTO account (
username, password, email) VALUES ('jeswin', 'secretive', '[email protected]')`
);
}
A simple Select query
async function getUsers() {
const pool = pg.getPool(config);
const params = new pg.Params({
username: "jeswin"
});
const { rows } = await pool.query(
`SELECT * FROM "appusers" WHERE username=${params.id("username")}`,
params.values()
);
}
Insert Statements
async function createAccount() {
const pool = pg.getPool(config);
const params = new pg.Params({
email: "[email protected]",
password: "secretive",
username: "jeswin"
});
const { rows } = await pool.query(
`INSERT INTO account (${params.columns()}) VALUES (${params.ids()})`,
params.values()
);
}
Transactions. If there's an exception, everything within the transaction is rolled back automatically.
async addTwoUsers() {
pg.createPool(config);
const pool = pg.getPool(config);
await pg.withTransaction(async client => {
await client.query(`INSERT INTO account (
username, password, email) VALUES ('user1', 'secretive1', '[email protected]')`);
await client.query(`INSERT INTO account (
username, password, email) VALUES ('user2', 'secretive2', '[email protected]')`);
}, config);
}