Compile
April 1, 2025 · View on GitHub
= The SQL Graph in Clojure
Utilizes Tinkerpop3 graph over SQL database using sqlg.
image:https://img.shields.io/github/license/fern-flower-lab/sqlg-clj?style=for-the-badge[GitHub] image:https://img.shields.io/clojars/v/ai.z7/sqlg-clj.svg?style=for-the-badge[] image:https://img.shields.io/github/v/tag/fern-flower-lab/sqlg-clj?style=for-the-badge[GitHub tag (latest by date)] image:https://img.shields.io/github/last-commit/fern-flower-lab/sqlg-clj?style=for-the-badge[GitHub last commit] image:https://img.shields.io/github/v/release/fern-flower-lab/sqlg-clj?style=for-the-badge[GitHub release (latest by date)]
WARNING: Origin's library uses J17 for releases (at least for 3.0.x versions), so independently on this Clojure library build you will be forced to use that version too.
== Installation
Add this dependency to your project.clj:
[source,clojure]
[ai.z7/sqlg-clj "0.1.0"]
This library supports multiple versions of the SQLG library. To specify which version you want to use, activate the appropriate profile:
For SQLG 3.1.1 (latest): [source,shell]
lein with-profile +lib-3.1.1
For SQLG 3.0.4: [source,shell]
lein with-profile +lib-3.0.4
For SQLG 3.0.1: [source,shell]
lein with-profile +lib-3.0.1
For SQLG 2.1.6 (legacy): [source,shell]
lein with-profile +lib-2.1.6
For development with logging enabled: [source,shell]
lein with-profile +dev,+logging,+lib-3.1.1
== Supported DBs
. PostgreSQL . MySQL . MariaDB . HSQLDB . H2 . MSSQL
CAUTION: The low-level driver is well tested only with PostgreSQL + C3P0. You are warned.
== Config
Basic config is represented by sample.properties
[source,properties]
sample.graph.db.type = postgresql sample.graph.db.host = localhost sample.graph.db.port = 5432 sample.graph.db.name = sample sample.graph.db.user = username sample.graph.db.pass = password
In order to get it prepared use
[source,clojure]
user=> (require '[sqlg-clj.config :as c]) user=> (def config (-> "sample" c/load-config :sample :graph :db c/db-config))
Same result may be done using EDN config, i.e. directly from your app:
[source,clojure]
user=> (def config (c/db-config {:port 5432 :pass "password" :user "username" :type "postgresql" :host "localhost" :name "sample"}))
When the config is ready it may be easily read back
[source,clojure]
user=>(c/config->clj config) {"jdbc.url" "jdbc:postgresql://localhost:5432/sample", "jdbc.username" "username", "jdbc.password" "password"}
=== Starting
Open SqlgGraph directly
[source,clojure]
(def G (c/graph config))
or indirectly
[source,clojure]
(def G (c/open-graph config))
The last method opens a new TinkerGraph with default configuration or open a new Graph instance with the specified configuration. The configuration may be a path to a file or a Map of configuration options. When gets prepared BaseConfiguration or Configuration object as an argument - returns SqlgGraph.
=== Using
[source,clojure]
user=> (require '[sqlg-clj.core :refer :all]) user=> (-> ^Graph G traversal V (has-label "label")) #object[org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal 0x3fffba3f "[GraphStep(vertex,[]), HasStep([~label.eq(label)])]"]
=== Transactions
In order to store or to unroll the changes were made by some iteration between calls, use
[source,clojure]
user=> (require '[sqlg-clj.util :as u]) user=> (u/commit! G) ;; or user=> (u/rollback! G)
respectively.
=== Examples
==== create, find, process
[source,clojure]
user=> (require '[sqlg-clj.util :as u]) user=> (require '[sqlg-clj.data :as d]) user=> (require '[sqlg-clj.core :refer :all])
user=> (-> G (d/add-V :test {:aa 11 :bb 22})) #object[org.umlg.sqlg.structure.SqlgVertex 0x2e5e87e9 "v[public.test:::14]"] user=> (-> G (d/add-V :test {:aa 33 :cc 44})) #object[org.umlg.sqlg.structure.SqlgVertex 0x6ec0ead1 "v[public.test:::15]"] user=> (u/commit! G) nil
user=> (def vxs (-> G traversal V (has-label :test) (has :aa) u/into-vec!)) #'user/vxs
user=> (map #(value % :aa) vxs) (11 33) user=> (map #(value % :bb) vxs) (22 nil) user=> (map #(value % :cc) vxs) (nil 44)
==== dealing with java native structures
[source, clojure]
(select->clj ^Graph G traversal V (has-label "my-label") (has "id" #uuid "1622c1fe-41d7-4536-8138-ccfba8385678")) ;; this returns lazy sequence of keywordized hashmaps
=== More info
Please, read original documentation https://tinkerpop.apache.org/docs/current/reference[here] and http://sqlg.org[here]
== Development
=== Checking for Dependency Updates
This project uses the lein-ancient plugin to check for outdated dependencies. You can run:
[source,shell]
lein ancient check
To automatically upgrade all outdated dependencies:
[source,shell]
lein ancient upgrade :interactive
=== Project Structure
The project follows standard Clojure conventions:
src-clj/- Source coderesources/- Configuration filesproject.clj- Core project definition (kept clean for publishing)profiles.clj- Development profiles and version overrides
=== Building
[source,shell]
Compile
lein compile
Start a REPL
lein repl
Create a JAR
lein jar
Deploy to repositories
lein deploy
=== Testing
The library includes tests that demonstrate core functionality with examples from this README. Tests are designed to work with a PostgreSQL database.
Setup test database: [source,shell]
./test/setup_test_db.sh
Run tests with a specific SQLG version: [source,shell]
Latest version
lein with-profile +dev,+logging,+lib-3.1.1 test
Legacy version
lein with-profile +dev,+logging,+lib-2.1.6 test
Run tests with all supported SQLG versions: [source,shell]
./test/run_all_version_tests.sh
Test environment variables: [source,shell]
Configure test database connection
export SQLG_TEST_HOST=localhost export SQLG_TEST_PORT=5432 export SQLG_TEST_DB=sqlgtest export SQLG_TEST_USER=postgres export SQLG_TEST_PASS=postgres
== License
©2022-2025 Fern Flower Lab
Distributed under the MIT Licence.