fragmentum v0.7.0
Fragmentum
A Query Builder for dynamic PostgreSQL queries (MySQL support planned too)
Fragmentum allows you to create SQL queries dynamically based off of an array of conditions with ease.
import { builder, select, from, where, bind, eq } from "fragmentum";
builder(
select("username", "user_id"),
from("users"),
where(eq("username", bind("username", 2)))
).serialize();
// >> SELECT "username", "user_id" FROM "users" WHERE "username" = '2';
Install
$ yarn add fragmentum
Then
// es6
import { builder } from 'fragmentum';
// commonjs
const { builder } = require('fragmentum');
About
In Fragmentum, a query is composed of fragments, which are discrete parts of a query fragmentum understands how to compose together dynamically.
For example, imagine you're writing a REST api with optional query-string values:
app.get("/users", async (req, res) => {
const sql = builder(
select("username", "user_id"),
from("users"),
req.query.username &&
where(eq("username", bind("username", req.query.username)))
).serialize();
res.send({
users: await someDatabaseLibrary.query(sql)
});
});
Even better: what if we wanted to do something more complex, like optionally joining in a table?
const sql = builder(
shouldJoinUsers && join('users', 'users.group_id', 'group.id'),
).select().from('group')
Fragmentum makes it easy to declaratively compose together different pieces of your SQL queries.
Please note that fragmentum doesn't handle connecting to the database or execution of queries. It just generates SQL, and it's up to you how to execute it.