3.0.1 • Published 3 years ago

psychopiggy v3.0.1

Weekly downloads
24
License
MIT
Repository
github
Last release
3 years ago

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);
}
3.0.1

3 years ago

3.0.0

3 years ago

2.0.8

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.4

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago