⚠️ Oops! This page doesn't appear to define a type called _.

Build Status Coverage Status

bs-sql-composer

A library for SQL composition in BucklesScript

Usage

Basic Select

  SqlComposer.Select.(
    make()
    |. field("*")
    |. from("test")
    |. toSql
    |. Js.log
  );
[@@@ocaml.ppx.context { cookies = [] }]
let _ =
  let open SqlComposer.Select in
    ((((make ()) |. (field (("*")[@reason.raw_literal "*"]))) |.
        (from (("test")[@reason.raw_literal "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.
reason SqlComposer.Select.( 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

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

  SqlComposer.Select.(
    base_query
    |. where("AND test.foo = ?")
    |. toSql
    |. Js.log
  );
[@@@ocaml.ppx.context { cookies = [] }]
let base_query =
  let open SqlComposer.Select in
    (((make ()) |. (from (("test")[@reason.raw_literal "test"]))) |.
       (field (("foo")[@reason.raw_literal "foo"])))
      |. (field (("bar")[@reason.raw_literal "bar"]))
let _ =
  let open SqlComposer.Select in
    ((base_query |.
        (where (("AND test.foo = ?")[@reason.raw_literal "AND test.foo = ?"])))
       |. toSql)
      |. Js.log
SELECT
  foo
, bar
FROM test
WHERE 1=1
AND test.foo = ?

Alias a field

SqlComposer.Select.(
  make()
  |. from("test")
  |. field("foo AS bar")
  |. toSql
  |. Js.log
);
[@@@ocaml.ppx.context { cookies = [] }]
let _ =
  let open SqlComposer.Select in
    ((((make ()) |. (from (("test")[@reason.raw_literal "test"]))) |.
        (field (("foo AS bar")[@reason.raw_literal "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
);
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

SqlComposer.Select.(
  make()
  |. from("test")
  |. field("foo AS bar")
  |. groupBy("foo")
  |. groupBy("thing")
);
[@@@ocaml.ppx.context { cookies = [] }]
let _ =
  let open SqlComposer.Select in
    ((((make ()) |. (from (("test")[@reason.raw_literal "test"]))) |.
        (field (("foo AS bar")[@reason.raw_literal "foo AS bar"])))
       |. (groupBy (("foo")[@reason.raw_literal "foo"])))
      |. (groupBy (("thing")[@reason.raw_literal "thing"]))
SELECT
  foo AS bar
FROM test
WHERE 1=1
GROUP BY
  foo
, thing

Select Distinct

SqlComposer.Select.(
  make()
  |. field("foo")
  |. from("test")
  |. modifier(`Distinct)
);
[@@@ocaml.ppx.context { cookies = [] }]
let _ =
  let open SqlComposer.Select in
    (((make ()) |. (field (("foo")[@reason.raw_literal "foo"]))) |.
       (from (("test")[@reason.raw_literal "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" ]
}