node-pg-util
Utility Belt for PostgreSQL
npm install pg-util
The goal of this library is to make writing commons database tasks more succinct:
- getting a client
#getClient()
- query the database
#query(<client>, text, vals)
- getting the first row only
#one(<client>, text, vals)
- executing a sql file (by name)
#run(<client>, name, vals)
- executing a sql file (by name) and return the first row
#first(<client>, name, vals)
All the methods returns promises and if you use the new async/await
features in ES7 you can write some nifty code. Check out some of the examples:
Transactions
{ const createTableSQL = `CREATE TABLE boo ( name TEXT NOT NULL, email TEXT NOT NULL PRIMARY KEY );` const insertSQL = `INSERT INTO boo(name, email) VALUES ('John Doe', 'test@test.com');` const selectSQL = 'SELECT * FROM boo;' await thisdb}
Execute a paramterized query using a SQL file and get the first row
-- ./resources/tests/sql/select-param.sql file SELECT $1::text AS "name";
{ const pathToSQLFiles = path const db = const row = await db should}