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

pg.promised

v2.0.2

Published

Wraps around a `pg` or `pg.native` instance and converts it to use Promises for asynchronous flow control instead of callbacks.

Downloads

1

Readme

pg.promisied: A Promise wrapper for pg.

Wraps around a pg or pg.native instance and converts it to use Promises for asynchronous flow control instead of callbacks.

Quickstart:

// pg.promised works with either `pg` or `pg-native`
const pg = require("pg.promised")(require("pg-native"));

// Always make sure to call done() when finished with the connection, as per
// pg's docs.
function something1 () {
    return pg.connect("postgres://...")
    .then(({ client, done }) => {
        return client.query("SELECT * FROM foo")
        .then(result => {
            done();
            console.log(result);
        })
        ;
    })
    ;
}

// As that gets tedious, a small wrapper function `using()` has been added to
// the pg object. The following function is functionally-equivalent to the
// above:
function something2 () {
    return pg.using("postgres://...", client => client.query("SELECT * FROM foo"))
    .then(result => console.log(result))
    ;
}

Documentation:

wrap (default function exported)

wrap(pg: (pg|pg-native), ?PromiseImpl: (Promise|Bluebird|...)): pg.promised

First argument is required and should be either require("pg") or require("pg-native"). Second argument is optional, it is the Promise implementation to be used. If this argument is null or undefined it will default to global.Promise.

Example:

// To Use default global.Promise
const pg = require("pg.promised")(require("pg"));

// To Use Bluebird
const pg = require("pg.promised")(require("pg"), require("bluebird"));

pg.promised

All methods an properties are inherited from the pg object given to wrap. The following changes have been made:

pg.promised .connect

pg.connect(...args): Promise<{ client: pg.promised.Client, done: done() }>

.connect returns a promise instead of accepting a callback as it's final argument. All arguments to .connect are proxied to the pg object received by wrap. Consult node-postgres for more information on those specifics. It returns a Promise that resolves to a two-item object, a pg.promised.Client instance and a done function. As per the node-postgres docs, you must call done() as soon as you're done with the connection for it to go back into the pool.

Example:

pg.connect("postgres://...")
.then(({ client, done }) => {
    return client.query("SELECT * FROM foo")
    .then(results => {
        done();
        return results;
    })
    ;
})
.then(results => console.log(results.rows))
.catch(e => console.error(e))
;

pg.promised .using

pg.using(...args, cb: (Function<client: pg.promised.Client>: Promise)): Promise<(cb's return value)>

.using is a small helper that takes in everything a .connect call would but also accepts a final object, a callback that returns a promise. This callback receives the client instance and once the Promise returned by the callback resolves done() will automatically be called for that client. The Promise returned by cb will be returned from run.

Example:

pg.run("postgres://...", client => client.query("SELECT * FROM foo"))
.then(results => console.log(results.rows))
.catch(e => console.error(e))
;

pg.promised.Client

This is a Promised-wrapped class equivalend to the Client class of the pg object passed to wrap. All methods and properties have been inherited from that class, any differences are marked in this section:

pg.promised.Client .query

client.query(...args): Promise<results>

.query accepts all arguments given to the original client.query but wraps the value returned in a Promise. Note that this means that you'll need to use a different function recevie an event emitter from this function, if you care to do so.

pg.promised.Client .queryEmitter

client.query(...args): [node-postgres's result emitter]

No Promise-wrapping here, but it is needed since the original pg or pg-native modules have a Client object whose query method will return a specialized event emitter if the callback is omitted. Since the wrapped version of query removes this functionality, this brings it back.

Compared to pg-promise

pg.promised is much lower-level than pg-promise. pg.promised is simply the smallest set of changes needed to have the excellent node-postgres library use Promises for flow control instead of callbacks. pg-promise is very full-featured and adds a lot to node-postgres but I wanted something much simpler.