0.3.1 • Published 3 years ago

iql v0.3.1

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

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

ComponentQuery executorsNotes
pgpgUsed as input to the {Pool,Client}#query method. Also exported as query for backwards compatibility.
bq@google-cloud/bigqueryUsed as input to the BigQuery#createQueryJob method.