pg-Lazy
Simple functional helpers for node-postgres.
Requires Node >= ^10.0.0 and node-postgres ^8.0.0
Breaking Changes from v2.x.x to v3.x.x
Pool
andClient
are no longer an instance ofpg._Pool
andpg._Client
respectively.isConnected
is renamed withcanConnect
- Now user proper ES6 class extends.
Breaking Changes from v1.x.x to v2.x.x
- Due to new es6 codes, this module now requires Node v8.1.4 and above.
- This module no longer mutates pg.Pool and pg.Client, it instead
extends
them and store them aspg._Pool
andpg._Client
- It no longer automatically initialize the
Pool
unless a third Object argument is passed{singleton:true}
pg-Lazy
now returns a default Object{ pg, Pool, Client, sql, _raw }
in whichPool
is an instance ofpg._Pool
and Client is an instance ofpg._Client
. To get the originalpg.Pool
andpg.Client
instances, you can usepg
to access them.- If
{singleton:true}
is passed as a third argument, it then addspool
from the returned Object. Thispool
is an already-initializedpg._Pool
- Read more changes here ChangeLog
Installation
npm install pg-lazy pg --save
or yarn add pg-lazy pg
Usage
Manual Pool initialization:
const pgLazy = ;// create your configurationconst connectionString = 'postgres://localhost:5432/pg_test';// pool instance is no longer initiated, you must initialize it using pg.Pool.const Pool sql _raw pg = ;const pool = { // regular query return pool; // many for more than 1 result return pool; // one for single result return pool; // none for no result return pool;}{ const username = await }
Automatic Pool initialization:
const pgLazy = ;// create your configurationconst connectionString = 'postgres://localhost:5432/pg_test';// pool instance is automatically initialized when passing {singleton:true}const pool sql _raw pg = ; { // regular query return pool; // many for more than 1 result return pool; // one for single result return pool; // none for no result return pool;}{ const username = await }
Helpers
-
pg.Pool with prototype methods
query
,many
,one
,none
,withTransaction
,canConnect
. -
pg.Client with prototype methods
query
,many
,one
,none
,canConnect
. -
Extends both with
.prepared(name).{query,many,one}()
-
All methods returns a Promise
-
Automatically defaults to Environment variables for DB config, that means you can also set your DB config via
process.env
-
Configures the client parser to parse postgres ints and numerics into javascript numbers (else
SELECT 1::int8
would return a string "1"). -
Accepts String, Objects and connectionString for configuration,
-
Exposes
sql
and_raw
template literal helpers for writing queries.const uname = 'nisha42'const key = 'uname'const direction = 'desc'await pool -
All query methods fail if the query you pass in is not built with the
sql
or_raw
tag. This avoids the issue of accidentally introducing sql injection with template literals. If you want normal template literal behavior (dumb interpolation), you must tag it with_raw
.
Example
const pgLazy = ;const url = 'postgres://user:pass@localhost:5432/my-db'const pool sql _raw pg = ;exports {return pool}exports {return pool}exports {return pool}
Check more examples on the Test folder
Query template tags
pg-extra forces you to tag template strings with sql
or _raw
.
You usually use sql
.
sql
is a simple helper that translates this:
sql`SELECT *FROM usersWHERE lower(uname) = lower()AND faveFood = ANY ()`
into the sql bindings object that node-postgres expects:
text: `SELECT *FROM usersWHERE lower(uname) = lower($1)AND faveFood = ANY ($2)`values: 'nisha42' 'kibble' 'tuna'
_raw
is how you opt-in to regular string interpolation, made ugly
so that it stands out.
Use .append()
to chain on to the query. The argument to .append()
must also be tagged with sql
or _raw
.
sql` ` // '$1 $2 baz'_raw` ` // 'foo bar $1'
Test
Setup local postgres database with seeded rows that the tests expect:
- psql -c 'create user lazy_test_user with password '"'lazy_test_pw'"';' -U postgres
- psql -c 'create database lazy_test owner lazy_test_user;' -U postgres
- psql -d lazy_test -c 'create table bars (n int not null);' -U lazy_test_user
- psql -d lazy_test -c 'insert into bars (n) values (1), (2), (3);' -U lazy_test_user
Then run the tests:
`yarn test` or `npm test`
Changelog
Shouts
- Heavily inspired by pg-extra.