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

v1.0.0

Published

Extensions to pg

Downloads

1

Readme

pg-extended

node-postgres is a solid library that provides access to postgres through Node without pushing many opinions ono you. This library adds a few extensions to pg that are opinionated, but provide all of the key database functionality that I typically need in my projects.

pg-extended should be 100% compatible with pg and only adds a few functions to it.

let Pool = require('pg').Pool

//any connection information that doesn't come from environmental variables can be added 
//here in the Pool() call.
pool = require('pg-extended')(new Pool())

runInTransaction

pg-extended allows you to control transactions through javascript on the pool and on clients. runInTransaction takes a single function as an argument and passes it a database connection. A transaction is opened on that db connection before the function is called. If the function finishes normally, the transaction is committed and the functions return value is passed back through runInTransaction. If the function throws an exception, the transaction is rolled back and the exception is rethrown by runInTransaction.

let client = await pool.getClient()
try {
  let returnedValue = client.runInTransaction(async(client)=> {
    await client.query('INSERT INTO mytable VALUES (1);')
    return 'success string'
  })

  if (returnedValue = 'success string') {
    console.log('no exception was thrown, so the transaction was committed')
  }
} catch(e) {
  console.log('an exception was thrown, so the transaction was rolled backed')
}

client.release()

buildTempTable

pg has nice features for inserting a single row into a table, but it requires round trips to the DB server to insert multiple rows. In addition, programs often need to do set based validations before performing inserts, and these can be easier and more effecient when done on the database server.

buildTempTable offers an efficient way to load arbitrary numbers of records to the server for validation and processing. It takes 4 arguments, a table name, an array of objects, a default data schema (necessary when the data array has 0 records), and a boolean specifying if the table should be dropped on commit (defaults to false and only valuable when used inside a transaction).

All fields in the temp table have type TEXT. But, the savings in round trip time and transaction length, will outweight the costs of TEXT fields in the table in most use cases.

      const data = [
        {
          a: 'hi',
          b: '2'
        },
        {
          a: 3,
          b: 4
        }
      ]

      const schema = {a:1, b:1}

      await client.buildTempTable('tableName', data, false)

      await client.query(`INSERT INTO permanent_table
                          SELECT * from tt;`)

      client.release() //this destroys the temp table