/clara-eav

EAV triplets for Clara Rules

Primary LanguageClojureMIT LicenseMIT

Clojars Cljdoc CircleCI Codecov License

About

ClaraEAV is a thin layer over Clara-Rules API that simplifies working with EAV triplets, similar to Triplestores. It works with both Clojure and ClojureScript.

The main benefit of EAV triplets over Clara's default n-tuples (arbitrary records) is that updates (equivalent to a retraction and an insertion) are local to the attribute (EAV triplet). Updating an n-tuple requires retraction and re-insertion of the whole tuple. The downside is extra joins are needed to build back an entity from it's constituent EAVs.

Installation

ClaraEAV releases are on Clojars.

Usage

You should be familiar with Clara Rules before using ClaraEAV. See the documentation, including the Guide, on Cljdoc. For a more complete example see the rules test.

(ns sample
  (:require [clara.rules :as r]
            [clara-eav.rules :as er]))

(er/defquery todo-q [:?e] 
  [?todo <- er/entity :from [[?e]]])

(er/defsession session 
  'sample)

(def tx
  #:todo{:db/id :new
         :text "..."
         :done false})

(-> session
    (er/upsert tx)
    (r/fire-rules)
    (r/query todo-q :?e :new))

; ({:?e :new, :?todo #:todo{:db/id :new, :text "...", :done false}})

Credits