Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add functions for working with schema
  • Loading branch information
clayton committed Oct 4, 2017
1 parent f4a5eb0 commit e1108c4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion project.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" :description "Utilities for using datomic databases"
:url "http://www.shh.mpg.de/" :url "http://www.shh.mpg.de/"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"
Expand Down
38 changes: 38 additions & 0 deletions src/main/clojure/de/mpg/shh/util_datomic_peer/schema.clj
@@ -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.