Skip to content
ParticleDB
Release candidateParticleDB 1.1.19-rc.17 is not yet GA or ready for production data.Release status →

Clojure

Use the configured address for your client. See connection addresses and security. The examples use the SQL client address unless they explicitly select the HTTP API or ParticleDB JDBC driver.

Clojure applications connect to ParticleDB with next.jdbc and a JDBC driver: the PostgreSQL JDBC driver (SQL client interface, port 5432) or the ParticleDB JDBC driver (ParticleDB JDBC interface, port 7173). Use parameters and keep each transaction on a single connection.

;; deps.edn
{:deps {com.github.seancorfield/next.jdbc {:mvn/version "1.3.1093"}
org.postgresql/postgresql {:mvn/version "42.7.11"}}}

The examples connect as postgres with the password secret, for a server started with particledb start --auth-method scram-sha-256 --pg-password secret.

(require '[next.jdbc :as jdbc])
(def db (jdbc/get-datasource {:dbtype "postgresql" :host "127.0.0.1" :port 5432
:dbname "particledb" :user "postgres" :password "secret"}))
(jdbc/execute-one! db ["CREATE TABLE users (id INT PRIMARY KEY, name TEXT)"])
(jdbc/execute-one! db ["INSERT INTO users VALUES (?, ?)" 1 "alice"])
;; => #:next.jdbc{:update-count 1}
(jdbc/execute! db ["SELECT * FROM users WHERE id = ?" 1])
;; => [#:users{:id 1, :name "alice"}]
(jdbc/execute-one! db ["CREATE TABLE accounts (id INT PRIMARY KEY, balance INT)"])
(jdbc/execute-one! db ["INSERT INTO accounts VALUES (1, 500), (2, 0)"])
(jdbc/with-transaction [tx db]
(jdbc/execute-one! tx ["UPDATE accounts SET balance = balance - ? WHERE id = ?" 100 1])
(jdbc/execute-one! tx ["UPDATE accounts SET balance = balance + ? WHERE id = ?" 100 2]))
(jdbc/execute! db ["SELECT id, balance FROM accounts ORDER BY id"])
;; => [#:accounts{:id 1, :balance 400} #:accounts{:id 2, :balance 100}]

An exception thrown inside with-transaction rolls the whole transaction back.

Pass pgjdbc’s :sslmode (and :sslrootcert for verify-ca / verify-full) in the datasource map. With the server’s default --auth-method password, a SQL client login must use TLS; see TLS and authentication.

To use the ParticleDB JDBC driver, add it to deps.edn:

;; deps.edn
{:deps {com.github.seancorfield/next.jdbc {:mvn/version "1.3.1093"}
ai.particledb/particledb-jdbc {:mvn/version "1.0.12"}}}
(def ndb (jdbc/get-datasource {:jdbcUrl "jdbc:particledb://127.0.0.1:7173/particledb"
:user "postgres" :password "secret"}))
(jdbc/execute! ndb ["SELECT id, name FROM users"])
;; => [#:users{:id 1, :name "alice"}]

No JVM flags are needed. For TLS, see Java and JDBC.