Bisql (pronounced báisikl 🚲) is a SQL-first, SQL-only, SQL-obsessed data access toolkit for Clojure.
Write real SQL. Keep it executable. Generate the boring parts.
No query builder
No data mapper
No hidden SQL
No boilerplate SQL
note
This project is still early and the API may change. Support for databases beyond PostgreSQL and Malli integration are both planned.
Add it to deps.edn:
{:deps {io.github.hatappo/bisql {:mvn/version "0.2.0"}}}
If you want a shorter CLI entrypoint, you can also add an alias:
{:aliases
{:bisql {:main-opts ["-m" "bisql.cli"]}}}
Then commands such as these become available:
clojure -M:bisql gen-config
clojure -M:bisql gen-crud
clojure -M:bisql gen-declarations
If you prefer bb, you can also add tasks like:
{:tasks
{:gen-config (apply clojure "-M:bisql" "gen-config" *command-line-args*)
:gen-crud (apply clojure "-M:bisql" "gen-crud" *command-line-args*)
:gen-declarations (apply clojure "-M:bisql" "gen-declarations" *command-line-args*)}}
When passing CLI flags through bb, use -- as a separator. For example:
bb gen-declarations -- --include-sql-template --suppress-unused-public-var
Start with a simple table:
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
status TEXT NOT NULL
);
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
state TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX orders_state_created_at_idx
ON orders (state, created_at);
Place a SQL template under a classpath sql/... directory.
For example:
src/sql/postgresql/public/users/find-active.sqlsrc/sql.cljSELECT *
FROM users
WHERE status = /*$status*/'active'
ORDER BY id
LIMIT /*$limit*/100
This template is still valid SQL: you can run it directly with the embedded literals, and bisql replaces those literals with bind parameters at runtime.
(ns sql
(:require [bisql.core :as bisql]))
(bisql/defquery)
Then use the generated query function:
(ns app.user-service
(:require [next.jdbc :as jdbc]
[sql.postgresql.public.users.core :as users]))
(def datasource
(jdbc/get-datasource {:dbtype "postgresql"
:host "localhost"
:port 5432
:dbname "bisql_dev"
:user "bisql"
:password "bisql"}))
(users/find-active datasource {:status "active"
:limit 20})
bisql.core/defquery uses the current namespace only to find SQL files.
Each discovered SQL file defines executable query functions in the namespace derived from its file path, so the same SQL file always maps to the same namespace.
By default, query execution uses the :next-jdbc adapter.
Continuing from the previous example, you can generate a config template (bisql.edn) and modify it if needed:
clojure -M -m bisql.cli gen-config
Then generate the CRUD SQL:
clojure -M -m bisql.cli gen-crud
Depending on the tables present in the target database, this writes files such as:
src/sql/postgresql/public/users/crud.sqlsrc/sql/postgresql/public/orders/crud.sqlGenerated CRUD SQL includes templates such as insert, insert-many, get-by-*,
upsert-by-*, count, count-by-*, update-by-*, delete-by-*, list, and list-by-*.
These generated queries are meant to cover the typical index-friendly SQL patterns you would usually write by hand. In practice, that often means you do not need to write much custom SQL at all. When you do need a custom query, the generated SQL templates are also a convenient base to copy and adapt.
For the sample tables above, this typically includes:
users.crud/insertusers.crud/insert-manyusers.crud/upsert-by-idusers.crud/upsert-by-emailusers.crud/get-by-idusers.crud/get-by-emailusers.crud/countusers.crud/count-by-statususers.crud/update-by-idusers.crud/update-by-emailusers.crud/delete-by-idusers.crud/delete-by-emailorders.crud/insertorders.crud/insert-manyorders.crud/upsert-by-idorders.crud/countorders.crud/count-by-stateorders.crud/count-by-state-and-created-atorders.crud/listorders.crud/get-by-idorders.crud/update-by-idorders.crud/delete-by-idorders.crud/list-by-stateorders.crud/list-by-state-and-created-atThen execute one of the generated functions:
(ns app.order-service
(:require [next.jdbc :as jdbc]
[sql]
[sql.postgresql.public.orders.crud :as orders]))
(def datasource
(jdbc/get-datasource {:dbtype "postgresql"
:host "localhost"
:port 5432
:dbname "bisql_dev"
:user "bisql"
:password "bisql"}))
(orders/list datasource {:limit 20
:offset 0})
SQL templates are resolved from the classpath under the logical sql base path.
That means they can live under src/sql/, resources/sql/, or any other classpath root
that exposes sql/....
The same options can also be passed through environment variables such as
BISQL_HOST, BISQL_PORT, BISQL_DBNAME, BISQL_USER, BISQL_PASSWORD,
BISQL_SCHEMA, BISQL_BASE_DIR, BISQL_DBTYPE, and BISQL_CONFIG.
The precedence order is CLI options > environment variables > config file > defaults.
gen-declarations is an optional helper for projects that prefer explicit namespace
files. It generates navigation-oriented declare forms with docstrings derived
from the SQL templates, so IDEs and the REPL can jump to the intended namespace
and query source without letting a shallow (defquery) populate undeclared
namespaces. By default those docstrings include the project-relative SQL file path
and line number; pass --include-sql-template if you also want the SQL template
body included.
For more detail on the 2-way SQL syntax and rendering behavior, see:
For local setup, tasks, and dev workflow, see:
if conditions.bisql/DEFAULT to valid SQL value contexts if context-aware rendering becomes necessary.nil comparisons consistently in WHERE / HAVING clauses instead of letting expressions such as = NULL, LIKE NULL, or IN (NULL) silently behave unexpectedly. This likely needs stricter SQL context parsing, because = NULL is dangerous in WHERE / HAVING but can still be valid assignment syntax in SET.Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |