Table of contents
  1. Getting started
    1. String interpolation
    2. String interpolation helpers
    3. Composition

Getting started

import { table, dsql as sql, SafeString } from "../../src";

Construct a table instance

const users = table(["id", "age", "name"], "users");

Create a query

const q = users.selectStar();

Get the SQL

const str = q.stringify();
str;
SELECT
  *
FROM
  `users`

String interpolation

const q2 = users.select(["age"]).where((fields) => sql`${fields.id}=1`);
q2.stringify();
SELECT
  `age` AS `age`
FROM
  `users`
WHERE
  `id` = 1

String interpolation helpers

const eq = (
    a: SafeString | string | number,
    b: SafeString | string | number
): SafeString => sql`${a}=${b}`;

const MAX = (it: SafeString): SafeString => sql`MAX(${it})`;

const q3 = users
    .select((fields) => ({ age: MAX(fields.age) }))
    .where((fields) => eq(fields.id, 1));

q3.stringify();
SELECT
  MAX(`age`) AS `age`
FROM
  `users`
WHERE
  `id` = 1

Composition

q.as("q").commaJoin(q3.as("q3")).selectStar().stringify();
SELECT
  *
FROM
  (
    SELECT
      *
    FROM
      `users`
  ) AS `q`,
  (
    SELECT
      MAX(`age`) AS `age`
    FROM
      `users`
    WHERE
      `id` = 1
  ) AS `q3`

This document used eval-md