STATUS: Early alpha, will eat your homework, set laptop on fire, use at own risk
[com.wotbrew/relic "0.1.1"]
relic
is a Clojure/Script in-memory database and data processing library, inspired by Codd's relational algebra.
It has materialized views (with incremental maintenance), change tracking, constraints and SQL-like query DSL.
It is an attempt to deliver in memory the programming model described by the tar pit paper.
(rel/q db [[:from :Library]
[:where [contains? :lib/tags "relational"] [str/starts-with? :lib/name "rel"]]
[:join :Author {:lib/author :author/id}]
[:select
:lib/name
:author/github
[:url [str "https://github.com/" :author/github "/" :lib/name]]]])
;; =>
({:lib/name "relic", :author/github "wotbrew", :url "https://github.com/wotbrew/relic"})
See documentation for a detailed reference.
Do you suffer from map fatigue? [1]
Did you try meander, core.logic, datascript and every graph-map-database under the sun but still do not feel out of the tar pit?
Are you tired of writing mechanical wiring and glue? That has nothing to do with your actual business logic?
relic
might help, but it's not a medical professional. It's a functional relational programming library.
Definitely not at all like the other in-memory databases in clojure. this time its different, really.
I like to use rel
as an alias.
(require '[com.wotbrew.relic :as rel])
This is a query, lets find some bob's.
[[:from :Customer]
[:where [= :name "bob"]]]
You can refine queries by just adding elements to the vector. All your favorites are here, filtering (:where
), computing columns (:extend
), joins (:join
& :left-join
), grouping and aggregation (:agg
) and more.
[[:from :Customer]
[:where [= :name "bob"]]
[:extend [:greeting [str "Hello, " :name "!"]]]
Because queries are just vectors, they just sort of lounge around being values. To put them to work we have to feed some data into relic.
relic
databases are boring clojure maps. Prepare yourself:
(def db {})
See, boring.
You manipulate your database with the transact
function. This returns a new database with the transaction applied. Plain old functional programming, no surprises.
(def db (rel/transact {} [:insert :Customer {:name "bob"} {:name "alice"}])
db
;; =>
{:Customer #{{:name "bob"}, {:name "alice"}}}
Now we have some state, we can ask questions of relic, as you would a SQL database.
(rel/q db [[:from :Customer] [:where [= :name "bob"]]])
;; =>
#{{:name "bob"}}
(rel/q db [[:from :Customer] [:where [= :name "alice"]]])
;; =>
#{{:name "alice"}}
Ok ok, neat but not cool.
Ahhhh... but you don't understand, relic
doesn't just evaluate queries like some kind of cave man technology - it is powered by a data flow graph.
relic
knows when your data changes, and it knows how to modify dependent relations in a smart way.
You can materialize any query such that it will be maintained for you as you modify the database. In other words relic
has incremental materialized views.
(rel/materialize db [[:from :Customer] [:where [= :name "bob"]]])
;; => returns the database, its value will be the same (hint: metadata).
{:Customer #{{:name "bob"}, {:name "alice"}}}
materialize
will return a new database, against which materialized queries will be instant, and as you change data in tables, those changes will flow to materialized queries automatically.
You can do more than query and materialize with relic, you can react to changes, use constraints and more.
If you read the tarpit paper, you might find this real estate example informative.
For further reading, see the docs
Any many, many more, sorry if I forgot you, give me a PR.
#relic
on clojurians
Email and raise issues. PR welcome, ideas and discussion encouraged.
Copyright 2022 Dan Stone (wotbrew)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Can you improve this documentation? These fine people already did:
Dan Stone & wotbrewEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close