npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pga

v2.1.6

Published

A convenience wrapper around the pg module's Pool object.

Downloads

6

Readme

pga

npm Node.js Build Status Coverage Dependencies Status devDependencies Status MIT licensed

A convenience wrapper around the pg module's Pool object.

Installation

npm install --save pga

Usage

const pga = require('pga');

var db = pga({
  user:     'postgres',
  password: '',
  database: 'postgres',
  host:     'localhost',
  port:     5432,
  max:      10
});

API

#close

Closes the database connection. An alias for Pool.end.

db.close();

#parallel

Performs a series of parameterized queries in parallel over multiple connections from the underlying pool.

Query execution order is arbitrary, but the results are returned in the expected order.

If a single query results in an error, parallel will immediately execute the specified callback function with both the error and potentially a partial array of results from other successful queries. When returning a Promise, one error among any of the queries will result in the Promise being rejected.

parallel is provided as a means of efficiently performing multiple SELECT queries at once rather than one after the other in sequence. Do not use parallel to execute grouped SQL queries that may potentially alter the database- use transact instead!

// Parallelized queries with a callback function.
db.parallel([
  'SELECT COUNT(*) FROM test;',
  { text: 'SELECT * FROM test WHERE id = $1::int;', values: [ 1 ] },
  'SELECT * FROM test;'
], function(error, results) {
  if (error) {
    return console.error(error);
  }
  console.log(results);
});

// Parallelized queries that return a Promise object.
db.parallel([
  'SELECT COUNT(*) FROM test;',
  { text: 'SELECT * FROM test WHERE id = $1::int;', values: [ 1 ] },
  'SELECT * FROM test;'
]).then(function(results) {
  console.log(results);
}).catch(function(error) {
  console.error(error);
});

pga also accepts comma-separated queries and optional callback functions for parallel queries if passing in an array is too unwieldy:

db.parallel(
  'SELECT COUNT(*) FROM test;',
  { text: 'SELECT * FROM test WHERE id = $1::int;', values: [ 1 ] },
  'SELECT * FROM test;',
  function(error, results) {
    if (error) {
      return console.error(error);
    }
    console.log(results);
  });

#query

Performs a single parameterized query. An alias for Pool.query.

// A regular query with a callback function.
db.query('SELECT * FROM test;', function(error, result) {
  if (error) {
    return console.error(error);
  }
  console.log(result.rows);
});

// A regular query with a parameter and callback function.
db.query('SELECT * FROM test WHERE id = $1::int;', [ 1 ], function(error, result) {
  if (error) {
    return console.error(error);
  }
  console.log(result.rows);
});

// A regular query that returns a Promise object.
db.query('SELECT * FROM test;').then(function(result) {
  console.log(result.rows);
}).catch(function(error) {
  console.error(error);
});

// A regular query with a parameter that returns a Promise object.
db.query('SELECT * FROM test WHERE id = $1::int;', [ 1 ]).then(function(result) {
  console.log(result.rows);
}).catch(function(error) {
  console.error(error);
});

#transact

Performs a database transaction on an array of parameterized queries.

// A transaction with a callback function.
db.transact([
  'SELECT COUNT(*) FROM test;',
  { text: 'SELECT * FROM test WHERE id = $1::int;', values: [ 1 ] },
  { text: 'INSERT INTO test (name) VALUES ($1:text);', values: [ 'Name!' ] },
  'SELECT COUNT(*) FROM test;'
], function(error, results) {
  if (error) {
    return console.error(error);
  }
  console.log(results);
});

// A transaction that returns a Promise object.
db.transact([
  'SELECT COUNT(*) FROM test;',
  { text: 'SELECT * FROM test WHERE id = $1::int;', values: [ 1 ] },
  { text: 'INSERT INTO test (name) VALUES ($1:text);', values: [ 'Name!' ] },
  'SELECT COUNT(*) FROM test;'
]).then(function(results) {
  console.log(results);
}).catch(function(error) {
  console.error(error);
});

pga also accepts comma-separated queries and optional callback functions for transactions if passing in an array is too unwieldy:

db.transact(
  'SELECT COUNT(*) FROM test;',
  { text: 'INSERT INTO test (name) VALUES ($1:text);', values: [ 'Name!' ] },
  'SELECT COUNT(*) FROM test;',
  function(error, results) {
    if (error) {
      return console.error(error);
    }
    console.log(results);
  });

Extras