4.1.0 • Published 11 months ago

@soluzioni-futura/pg-component v4.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

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)
3.2.1

1 year ago

3.2.0

1 year ago

4.1.0

11 months ago

4.0.0

11 months ago

3.1.0

1 year ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.3

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago