Inline Query Language
This package aims to make SQL-like queries type safe and easy to build dynamically with an expressive API
import { Client } from 'pg';
import { query } from 'iql';
import type { QueryResult } from 'iql';
interface IRawUser {
id: string;
name: string;
}
interface IUserParams {
id: string;
ids: string[];
}
const findA = query<IRawUser, IUserParams>`
SELECT id, name FROM public.users
WHERE id = ${'id'}
-- WHERE id = $1
OR id = ${(agg) => agg.key('id')}
-- OR id = $1
OR id = ${(agg, { id }) => agg.value(id)} -- This creates a new parameter each time it is called
-- OR id = $2
OR id IN (${(agg, { ids }) => agg.values(ids)}); -- Creates parameters for each member of passed value, each time it is called.
OR id IN (${(agg) => agg.values('ids')}); -- Same as above
-- OR id IN ($3, $4, ..., $N);
`;
const pg = new Client();
const result = await pg.query<QueryResult<typeof findA>>(findA.compile({ id: '6', ids: ['a', 'b', '5'] }));
// row is of type IRawUser
result.rows.forEach((row) => {});
Component | Query executors | Notes |
---|---|---|
pg |
pg |
Used as input to the {Pool,Client}#query method. Also exported as query for backwards compatibility. |
bq |
@google-cloud/bigquery |
Used as input to the BigQuery#createQueryJob method. |