A library for SQL composition in BucklesScript
let _ = SqlComposer.Select.(
select
|> field "*"
|> from "test"
|> to_sql
|> Js.log
)
SELECT
*
FROM test
WHERE 1=1
let _ = SqlComposer.Select.(
select
|> field "*"
|> from "test"
|> where "AND test.foo = ?"
|> to_sql
|> Js.log
)
SELECT
*
FROM test
WHERE 1=1
AND test.foo = ?
let _ = SqlComposer.Select.(
select
|> field "*"
|> from "test"
|> join "JOIN foo ON test.foo_id = foo.id"
|> to_sql
|> Js.log
)
SELECT
*
FROM test
JOIN foo ON test.foo_id = foo.id
WHERE 1=1
let _ = SqlComposer.Select.(
let base_query =
from "test"
|> select "foo"
|> select "bar"
in
where base_query "AND test.foo = ?"
|> to_sql
|> Js.log
)
SELECT
foo
, bar
FROM test
WHERE 1=1
AND test.foo = ?
let _ = SqlComposer.Select.(
select
|> from "test"
|> field "foo AS bar"
|> to_sql
|> Js.log
)
SELECT
foo AS bar
FROM test
WHERE 1=1
let _ = SqlComposer.Select.(
select
|> from "test"
|> field "*"
|> order_by (`Asc "foo")
|> order_by (`Desc "bar")
|> to_sql
|> Js.log
)
SELECT
*
FROM test
WHERE 1=1
ORDER BY
foo ASC
, bar DESC
let _ = SqlComposer.Select.(
select
|> from "test"
|> field "foo AS bar"
|> group_by "foo"
|> group_by "thing"
)
SELECT
foo AS bar
FROM test
WHERE 1=1
GROUP BY
foo
, thing
let _ SqlComposer.Select.(
select
|> field "foo"
|> from "test"
|> modifier `Distinct
)
SELECT DISTINCT
foo
FROM test
WHERE 1=1
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" ]
}