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:
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})
Generate a config template:
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-*countcount-by-*update-by-*delete-by-*upsert-by-*See also:
Run clojure -M:bisql gen-crud to generate the routine CRUD SQL you would otherwise write by hand.
Add defquery to your application code once, so Bisql can turn SQL files, including generated ones, into ordinary Clojure functions.
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 |