@soluzioni-futura/pg-component
TypeScript icon, indicating that this package has built-in type declarations

4.1.0 • Public • Published

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)

Readme

Keywords

Package Sidebar

Install

npm i @soluzioni-futura/pg-component

Weekly Downloads

7

Version

4.1.0

License

ISC

Unpacked Size

22.1 kB

Total Files

7

Last publish

Collaborators

  • giovanni.bruno
  • nico.fuccella
  • vforvalerio87
  • zhoujian26
  • riccardorispoli
  • allemonta