0.7.0 • Published 6 years ago

fragmentum v0.7.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Fragmentum Build Status Maintainability

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.

0.7.0

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.4

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago