@soluzioni-futura/pg-component
v4.1.0
Published
## Quick start
Downloads
53
Keywords
Readme
pg-component
Quick start
Install the package
$ npm i @soluzioni-futura/pg-component
Usage
Setup
import PgComponent, { getInsertQuery, getUpdateQuery, debugTransformer } from "@soluzioni-futura/pg-component"
const pgComponent = new PgComponent({
user: process.env.PG_USER,
password: process.env.PG_PASSWORD,
host: process.env.PG_HOST,
port: process.env.PG_PORT,
database: process.env.PG_DATABASE
})
Execute a simple query
const qb = pgComponent.createQueryBuilder()
const { rows: [user] } = qb.execute(`
SELECT *
FROM "Users"
WHERE id = ${qb.addValue(userId)}
`)
Use sql query utility
const qb = pgComponent.createQueryBuilder()
const { rows: [{ id }] } = qb.execute(getInsertQuery({
qb,
table: "Users",
insertData: {
name: "Mario",
lastname: "Rossi",
age: 30
},
returningValues: ["id"]
}))
const qb = pgComponent.createQueryBuilder()
const { rowCount } = await qb.execute(getUpdateQuery({
qb,
table: "Users",
updateData: {
...data.updates,
lastEditTimestamp: new Date()
},
whereData: {
id: userId,
enabled: true
}
}))
Debug
You can print the sql executed with optionsObject
const options: QueryBuilderOptions = {
debugOutputPath: "logs/out.pgsql",
transformerFunction: debugTransformer
}
const qb = pgComponent.createQueryBuilder(options)