11
66%
MIT
A library for SQL composition in BuckleScript

Build Status Coverage Status

bs-sql-composer

A library for SQL composition in BucklesScript

Usage

Basic Select

  • RE
  • ML
  SqlComposer.Select.(
    make()
    |. field("*")
    |. from("test")
    |. toSql
    |. Js.log
  );
let _ =
  let open SqlComposer.Select in
    ((((make ()) |. (field "*")) |. (from "test")) |. toSql) |. Js.log
SELECT
  *
FROM test
WHERE 1=1

Where Clause

There is an implicit WHERE 1=1 added to every query. This is done to eliminate confusion with compound queries regarding the concatenation of where clauses.

  • RE
  • ML
  SqlComposer.Select.(
    make()
    |. field("*")
    |. from("test")
    |. where("AND test.foo = ?")
    |. toSql
    |. Js.log
  );
let _ =
  let open SqlComposer.Select in
    (((((make ()) |. (field "*")) |. (from "test")) |.
        (where "AND test.foo = ?"))
       |. toSql)
      |. Js.log
SELECT
  *
FROM test
WHERE 1=1
AND test.foo = ?

Join Clause

  SqlComposer.Select.(
    make()
    |. field("*")
    |. from("test")
    |. join("JOIN foo ON test.foo_id = foo.id")
    |. toSql
    |. Js.log
  )
SELECT
  *
FROM test
JOIN foo ON test.foo_id = foo.id
WHERE 1=1

Adding to a base query

  • RE
  • ML
  let base_query = SqlComposer.Select.(
    make()
    |. from("test")
    |. field("foo")
    |. field("bar")
  );

  SqlComposer.Select.(
    base_query
    |. where("AND test.foo = ?")
    |. toSql
    |. Js.log
  );
let base_query =
  let open SqlComposer.Select in
    (((make ()) |. (from "test")) |. (field "foo")) |. (field "bar")
let _ =
  let open SqlComposer.Select in
    ((base_query |. (where "AND test.foo = ?")) |. toSql) |. Js.log
SELECT
  foo
, bar
FROM test
WHERE 1=1
AND test.foo = ?

Alias a field

  • RE
  • ML
SqlComposer.Select.(
  make()
  |. from("test")
  |. field("foo AS bar")
  |. toSql
  |. Js.log
);
let _ =
  let open SqlComposer.Select in
    ((((make ()) |. (from "test")) |. (field "foo AS bar")) |. toSql) |.
      Js.log
SELECT
  foo AS bar
FROM test
WHERE 1=1

Order By

SqlComposer.Select.(
  make()
  |. from("test")
  |. field("*")
  |. orderBy(`Asc("foo"))
  |. orderBy(`Desc("bar"))
  |. toSql
  |. Js.log
);
SELECT
  *
FROM test
WHERE 1=1
ORDER BY
  foo ASC
, bar DESC

Group By

  • RE
  • ML
SqlComposer.Select.(
  make()
  |. from("test")
  |. field("foo AS bar")
  |. groupBy("foo")
  |. groupBy("thing")
);
let _ =
  let open SqlComposer.Select in
    ((((make ()) |. (from "test")) |. (field "foo AS bar")) |.
       (groupBy "foo"))
      |. (groupBy "thing")
SELECT
  foo AS bar
FROM test
WHERE 1=1
GROUP BY
  foo
, thing

Select Distinct

  • RE
  • ML
SqlComposer.Select.(
  make()
  |. field("foo")
  |. from("test")
  |. modifier(`Distinct)
);
let _ =
  let open SqlComposer.Select in
    (((make ()) |. (field "foo")) |. (from "test")) |. (modifier `Distinct)
SELECT DISTINCT
  foo
FROM test
WHERE 1=1

How do I install it?

Inside of a BuckleScript project:

yarn install --save bs-sql-composer

Then add bs-sql-composer to your bs-dependencies in bsconfig.json:

{
  "bs-dependencies": [ "bs-sql-composer" ]
}