Skip to content
Permalink
Browse files

add functions for working with schema

  • Loading branch information...
clayton committed Oct 4, 2017
1 parent f4a5eb0 commit e1108c472a1b32f3012e59d9933ab250dfe73d12
Showing with 39 additions and 1 deletion.
  1. +1 −1 project.clj
  2. +38 −0 src/main/clojure/de/mpg/shh/util_datomic_peer/schema.clj
@@ -1,4 +1,4 @@
(defproject de.mpg.shh/util-datomic-peer "0.0.2"
(defproject de.mpg.shh/util-datomic-peer "0.0.3"
:description "Utilities for using datomic databases"
:url "http://www.shh.mpg.de/"
:license {:name "Eclipse Public License"
@@ -0,0 +1,38 @@
(ns de.mpg.shh.util-datomic-peer.schema
(:require [datomic.api :as dt])
(:import [java.util.regex Pattern]))


(defn entity-attr->datascript-kv
[accumulator item]
(merge-with merge accumulator {(first item) {(second item) (last item)}}))


(defn entity-schema->value-type
[entity-schema]
(let [filtered-entity-schema (filter #(and (= (second %) :db/valueType)
(not (contains? #{:db.type/string :db.type/ref} (last %)))) entity-schema)]
(into {} (map (fn [[k v]] [k (:db/valueType v)]) (reduce entity-attr->datascript-kv {} filtered-entity-schema)))))

(defn entity-schema->datascript
[entity-schema]
(let [filtered-entity-schema (remove #(or (contains? #{:db/doc :db/ident} (second %))
(and (= (second %) :db/valueType)
(not= (last %) :db.type/ref))
(= (last %) :db.cardinality/one)) entity-schema)]
(reduce entity-attr->datascript-kv {} filtered-entity-schema)))

(defn domain-schema
[db dom-pat]
(let [_ (when-not (instance? Pattern dom-pat) (throw (ex-info "domain-schema requires an re-pattern" {:cause (str "You supplied '" (type dom-pat) "'")})))
domain-schema (dt/q '[:find ?attr ?iattr ?i
:in $ ?dom-pat
:where [(datomic.api/ident $ ?x) ?i]
[(datomic.api/ident $ ?xattr) ?iattr]
[?e ?xattr ?x]
[?e :db/ident ?attr]
[(datomic.Util/namespace ?attr) ?ns]
[(re-find ?dom-pat ?ns)]]
db
dom-pat)]
domain-schema))

0 comments on commit e1108c4

Please sign in to comment.
You can’t perform that action at this time.