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

@rdcl/pg

v2.0.1

Published

A wrapper around [node-postgres](https://node-postgres.com) which introduces some more convenient API's.

Downloads

4

Readme

@rdcl/pg

A wrapper around node-postgres which introduces some more convenient API's.

[[TOC]]

Usage

// One class is exported: PG. Its arguments correspond with the arguments of Client from node-postgres.
const { PG } = require('@rdcl/pg')
const pg = new PG({
  ssl: { rejectUnauthorized: false },
})

// Helper methods are exposed to easily perform queries and return their results.
// All methods are bound to the instance so you can safely extract them.
const { select, selectOne, execute, withDB } = pg

// All results are promises.
const list = () => select`
  select id, name
  from people
`

// In the `select` and `selectOne` methods, you may specify a row mapper.
// The query is converted to a prepared statement, interpolated values are passed along safely.
const insert = (name, age) => selectOne(row => row.id)`
  insert into people (id, name, age)
  values (uuid(), ${ name }, ${ age })
  returning id
`

// The withDb method allows you to do more complicated things.
// All helper methods are available on the `db` object.
// Again, all results are promises.
const updateAge = (id, age) => withDb(async db => {
  const result = await db.selectOne`
    select *
    from people
    where id = ${ id }
  `

  if (result) {
    await db.execute`
      update people
      set
        name = ${ result.name },
        age = ${ age }
    `
  }
})

// You may also specify error mappers to translate errors to domain specific errors.
const createTransaction = (account, amount) => selectOne(row => row.id, {
  errorMapper: {
    23503: () => new NoSuchAccount(`Account ${ account } does not exist`),
  },
})`
  insert into transactions (id, account, amount)
  values (uuid(), ${ account }, ${ amount })
  returning id
`

// Alternatively, you may also use postfix notation.
const { q } = pg
const get = () => q`
  select id, name
  from shapes
`.select()

Reference

new PG(...)

Takes the same arguments as Client from node-postgres. Returns an object which has a couple of convenience methods.

PG::select

Performs the provided query and returns the selected rows. Optionally takes a row mapper and an error mapper.

Examples

select`select 1`
select()`select 1`
select(row => row.id)`select 1 as id`
select(row => row, {
  123456: err => new Error('your error'),
})`select 1`

PG::selectOne

Same as PG::select, but only returns the first row (or undefined).

PG::execute

Pretty much the same as PG::select, but does not return anything (so does not accept a row mapper either).

PG::withDb

A wrapper method which takes a callback. This callback is called with a Connection object, which contains helper methods. This method will take care of setting up a database connection, and cleaning it up afterwards (even if there were errors).

PG::q

Returns a QuerySpecification object, which has select, selectOne and execute methods which function identically to the functions described above.

Examples

withDb(async db => {
  // do your database stuff here
})

Connection::client

The node-postgres client.

Connection::query

Low level wrapper for queries, which return the original QueryResult.

Examples

withDb(async db => {
  const result1 = await db.query`...`
  const result2 = await db.query()`...`
  const result3 = await db.query({
    errorMapper: {
      // ...
    },
  })
})

Connection::select, Connection::selectOne, Connection::execute

Identical to the top level methods, except that these do not open a new connection (where the top level methods do).