项目作者: Pom4H

项目描述 :
Query Builder for PostgreSQL. With jsonb support. Written in TypeScript.
高级语言: TypeScript
项目地址: git://github.com/Pom4H/pg-query-config.git
创建时间: 2020-06-01T16:41:16Z
项目社区:https://github.com/Pom4H/pg-query-config

开源协议:MIT License

下载


Query Builder for PostgreSQL

With jsonb support. Written in TypeScript.

install

  1. npm install pg-query-config

example

  1. const { QueryConfig } = require('pg-query-config');
  2. const db = require('./myPgClient'); // pg or typeorm
  3. const query = new QueryConfig({ table: 'account' });
  4. query.where({ status: 'active', profile: { name: ['John', 'Peter'], email: 'example@mail.com' } });
  5. query.text // SELECT * FROM account WHERE status = $1 AND profile->>'name' IN ($2,$3) AND profile->>'email' = $4
  6. query.values // [ 'active', 'John', 'Peter', 'example@mail.com' ]
  7. db.query(query);

typescript example

  1. import { QueryConfig, LeftContain } from 'pg-query-config';
  2. type Color = 'black' | 'white' | 'blue' | 'red';
  3. type Brand = 'BMW' | 'Audi' | 'TOYOTA';
  4. type Engine = {
  5. cylinders: number;
  6. hp: number;
  7. };
  8. type Car = {
  9. brand: Brand;
  10. color: Color;
  11. engine: Engine;
  12. };
  13. type User = {
  14. id: number;
  15. name: string;
  16. car: Car;
  17. };
  18. const query = new QueryConfig<User>({ table: 'car_user' });
  19. query
  20. .select(['name', 'car'])
  21. .where({ id: 100 })
  22. .orWhere([
  23. { car: { engine: LeftContain({ hp: 500 }) } },
  24. { car: { engine: LeftContain({ cylinders: 8 }) } }
  25. ]);
  26. query.text // SELECT name,car FROM car_user WHERE id = $1 AND (car->>'engine' @> $2 OR car->>'engine' @> $3)
  27. query.values // [ 100, '{"hp":500}', '{"cylinders":8}' ]