Last active
May 6, 2016 02:56
-
-
Save eslick/6af3320804075981eb31 to your computer and use it in GitHub Desktop.
One solution to integrating Schema and Datomic
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
(in-ns test | |
(:require | |
[schema.macros :as macros] | |
[schema.utils :as sutils])) | |
;; Wrapper type needed because Entity values do not implement | |
;; IPersistentMap interface | |
(defrecord EntitySchema | |
[schema] | |
Schema | |
(walker [this] | |
(let [map-checker (s/subschema-walker schema)] | |
(fn [e] | |
(or (when-not (sd/entity? e) | |
(macros/validation-error this e | |
(list 'instance? datomic.Entity (sutils/value-name e)))) | |
(map-checker (into {} (seq e))))))) | |
(explain [this] | |
(list 'entity 'datomic.Entity (explain (:schema this))))) | |
(defn entity-schema? | |
[schema] | |
(= (type schema) EntitySchema)) | |
(defn model-base-schema | |
"Generates a schema for Datomic entity with model tag 'type'" | |
[type] | |
{:pre [(keyword? type)]} | |
(EntitySchema. {:model/id s/Uuid | |
:model/type (s/both s/Keyword (s/eq type))})) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As
sd/entity?
is not defined you can use(instance? datomic.Entity e)
in its place.