Complex queries can be written with normal SQL, including the values needs to be bound and prefixed with the `sql` tag.
npm install sql-pg

Latest Release: v10.4.1, Latest Major Release: v10.0.0, All Releases
![]() |
Make the id variable? | ![]() |
| Or even more simple? | ![]() |
pg as database driver
bash
npm install --save pg sql-pg
`
Initialisation
Use it in your project:
`javascript
const sql = require('sql-pg')()
`
The connection use per default the env var DATABASE_URL.
Usage
$3
Simple data manipulation can be done without writing any SQL Statements.
E.g. some user data manipulation:
`javascript
const id = await sql.insert(
'users',
{ name: 'Sharaal', email: 'sql-pg@sharaal.de', passwordhash: '...' }
)
await sql.update('users', { validated: 1 }, { id })
await sql.delete('users', { id })
`
More complex data manipulation can be done with the SQL Tag.
$3
Often needed convenient methods to check and extract query results are available with the Selection Methods.
E.g. select all not validated users:
`javascript
const users = await sql.any('users', ['name', 'email'], { validated: 0 })
`
Also the Selection Methods supports SQL Tag as parameter for more complex selections. Because they are highly inspired by pg-promise, there are the Selection Methods any/manyOrNone, many, oneOrNone and one available.
$3
If it becomes more complex the SQL Tag and Tag Helpers are the way to go.
They are as near as possible to native SQL queries to be readable and easy to write. All variables can be directly used and will be exchanged via placeholders and given to the database separately as values. For non native values like lists, for table/column names and conditions there are Tag Helpers.
E.g. list of not activated users filtered by name:
`javascript
const name = 'raa'
const users = await sql.any(
sql
%${name}%}
)
`
There are a lot more Tag Helpers available like .identifier, .table, .column(s), .value(s), .valuesList, .assignments, .conditions, .limit, .offset, .pagination, .if, .jsonColumnObject and .jsonColumnText`.