https://github.com/ckpack/pg-helper
A lightweight node-postgres build query utils
https://github.com/ckpack/pg-helper
node-postgres pg pg-helper postgres postgresql sql
Last synced: 11 months ago
JSON representation
A lightweight node-postgres build query utils
- Host: GitHub
- URL: https://github.com/ckpack/pg-helper
- Owner: ckpack
- License: mit
- Created: 2021-02-25T05:14:24.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-04-07T03:49:16.000Z (almost 5 years ago)
- Last Synced: 2024-10-14T13:03:34.627Z (over 1 year ago)
- Topics: node-postgres, pg, pg-helper, postgres, postgresql, sql
- Language: TypeScript
- Homepage:
- Size: 271 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pg-helper
A lightweight [node-postgres](https://node-postgres.com/) build query utils.
Since [node-postgres](https://node-postgres.com/) 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
```js
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](__test__/)
# [Documentation](https://ckpack.github.io/pg-helper/)