Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clj-util-datomic-peer/src/main/clojure/de/mpg/shh/util_datomic_peer/schema.clj
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
38 lines (32 sloc)
1.79 KB
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
(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)) |