# fragmentum

> A SQL Query builder optimized for backend applications

Latest version **0.7.0** (published 2018-03-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install fragmentum
pnpm add fragmentum
yarn add fragmentum
bun add fragmentum
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.0 |
| Published | 2018-03-02 |
| First published | 2018-02-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 6.x |
| Dependencies | 0 |
| Unpacked size | 159.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Nathan Lincoln |
| Maintainers | nlincoln |

## Links

- npm: https://www.npmjs.com/package/fragmentum
- Repository: https://github.com/NLincoln/fragmentum
- Homepage: https://github.com/NLincoln/fragmentum#readme
- Issues: https://github.com/NLincoln/fragmentum/issues
- npm.io page: https://npm.io/package/fragmentum

## Recent versions

- 0.7.0 (latest) — 2018-03-02
- 0.6.0 — 2018-02-27
- 0.5.0 — 2018-02-18
- 0.4.0 — 2018-02-17
- 0.3.4 — 2018-02-16
- 0.3.3 — 2018-02-15
- 0.3.2 — 2018-02-15
- 0.3.1 — 2018-02-15
- 0.3.0 — 2018-02-15
- 0.2.0 — 2018-02-15
- 0.1.1 — 2018-02-13
- 0.1.0 — 2018-02-12
- 0.0.1 — 2018-02-12

## README

# Fragmentum [![Build Status](https://travis-ci.org/NLincoln/fragmentum.svg?branch=master)](https://travis-ci.org/NLincoln/fragmentum) [![Maintainability](https://api.codeclimate.com/v1/badges/97edcdd4747c26ca32ef/maintainability)](https://codeclimate.com/github/NLincoln/fragmentum/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.

```js
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
```sh
$ yarn add fragmentum
```

Then

```js
// 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:

```js
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?

```js
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.

---
_Source: https://npm.io/package/fragmentum · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
