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

pg2

v0.0.1

Published

A PostgreSQL driver for node.js that focuses on performance

Downloads

192

Readme

Description

A PostgreSQL driver for node.js that focuses on performance.

Requirements

Install

npm install pg2

Examples

  • Buffered SELECT query:
var Client = require('pg2');

var c = new Client({
  host: '127.0.0.1',
  user: 'foo',
  password: 'bar',
  db: 'test'
});

c.query('SELECT i FROM generate_series(1, 10) AS i', (err, rows) => {
  if (err)
    throw err;
  console.dir(rows);
});

c.end();
  • Streamed SELECT query:
var Client = require('pg2');

var c = new Client({
  host: '127.0.0.1',
  user: 'foo',
  password: 'bar',
  db: 'test'
});

c.query('SELECT i FROM generate_series(1, 10) AS i');
 .on('data', (result) => {
  result.on('data', (row) => {
    console.dir(row);
  });
});

c.end();
  • Using arrays (faster) instead of objects for rows:
var Client = require('pg2');

var c = new Client({
  host: '127.0.0.1',
  user: 'foo',
  password: 'bar',
  db: 'test'
});

c.query('SELECT i FROM generate_series(1, 10) AS i', { arrays: true }, (err, rows) => {
  if (err)
    throw err;
  console.dir(rows);
});

c.end();
  • Using positional parameters in a query:
var Client = require('pg2');

var c = new Client({
  host: '127.0.0.1',
  user: 'foo',
  password: 'bar',
  db: 'test'
});

c.query('SELECT i FROM generate_series($1::numeric, $2::numeric) AS i',
        [1, 10],
        (err, rows) => {
  if (err)
    throw err;
  console.dir(rows);
});

c.end();

API

require('pg2') returns a Client object

Client properties

  • connected - boolean - true if the Client instance is currently connected to the server.

  • backendParams - object - Once authenticated, this value will contain any backend status values. If no such values have been received, the value of this property will be null.

  • key - object - Once authenticated, this object will contain two properties (pid and key) which is used to uniquely identify this connection on the server.

  • status - integer - Indicates the backend transaction status. Valid values:

    • 73 - Not in a transactional block
    • 84 - In a transactional block
    • 69 - In a failed transactional block (queries will be rejected until block is ended)

Client events

  • ready() - A connection to the server has been established and authentication was successful.

  • error(< Error >err) - An error occurred at the connection level.

  • close() - The connection has been closed.

Client methods

  • (constructor)([< object >config]) - Creates and returns a new Client instance. Valid config options include:

    • user - string - Username for authentication. Default: (OS username of current logged in user)

    • password - string - Password for authentication. Default: ''

    • host - string - Hostname or IP address of the server. Default: 'localhost'

    • port - integer - Port number of the server. Default: 5432

    • db - string - A database to automatically select after authentication. Default: ''

    • keepalive - mixed - If true, this enables TCP keepalive probes using the OS default initial delay value. If a number, this both enables TCP keepalive probes and sets the initial delay value to the given value. Default: true

    • streamType - string - Set to 'normal' to use node's full-featured streams instead of simpler streams. The main difference is that the simpler streams do not implement .read() support. Default: 'simple'

  • query(< string >query[, < array >values][, < object >options][, < function >callback]) - mixed - Enqueues the given query. If callback is not supplied, a ResultStream instance is returned. values can be an array of values that correspond to positional placeholders inside query. Valid options are:

    • arrays - boolean - When true, arrays are used to store row values instead of an object keyed on column names. (Note: using arrays performs better) Default: false

    • hwm - integer - This is the highWaterMark to use for RowStream instances emitted by a ResultStream instance. This only applies when streaming rows. Default: (node's default) 16

  • connect([< function >callback]) - (void) - Explicitly attempts to connect to the server. Note that calling query() will implicitly attempt a connection if one is not already made. If not connected, callback is added as a one-time 'ready' event listener.

  • end() - (void) - Closes the connection once all queries in the queue have been executed.

  • destroy() - (void) - Closes the connection immediately, even if there are other queries still in the queue. Any/all queries still in the queue are properly notified.

ResultStream is an object stream that emits RowStream instances. In the case of multiple statements/queries passed to query() (separated by ;), there will be one RowStream instance for each statement/query.

RowStream is an object stream that emits rows.

TODO (in no particular order)

  • Allow passing back of data type OIDs for columns sent by the server

  • client.abort() that implicitly opens a new, temporary connection to kill the currently running query

  • COPY data support (both in and out)