• Stars
    star
    408
  • Rank 105,946 (Top 3 %)
  • Language
    Clojure
  • License
    MIT License
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Functional relational programming for Clojure(Script).

relic

tests Clojars Project

status: alpha, major breaking changes unlikely, minor ones likely

relic is a Clojure(Script) data structure that provides the functional relational programming model described by the tar pit paper.

Why

The relational view (or model) of data ... appears to be superior in several respects to the graph or network model .... It provides a means of describing data with its natural structure only-that is, without superimposing any additional structure for machine representation purposes - Codd, A Relational Model of Data for Large Shared Data Banks

Support for relational data representation

Programming with maps becomes less ergonomic as soon as you have to deal with more than one collection of them at a time, so often they are over structured into trees that limit their use in different contexts.

Normalized relations are a more principled way to represent our collections of maps. There are some tools for working with relations in clojure.set, and they help.

But clojure.set has little to say about how groups of relations should be represented, and indexes are either created for each join, or not used at all.

Declarative data processing

Data processing pipelines could be defined as queries, a specification of the result rather than the steps - but you need mechanisms that can do this efficiently, particularly for partial updates.

Declarative relational constraints

It is often important to rule out invalid states, there are many tools for doing this to single maps or collections, but specifying constraints that exist in the space of relationships is often on the programmer to do manually.

It'd be nice to have a constraint language which was more like a query.

Reactive programming

In interactive applications, the system must respond to user input. You have the problem of invalidation - that is, how do you make sure dependent computations are re-run in response to the state of the program changing. re-frame does this with its subscriptions, but getting good performance if you have to invalidate say, an aggregate - is difficult and on you.

Reducing accidental complexity

relic is trying to reduce complexity, an experiment that allows you to program with normalized data, declarative query, constraints and data processing, and retain good-enough performance.

Features

  • Fully featured in-memory database with indexed SQL style query.
  • Integrated and embedded in clojure, use clojure functions in queries, build queries with clojure.
  • Materialized views with incremental maintenance.
  • Make invalid states illegal with constraints.
  • Reactive, allowing efficient integration with react, bind components to materialized queries and remain responsive at 60fps.

relic only targets in-memory use cases, for this kind of thing at scale consider: materialize.

Installation

With leiningen

[com.wotbrew/relic "0.1.5"]

With clojure (deps.edn)

com.wotbrew/relic {:mvn/version "0.1.5"}

Documentation

See documentation for a detailed reference.

Pitch

Do you suffer from map fatigue? [1]

despair

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?

in 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 it is different, really.

Brief tour

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 :Customer)
;; => 
({:name "bob"}, {:name "alice"})

(rel/q db [[:from :Customer] [:where [= :name "bob"]]]) 
;; => 
({:name "bob"})

(rel/q db [[:from :Customer] [:agg [] [:avg-name-len [rel/avg [count :name]]]]])
;; => 
({:avg-name-len 4})

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/mat db [[:from :Customer] [:where [= :name "bob"]]])
;; => returns the database, its value will be the same, but internally some machinery will have been allocated.
{:Customer #{{:name "bob"}, {:name "alice"}}}

mat 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.

Another example demonstrating usage in the browser can be found in cljidle.

For further reading, see the docs.

Related libraries

Thanks

LETS GO

lets go

#relic on clojurians

Email and raise issues. PR welcome, ideas and discussion encouraged.

License

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.