1.0.17 • Published 3 years ago

pg-query-config v1.0.17

Weekly downloads
18
License
MIT
Repository
github
Last release
3 years ago

Query Builder for PostgreSQL

With jsonb support. Written in TypeScript.

install

npm install pg-query-config

example

const { QueryConfig } = require('pg-query-config');
const db = require('./myPgClient'); // pg or typeorm

const query = new QueryConfig({ table: 'account' });

query.where({ status: 'active', profile: { name: ['John', 'Peter'], email: 'example@mail.com' } });

query.text // SELECT * FROM account WHERE status = $1 AND profile->>'name' IN ($2,$3) AND profile->>'email' = $4
query.values // [ 'active', 'John', 'Peter', 'example@mail.com' ]

db.query(query);

typescript example

import { QueryConfig, LeftContain } from 'pg-query-config';

type Color = 'black' | 'white' | 'blue' | 'red';
type Brand = 'BMW' | 'Audi' | 'TOYOTA';
type Engine = {
    cylinders: number;
    hp: number;
};
type Car = {
    brand: Brand;
    color: Color;
    engine: Engine;
};
type User = {
    id: number;
    name: string;
    car: Car;
};

const query = new QueryConfig<User>({ table: 'car_user' });

query
    .select(['name', 'car'])
    .where({ id: 100 })
    .orWhere([
        { car: { engine: LeftContain({ hp: 500 }) } }, 
        { car: { engine: LeftContain({ cylinders: 8 }) } }
    ]);

query.text // SELECT name,car FROM car_user WHERE id = $1 AND (car->>'engine' @> $2 OR car->>'engine' @> $3)
query.values // [ 100, '{"hp":500}', '{"cylinders":8}' ]
1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.9

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago