https://github.com/pom4h/pg-query-config
Query Builder for PostgreSQL. With jsonb support. Written in TypeScript.
https://github.com/pom4h/pg-query-config
jsonb postgresql typescript
Last synced: over 1 year ago
JSON representation
Query Builder for PostgreSQL. With jsonb support. Written in TypeScript.
- Host: GitHub
- URL: https://github.com/pom4h/pg-query-config
- Owner: Pom4H
- License: mit
- Created: 2020-06-01T16:41:16.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-09-27T14:02:35.000Z (over 4 years ago)
- Last Synced: 2025-03-01T23:02:55.556Z (over 1 year ago)
- Topics: jsonb, postgresql, typescript
- Language: TypeScript
- Homepage:
- Size: 24.4 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Query Builder for PostgreSQL
With jsonb support. Written in TypeScript.
#### install
```
npm install pg-query-config
```
#### example
```javascript
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
```javascript
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({ 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}' ]
```