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

postgres-muchos

v1.0.4

Published

A Postgresql Class for pools

Downloads

14

Readme

PostgresMuchos

Build StatusInline docsnpm version

Synopsis

PostgresMuchos is an ES6 class for managing a pool of postgresql connections. All methods return native promises. This class utilizes the node-postgres and node-pool modules internally. Note: The version of node-pool (generic-pool) included with pg is not used.

Installation

npm install postgres-muchos --save

Documentation

See the JSDoc for extensive documentation.

Instanciating the class.

let PostgresMuchos = require('postgres-muchos');
let db = new PostgresMuchos (dbConfig, poolConfig, <optional> emitControl);

The dbConfig object contains the database connection info:

  • host: string - The host to connect to
  • user: string - user name
  • password: string - password
  • port: number (optional = 5432) - port number
  • database: string (optional) - database

The poolConfig object determines the pool behavior. See the node-pool documentation for the complete list. Here are some of the common properties:

  • min: number (optional = 0) - The minimum number of connections to acquire.
  • max: number (optional = 10) - The max number of connections to acquire.
  • idleTimeout: number (optional = 30000) - The number of milliseconds of inactivity before reaping client.
  • acquireTimeoutMillis: number (optional = 1000) - How many milliseconds of unsuccessful connection attempts before giving up.

The emitControl object allows the user to determine which events the class fires. The default is to not fire any events.

  • connect: boolean (optional = false) - Emit an event with every acquistion.
  • disconnect: boolean (optional = false) - Emit an event with every acquistion release.
  • query: boolean (optional = false) - Emit an event with every submission of a query.
  • resutls: boolean (optional = false) - Emit an event with every results return.

Query the database

db.query('select now() as thedate')
.then( (results) => {
    console.log(results.rows);
    // [ anonymous { thedate: 2016-11-04T18:12:50.634Z } ]
    return db.query('select $1::integer as thenumber', [100]);
})
.then ( (results) => {
    console.log(results.rows);
    // [ anonymous { thenumber: 100 } ]
    return db.close();
})
.then ( () => {
    console.log('normal exit');
})
.catch( (err) => {
    db.close.then();
    console.log(err);  
});

The query method returns a promise. If sql was sent, then the resolved promise contains a results object. The actual data is in the results.rows property which is an array of objects with the column names being the property names.

Destroy the pool

db.close().then( () => {
    // it's closed
})
.catch( (err) -> {
    // probably closed already
});

Events

The class emits 4 different events. The contents of each event is an object. The motive behind providing events was to ease in debugging. Multi-threaded appplications with database pools, can be difficult to debug. For normal processing, the events are not very useful.

connect:
{ event: 'connect', processID: 6872 }
disconnect:
{ event: 'disconnect', processID: 6872 }
query:
{ event: 'query',
  sql: 'select $1::integer as thenumber',
  parms: [ 100 ],
  processID: 6872 }
results:
{ event: 'results',
  elapsedTime: 2,
  sql: 'select $1::integer as thenumber',
  parms: [ 100 ],
  data: [ anonymous { thenumber: 100 } ],
  processID: 6872 }