Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add functions for working with schema
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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)) |