2.1.19 • Published 3 years ago

pg-database v2.1.19

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

pg-database

Overview

Handy wrapper for pg (formerly node-postgres) database connection instances featuring:

  • CRUD operation wrappers
  • named placeholders
  • query logging

You can import and instantiate database connection manually:

import Database from 'pg-database';
const database = new Database('<connection-uri>', console.log);

You can import an "automatically instantiated" database providing DB_URI=<connection-uri> and DB_LOG=<1|> environment variables:

import { database } from 'pg-database';

You can use this module as a simple drop-in replacement for pg.
Original pool, query and close methods are bound as they are.

import { database } from 'pg-database';

const client = await database.pool.connect();
client.query('LISTEN events');
client.on('notification', console.log);
client.on('error', console.error);
await client.release();

CRUD

Check out src/index.ts to get a picture of available methods: minimal and simple.

Named placeholders

You can use object for replacements (named placeholders) instead of array replacements (positional placeholders).
Placeholders must begin with : or $ and can contain only letters, numbers, underscores, dashes.

Before:

client.query('SELECT name FROM people WHERE name = $1', ['john'])

After:

client.query('SELECT name FROM people WHERE name = :name', { name: 'john' })

Query logging

You can log executed queries.

Either create your db instance with a customer logger:

const database = new Database('<connection-uri>', console.log);

Or use the automatic instance setting DB_URI=<connection-uri> and DB_LOG=1 env variables:

import { database } from 'pg-database';

Development

To run tests locally:

# start db
docker run --rm --name pg-database-test -p 5432:5432 --detach --env POSTGRES_USER=root --env POSTGRES_PASSWORD=root --env POSTGRES_DB=test postgres:14-alpine postgres -c log_statement=all

# run tests
yarn test

# remove db
docker rm -f pg-database-test
2.1.18

3 years ago

2.1.19

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.9

3 years ago

2.1.16

3 years ago

2.1.17

3 years ago

2.1.14

3 years ago

2.1.15

3 years ago

2.1.12

3 years ago

2.1.13

3 years ago

2.1.10

3 years ago

2.1.11

3 years ago

2.1.0

3 years ago

2.0.3

3 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago