JSPM

pg-query-object

1.2.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q15220F
  • License MIT

Run a raw sql postgres with object in params

Package Exports

  • pg-query-object
  • pg-query-object/lib/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pg-query-object) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

pg-query-object

npm latest version npm bundle size build semantic-release Coverage Status

Run a raw query sql in postgres with object in params

How to use

type Params {
  name: string
  order: 'ASC' | 'DESC'
  limit: number
}

const params: Params = {
  name: 'max',
  order: 'ASC',
  limit: 10,
}

const sql = `
  select
      *,
      :limit AS itens_per_page
  from
      table
  where
      nome = :name
  order by name :order
  limit :limit
`

const [formattedRawQuery, arrayParams] = toSQL<Params>(sql, params)

/*
Output formattedRawQuery variable:

select
    *,
    $3 AS itens_per_page
from
    table
where
    nome = $1
order by name $2
limit $3

----------------------------------------
Output arrayParams variable:

['max', 'ASC', 10]
*/

Example with pg-query:

import { Client } from "pg";

const client = new Client();
await client.connect();
const res = await client.query(formattedRawQuery, arrayParams);
console.log(res.rows);
await client.end();

Example with typeorm

import { getManager } from "typeorm";

const manager = getManager();
const rawData = await manager.query(formattedRawQuery, arrayParams);

console.log(rawData);