Skip to content
/ iql Public

Open source inline query language

License

Notifications You must be signed in to change notification settings

AltNext/iql

Repository files navigation

IQL

Inline Query Language

This package aims to make SQL-like queries type safe and easy to build dynamically with an expressive API

Test Status Coverage Status

Bugs Code Smells Maintainability Rating Reliability Rating Security Rating Technical Debt Vulnerabilities

Snyk Vulnerabilities for GitHub Repo

npm NPM npm npm bundle size

GitHub issues GitHub pull requests GitHub code size in bytes Lines of code GitHub top language

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) => {});

Supported query executors

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.