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-x

v1.0.2

Published

Robust helpers for 'pg', works with callbacks or promises.

Downloads

7

Readme

pg-x

Robust helpers for pg.

Build Status NPM

Synopsis

Using pg's in-built pool:

const pool = new pg.Pool({
  connectionString : 'postgres://pgx@localhost/pgx',
})

Then you can pass the pool (a pg.pool) to these functions:

// promise
const selCount = 'SELECT count(*) AS count FROM tablename'
const row = await pgx.one(client, selCount)

// callback
const selCount = 'SELECT count(*) AS count FROM tablename'
pgx.one(client, selCount, (err, row) => {
  if (err) throw err

  console.log(Row:', row || 'none')
})

You can also pass a pg.client instead of a pg.pool to any method, since they both have a .query() method that works the same.

// promise - checkout a client
pool.connect()
  .then((client, done) => {
    const selCount = 'SELECT count(*) AS count FROM tablename'
    return pgx.one(client, selCount)
  })
  .finally(done)
;

// callback - checkout a client
pool.connect((err, client, done) => {
  if (err) throw err

  // one
  const selCount = 'SELECT count(*) AS count FROM tablename'
  pgx.one(client, selCount, (err, row) => {
    done()
    // do something with error
    if (err) return null

    console.log(Row:', row || 'none')
  })
})

API

Note: poc means "pool or client" which is either a pg.pool or a pg.client.

.query(poc, query, callback)

Pass it a query much as you would with a pg.pool or a pg.client. The rows is equivalent to result.rows and result is exactly what is given back to us by pg.

It is just a convenience function around the pool.query() and client.query() functions to also get the rows out in one call. That's all. Perhaps this is better named just .query() to reflect the pool.query() or client.query() in pg.

// query
const insBlah = 'INSERT INTO blah(name) VALUES($1)'
const query = {
  text : insBlah,
  values : [ 'John Doe' ],
}
pgx.query(pool, query, (err, rows, result) => {
  if (err) throw err

  console.log(`${result.rowCount} rows were affected`)
})

.one(poc, query, callback)

Get one row from the results (or null if there are no rows returned).

Example:

// one
const selCount = 'SELECT count(*) AS count FROM tablename'
pgx.one(pool, selCount, (err, row) => {
  if (err) throw err
  console.log(Found row:', row || 'none')
})

.sel(poc, table, col, val, callback)

Get all matching rows from a table where col = val (or [] if nothing matches).

Example:

// sel
pgx.sel(pool, 'blog', 'account_id', 23, (err, rows) => {
  if (err) throw err

  // `rows` is always an array - but empty if there are no matching rows
  // It is never `null` or `undefined` unless there was an `err`.
  console.log(`There are ${rows.length} rows`)
})

.all(poc, query, callback)

Get all rows from a table (or [] if nothing matches).

Example with direct SQL:

// all
const selAll = 'SELECT * FROM tablename'
pgx.all(pool, selAll, (err, rows) => {
  if (err) throw err

  // `rows` is always an array - but empty if there are no matching rows
  // It is never `null` or `undefined` unless there was an `err`.
  console.log(`There are ${rows.length} rows`)
  console.log(rows)
})

Example using a query:

// all
const query = {
  text : 'SELECT * FROM tablename WHERE col = $1',
  values : [ 'bob' ],
}
pgx.all(pool, query, (err, rows) => {
  if (err) throw err

  // `rows` is always an array - but empty if there are no matching rows
  // It is never `null` or `undefined` unless there was an `err`.
  console.log(rows)
})

.get(poc, table, col, val, callback)

Gets one row from the table specified, or null if it doesn't exist.

Example, fetching a user called bob:

pgx.get(pool, 'user', 'username', 'bob', (err, row) => {
  if (err) throw err

  // here, `row` is either `null` or an object with the cols/values
  if ( !row ) {
    console.log('No user found')
    return
  }

  // yes, the user exists
  console.log('User:', row)
})

Using .get() is equivalent to using .one() with the following query:

// using `.get()`
pgx.get(pool, 'user', 'username', 'bob', console.log)

// using `.one()`
const query = {
  text: 'SELECT * FROM user WHERE username = $1',
  values: [ 'bob' ],
}
pgx.one(pool, query, console.log)

.ins(poc, table, obj, callback)

Inserts a row into the table, using the key/value pairs in the obj. Returns the same rows and results as .query() above.

const user = {
  username : 'chilts',
  email : '[email protected]',
  website : 'https://chilts.org',
}
pgx.ins(client, 'account', user, (err, rows, result) => {
  // ...
})

.upd(poc, tablename, col, val, obj, callback)

Updates one or more rows using the col/val pairs in obj and using WHERE col = val.

const newDetails = {
  email : '[email protected]',
}
pgx.upd(client, 'account', 'username', 'chilts', newDetails, (err, rows, result) => {
  // ...
})

.del(poc, tablename, col, val, callback)

Deletes one or more rows using the col/val pairs in obj and using WHERE col = val. you don't delete ALL rows.

pgx.del(client, 'account', 'username', 'chilts', (err, rows, result) => {
  // ...
})

Author

Written by Andrew Chilton:

License

ISC

(Ends)