Psychopiggy is a thin wrapper around the excellent 'pg' module.
Adds these features:
- Named parameters
- Releases clients automatically
- Avoids transaction boilerplate
npm install psychopiggy
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', 'jeswin@example.com')`
);
}
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: "jeswin@example.com",
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', 'user1@example.com')`);
await client.query(`INSERT INTO account (
username, password, email) VALUES ('user2', 'secretive2', 'user2@example.com')`);
}, config);
}