Cuery - Composable SQL Querying
#
A composable SQL query builder based inspired by styled-components 💅 ✨
In 2016, I wrote a blog post about composing SQL queries and published this library as a reference. The years passed, and there are much cooler ways of doing it, so this is the new way - using template literals.
Installation#
yarn add cuery pg
# or
npm install --save cuery pg
Usage#
import { sql, execute } from "cuery";
const usersQuery = sql`SELECT * FROM users`;
const usersWithNameQuery = sql`
SELECT * FROM (${usersQuery})
WHERE name = ${params => params.name}
`;
execute(usersWithNameQuery, { name: "John" }); // => Promise(<all the users named "John">)
Transformations#
You can declare helper methods that do magic on your queries, like limit:
const limit = query =>
sql`
SELECT *
FROM (${query}) limited_query_${Math.floor(Math.random() * 100000)}
LIMIT ${p => p.limit}
OFFSET ${p => p.offset}
`;
// then you can just compose your queries!
const users = sql`SELECT * FROM users`;
const usersWithLimit = users.compose(limit); // or limit(users)
execute(usersWithLimit, { limit: 10, offset: 10 }); // start with offset of 10, then take 10 records.
Running tests#
- set the
DATABASE_URLenv variable for your PostgreSQL server - defaults tonode-postgres' default yarn test