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

v0.4.0

Published

A framework for running transactions using 'pg' (and getting them right).

Downloads

12

Readme

Pg Trans

A framework for running transactions using 'pg' (and getting them right).

The abilities of this package are:

  • start a transaction
  • run each statement in turn
  • check each result (and quit half-way if wanted)
  • munge each result if required
  • save the result of every statement
  • commit the transaction if everything was successful
  • rollback properly in case of error at any point
  • discard the client back to the pool in all success and error cases

Synopsis

var pg = require('pg')
var trans = require('pg-trans')(pg, conStr)

trans(
  [
    {
      name : 'create',
      sql  : 'CREATE TABLE foo(bar TEXT)',
    },
    {
      name : 'insert',
      sql : "INSERT INTO foo(bar) VALUES($1)",
      vals : [ 'Hello, World!' ],
    }
    {
      name : 'select',
      sql : 'SELECT * FROM foo',
    }
  ],
  function(err, result) {
    // check err as usual

    // result is equivalent to:
    var res = {
      begin  : [],
      create : [],
      insert : [],
      select : [{ bar : 'Hello, World!' }],
      commit : [],
    }
  }
)

Example

This example shows how to insert a name into the account table and an associated social_id into the social table. It remembers which account_id was created (from a sequence in the database) to use in the 2nd statement.

function signUp(name, social_id, callback) {
  var account_id
  trans(
    [
      {
        sql : 'INSERT INTO account(name) VALUES($1) RETURNING id AS account_id',
        vals : [ name ],
        res : function(rows) {
          account_id = rows[0].account_id
        },
      }
      {
        sql : 'INSERT INTO social(account_id, social_id) VALUES($1, $2) RETURNING id AS social_id',
        vals : function() {
          return [ account_id, social_id ]
        },
      }
    ],
    callback
  )
}

Actions

Each action must have the sql for the statement. All other attributes are optional:

  • sql (required) - the sql statement to be executed
  • vals (optional) - the values for the placeholders in the sql (default: [])
  • check (optional) - a function to run to check everything is ok (return a true value (an error) to rollback the transaction)
  • res (optional) - a function to run once the result is known

Note: the vals can also be a function which returns an array of vals.

The Process

The process goes through the following:

  1. a connection to the database is made
  2. a BEGIN is sent
  3. a loop through all actions is started:
  4. if vals is a function, it is called to retrieve the vals to use
  5. the sql and vals is sent
  6. the check function is called (if it exists). Return an error to stop the transaction and rollback (falsey to continue)..
  7. finally, the res function is called (if it exists). Whatever you return will be saved instead of the actual rows returned.
  8. a COMMIT is sent

If at any stage an error occurs, then a ROLLBACK is sent and the callback called with the error provided.

See all of the tests if you want to see how anything works.

History

  • v0.4.0 (2015-07-24)
    • pass all the results (so far) ino the vals() function
  • v0.3.0 (2015-07-19)
    • convert evverything to use a result object of arrays, not an array of arrays
    • updated docs
  • v0.2.0 (2015-07-18)
    • ability to call a '.check()' function at each stage
    • more tests
  • v0.1.0 (2015-07-17)
    • tests for most non-error paths
    • initial version

AUTHOR

Written by Andrew Chilton:

(Ends)