Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2012 11:51
Show Gist options
  • Save anonymous/4387690 to your computer and use it in GitHub Desktop.
Save anonymous/4387690 to your computer and use it in GitHub Desktop.
Simple template expansion meta-definitions for Datomic
(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))))
[
;; 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}
]
@c-spencer
Copy link

Accidentally anonymous, oops, see https://gist.github.com/4387703

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment