@ckpack/pg-helper

A small helper of node-postgres can help you build queries more easily and safely

Usage no npm install needed!

<script type="module">
  import ckpackPgHelper from 'https://cdn.skypack.dev/@ckpack/pg-helper';
</script>

README

pg-helper

A lightweight node-postgres build query utils.

Since node-postgres uses ordinal parameter query ($1, $2, etc), the variables need to have a clear order. Once too many parameters are used, it will be extremely troublesome. pg-helper allows you to build SQL easier, faster and safer.

Example

const {PgHelper} = require('@ckpack/pg-helper');  
const pgHelper = new PgHelper({
    host,
    user,
    password,
    database,
    port: 5432,
    max: 20,
    idleTimeoutMillis: 30000,
    connectionTimeoutMillis: 2000,
  },{
    logger: console,
    returning: true,
});

// in node-postgres
// pg.query(`SELECT * FROM ${tablename} WHERE field1 = $1 AND field2 = $2`, [field1, field2]);

// in pg-helper 
pgHelper.runSql(`SELECT * FROM ${tablename} WHERE field1 = {field1} AND field2 = {field2}`, {field1, field2});
// Still supports the following way
pgHelper.runSql(`SELECT * FROM ${tablename} WHERE field1 = $1 AND field2 = $2`, [field1, field2])

Demos

Documentation