Standalone SQL template

This guide assumes familiarity with:

With Waddler you can also use an sql without a database connections in cases where you want to just build a query or compose a part of a query to reuse it in different places. Let’s check both scenarios

Building a query string and params without a database conenction

import { sql } from "waddler/pg"

// You can then reuse this query in other sql statements
const sqlQuery = sql`select ${sql.identifier("id")} from ${sql.identifier("users")}`;

console.log(sqlQuery.toSQL());
select "id" from "users";

Building parts of a query in common functions and use it in a main sql query

// sql-builder.ts
import { SQL } from "waddler";
import { sql } from "waddler/pg";

export type Filters = {
    email: string,
    age: number,
}

export function buildFilters(filters: Filters): SQL {
    return sql`${sql.identifier('email')} = ${filters.email} 
                and ${sql.identifier('age')} = ${filters.age}`
}
// index.ts
import { waddler } from 'waddler/...'
import { buildFilters } from './sql-builder.ts'

const sql = waddler(process.env.DATABASE_URL);

await sql`select * from ${sql.identifier("users")} 
    where ${buildFilters({ email: '[email protected]', age 21 })}`
select * from "users" where "email" = $1 and "age" = $2
-- params: ['[email protected]', 21]