Created
December 27, 2012 11:51
-
-
Save anonymous/4387690 to your computer and use it in GitHub Desktop.
Simple template expansion meta-definitions for 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
(defn preprocess-schema | |
([schema] (preprocess-schema schema (atom {}))) | |
([schema templates] | |
(let [entries (transient []) | |
extract-entry (fn [entry] | |
(let [template (:meta/template entry) | |
templates (:meta/templates entry) | |
entry (dissoc entry :meta/template :meta/templates)] | |
[entry (if template | |
(concat [template] templates) | |
templates)])) | |
process-entry (fn [entry] | |
(let [[entry entry-templates] (extract-template-definitions entry)] | |
(merge (apply merge (map @templates entry-templates)) entry)))] | |
(doseq [entry schema] | |
(let [entry (process-entry entry)] | |
(if-let [template-name (:meta/def-template entry)] | |
(swap! templates assoc template-name (dissoc entry :meta/def-template)) | |
(conj! entries entry)))) | |
(persistent! entries)))) |
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
[ | |
;; meta templates | |
{:meta/def-template :string-column | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db.install/_attribute :db.part/db} | |
{:meta/def-template :ref-column | |
:db/valueType :db.type/ref | |
:db/cardinality :db.cardinality/one | |
:db.install/_attribute :db.part/db} | |
{:meta/def-template :identity | |
:db/index true | |
:db/unique :db.unique/value} | |
{:meta/def-template :id-column | |
:meta/templates [:string-column :identity]} | |
;; Sessions | |
{:db/id #db/id[:db.part/db] | |
:db/ident :db.part/sysdea-sessions | |
:db.install/_partition :db.part/db} | |
{:meta/template :id-column | |
:db/id #db/id[:db.part/db] | |
:db/ident :session/id | |
:db/doc "A session's ID." | |
:db/noHistory true} | |
{:meta/template :ref-column | |
:db/id #db/id[:db.part/db] | |
:db/ident :session/user | |
:db/doc "Which user the session is associated with, if any." | |
:db/noHistory true} | |
{:meta/template :string-column | |
:db/id #db/id[:db.part/db] | |
:db/ident :session/metadata | |
:db/doc "Any additional information needed on the session." | |
:db/noHistory true} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Accidentally anonymous, oops, see https://gist.github.com/4387703