psychopiggy
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

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', '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);
}

Readme

Keywords

none

Package Sidebar

Install

npm i psychopiggy

Weekly Downloads

1

Version

3.0.1

License

MIT

Unpacked Size

12.7 kB

Total Files

9

Last publish

Collaborators

  • jeswin