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.sql
SELECT *
FROM users
WHERE status = /*$status*/'active'
ORDER BY id
LIMIT /*$limit*/100
This template remains executable SQL with its embedded sample values. At runtime, Bisql renders it into SQL plus bind params.
(ns sql
(:require [bisql.core :as bisql]))
(bisql/defquery)
Then call the generated 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 CRUD SQL:
clojure -M -m bisql.cli gen-crud
This writes files such as:
src/sql/postgresql/public/users/crud.sqlsrc/sql/postgresql/public/orders/crud.sqlGenerated SQL typically includes:
insertinsert-manyget-by-*list-by-*list-by-*-starting-withcountcount-by-*count-by-*-starting-withupdate-by-*delete-by-*upsert-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/count-by-status-starting-withusers.crud/list-by-email-starting-withusers.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-order-number-starting-withorders.crud/count-by-state-and-created-atorders.crud/listorders.crud/get-by-idorders.crud/update-by-idorders.crud/delete-by-idorders.crud/list-by-order-number-starting-withorders.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.
See also:
The developer workflow with Bisql is straightforward:
Add defquery to your application code once, so Bisql can turn SQL files, including generated ones, into ordinary Clojure functions.
Run clojure -M:bisql gen-crud to generate the routine CRUD SQL you would otherwise write by hand, and add hand-written SQL where you need more complex queries.
If needed, run clojure -M:bisql gen-declarations to generate matching declare forms for those functions.
Call the generated query functions from your application code.
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 |