Reactive SQLite: register queries, receive recomputed results.
(def eng (rsql/connect)) ; in-memory
(def eng (rsql/connect "file:app.db")) ; or a file
(rsql/exec! eng "create table person (id integer primary key, name text, age int)")
(def adults (rsql/q! eng "select id, name from person where age >= ?" 18))
@adults
(rsql/exec! eng "insert into person values (1, 'Ann', 33)")
@adults ; recomputed
Result atoms are plain Clojure atoms: deref reads, watch reacts, Reagent re-renders for free on both platforms. Writes go through the engine (exec!/transact!) - the posh contract; the preupdate hook feeds invalidation, recomputation runs off the writing thread, and only genuinely changed results are delivered.
The engine namespace documents the semantics (scheduling, DDL, rollback); this is the sugar layer over it, malli-specified. On the JVM the backend is the bundled org.sqlite.jni binding; on ClojureScript (later in the series) connect takes the consumer's initialized @sqlite.org/sqlite-wasm module.
Reactive SQLite: register queries, receive recomputed results.
(def eng (rsql/connect)) ; in-memory
(def eng (rsql/connect "file:app.db")) ; or a file
(rsql/exec! eng "create table person (id integer primary key, name text, age int)")
(def adults (rsql/q! eng "select id, name from person where age >= ?" 18))
@adults
(rsql/exec! eng "insert into person values (1, 'Ann', 33)")
@adults ; recomputed
Result atoms are plain Clojure atoms: deref reads, watch reacts,
Reagent re-renders for free on both platforms. Writes go through
the engine (exec!/transact!) - the posh contract; the preupdate
hook feeds invalidation, recomputation runs off the writing
thread, and only genuinely changed results are delivered.
The engine namespace documents the semantics (scheduling, DDL,
rollback); this is the sugar layer over it, malli-specified. On
the JVM the backend is the bundled org.sqlite.jni binding; on
ClojureScript (later in the series) connect takes the consumer's
initialized @sqlite.org/sqlite-wasm module.(close! eng)Close the connection; the engine is unusable afterwards.
Close the connection; the engine is unusable afterwards.
(exec! eng sql & params)Run one write statement; trailing arguments bind positionally. Recomputation of affected queries is scheduled off this thread - sync! forces it.
Run one write statement; trailing arguments bind positionally. Recomputation of affected queries is scheduled off this thread - sync! forces it.
(q! eng sql & params)Register a query and run it once; returns its result atom. Params are the trailing arguments, one per ? in the sql.
Register a query and run it once; returns its result atom. Params are the trailing arguments, one per ? in the sql.
(sync! eng)Flush pending recomputes synchronously - deterministic reads for tests.
Flush pending recomputes synchronously - deterministic reads for tests.
(transact! eng stmts)Run [sql & params] vectors (or bare sql strings) as one transaction; recompute scheduled once after COMMIT.
Run [sql & params] vectors (or bare sql strings) as one transaction; recompute scheduled once after COMMIT.
(unq! eng result)Retire a registered query's result atom.
Retire a registered query's result atom.
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 |