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

postgresql-easy

v1.3.0

Published

A Node package for interacting with a PostgreSQL database

Downloads

2,067

Readme

postgresql-easy

Build Status

This is a Node module that makes it very simple to interact with PostgreSQL databases. It has the same API as https://github.com/mvolkmann/mysql-easier.

To install this, run npm install -S postgresql-easy

Setup

const PgConnection = require('postgresql-easy');
const pg = new PgConnection(config);

Demo

To run the demo code, follow these steps:

  1. Start database daemon with pg_ctl -D /usr/local/var/postgres start
  2. Create the demo database with createdb demo
  3. Start interactive mode with psql -d demo
  4. Create a table with create table demo_user ( id serial primary key, username text, password text );
  5. Exit interactive mode with ctrl-d.
  6. Run the demo with npm run demo

API

The config object can contain these properties:

  • database: the name of the database to use
  • debug: true to output messages describing each action; defaults to false
  • host: defaults to localhost
  • idleTimeoutMillis: time before connection is closed; default is 30000
  • max: maximum number of clients in pool; default is 10
  • password: if database requires authentication
  • port: defaults to 5432
  • user: if database requires authentication

The only one of these that is always required is "database".

PgConnection objects provide seven methods. All but disconnect return a promise. One way to use the returned promise is to chain calls to then and catch. Another is to use async and await.

deleteAll

This deletes all records from a given table.

try {
  await pg.deleteAll('flavors');
  // Do something after successful delete.
} catch (e) {
  // Handle the error.
}

deleteById

This deletes a record from a given table by id. It requires the table to have a column named "id".

try {
  await pg.delete('flavors', 7);
  // Do something after successful delete.
} catch (e) {
  // Handle the error.
}

disconnect

This disconnects from the database.

pg.disconnect();

getAll

This gets all records from a given table.

try {
  const result = await pg.getAll('flavors');
  // Process data in the array result.
} catch (e) {
  // Handle the error.
}

getById

This gets a record from a given table by id. It requires the table to have a column named "id".

try {
  const result = await pg.getById('flavors', 7);
  // Process data in the array result.
} catch (e) {
  // Handle the error.
}

insert

This inserts a record into a given table and returns the id of the new record. The keys of obj are column names and their values are the values to insert.

try {
  const result = pg.insert('flavors', {name: 'vanilla', calories: 100});
  // Do something after successful insert.
  // result will be the id of the newly inserted row.
} catch (e) {
  // Handle the error.
}

query

This executes a SQL query. It is the most general purpose function provided. It is used by several of the other functions.

const sql = 'select name from flavors where calories < 150';
try {
  const result = await pg.query(sql);
  // Do something with the result set in result.
} catch (e) {
  // Handle the error.
}

const sql = 'select name from flavors where calories < $1 and cost < $2';
try {
  const result = await pg.query(sql, 200, 3);
  // Do something with the result set in result.
} catch (e) {
  // Handle the error.
}

updateById

This updates a record in a given table by id. It requires the table to have a column named "id".

try {
  const result = pg.updateById(
    'flavors', 7, {name: 'chocolate', calories: 200});
  // Do something with the result set in result.
  // result will be an object describing the updated row.
} catch (e) {
  // Handle the error.
}