Skip to content

carmonw/bs-sql-composer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

bs-sql-composer

A library for SQL composition in BucklesScript

Usage

Basic Select

  let _ = SqlComposer.Select.(
    select
    |> field "*"
    |> from "test"
    |> to_sql
    |> Js.log
  )
SELECT
  *
FROM test
WHERE 1=1

Where Clause

  let _ = SqlComposer.Select.(
    select
    |> field "*"
    |> from "test"
    |> where "AND test.foo = ?"
    |> to_sql
    |> Js.log
  )
SELECT
  *
FROM test
WHERE 1=1
AND test.foo = ?

Join Clause

  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

Adding to a base query

  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 = ?

Alias a field

let _ = SqlComposer.Select.(
  select
  |> from "test"
  |> field "foo AS bar"
  |> to_sql
  |> Js.log
)
SELECT
  foo AS bar
FROM test
WHERE 1=1

Order By

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

Group By

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

Select Distinct

let _ SqlComposer.Select.(
  select
  |> 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" ]
}

About

A library for SQL composition in BucklesScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • OCaml 100.0%