Table of contents
import { table, dsql as sql, SafeString, castSafe } from "../../src";
We will use this table
CREATE TABLE users(id int, age int, name string);
Which is defined in typescript as
const users = table(
/* columns: */ ["id", "age", "name"],
/* db-name & alias: */ "users"
);
One Clause
const name = "Lucas";
users
.selectStar()
.where((f) => sql`${f.name} = ${name}`)
.stringify();
SELECT
*
FROM
`users`
WHERE
`name` = 'Lucas'
Two Clauses
One call
const name2 = "Lucas";
users
.selectStar()
.where((f) => [sql`${f.name} = ${name2}`, sql`${f.id} = 5`])
.stringify();
SELECT
*
FROM
`users`
WHERE
`name` = 'Lucas'
AND `id` = 5
Two calls
const id = 5;
users
.selectStar()
.where((f) => sql`${f.name} = 'Lucas'`)
.where((f) => sql`${f.id} = ${id}`)
.stringify();
SELECT
*
FROM
`users`
WHERE
`name` = 'Lucas'
AND `id` = 5
OR
const OR = (...cases: SafeString[]): SafeString => {
const j = cases.map((it) => it.content).join(" OR ");
return castSafe(`(${j})`);
};
users
.selectStar()
.where((f) => OR(sql`${f.name} = 'Lucas'`, sql`${f.id} = ${id}`))
.stringify();
SELECT
*
FROM
`users`
WHERE
(
`name` = 'Lucas'
OR `id` = 5
)
This document used eval-md