1.0.1 • Published 4 years ago

@jusay/pg v1.0.1

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
4 years ago

pg-library

Build License

Lightweight Node.js PostgreSQL library

Getting Started

Installation

Install the package into your project with npm:

$ npm install @jusay/pg

Or with yarn:

$ yarn add @jusay/pg

Usage

The library exposes a function called waitForDb that does several things under the hood:

  • Waits for the database to be connected (throws an error if unable to connect)
  • Creates a patch_history table if it's not created
  • Figures out patches that need to be applied (SQL files that are inside the <rootDir>/db folder)

This function is useful if the database needs to be ready and up-to-date before performing some task, like spinning up a server.

const app = require("express")();
const {waitForDb} = require("@jusay/pg");
const port = 3000;

waitForDb({
  interval: 2000, // pings the db every 2 seconds (2000 ms)
  maxTries: 5, // pings the db for at most 5 times until connected
})
  .then(() => {
    app.listen(port, () => {
      console.log(`Server is listening on port ${port}`);
    });
  })
  .catch((err) => {
    // handle errors
  });

In order to connect to the database, you will need to either:

  • Set a DB_URL environment variable (e.g. postgres://user:psswrd@localhost:5432/dbname)
  • Set the following environmental variables
    • DB_HOST (defaults to "localhost" if not set)
    • DB_PORT (default to 5432 if not set)
    • DB_NAME
    • DB_USER
    • DB_PASSWORD

Changelog

See CHANGELOG.md

Contributing

See CONTRIBUTING.md

License

BSD 3-Clause

1.0.1

4 years ago

1.0.0

4 years ago