Skip to content

Instantly share code, notes, and snippets.

@alexanderkiel
Created March 15, 2013 11:37
Show Gist options
  • Save alexanderkiel/5169287 to your computer and use it in GitHub Desktop.
Save alexanderkiel/5169287 to your computer and use it in GitHub Desktop.
Minimal example which shows that traversing ref attributes from an entity result in just the keyword for entities with :db/ident and in the full entity for entities without a :db/ident.
(require '[datomic.api :as d])
(d/create-database "datomic:mem://test")
(def conn (d/connect "datomic:mem://test"))
;; Add the schema
(d/transact conn [{:db/id (d/tempid :db.part/db )
:db/ident :community/type
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "A community type enum value"
:db.install/_attribute :db.part/db}
{:db/id (d/tempid :db.part/db )
:db/ident :community/neighborhood
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "A community's neighborhood"
:db.install/_attribute :db.part/db}
{:db/id (d/tempid :db.part/db )
:db/ident :neighborhood/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity
:db/doc "A unique neighborhood name (upsertable)"
:db.install/_attribute :db.part/db}])
;; Add one enum constant
(d/transact conn [[:db/add (d/tempid :db.part/user )
:db/ident :community.type/twitter ]])
;; Add one community with type and neighborhood
(let [neighborhood-id (d/tempid :db.part/user )]
(d/transact conn [{:db/id neighborhood-id
:neighborhood/name "foo"}
{:db/id (d/tempid :db.part/user )
:community/type :community.type/twitter
:community/neighborhood neighborhood-id}]))
(def db (d/db conn))
(def community (->> (d/q '[:find ?e :where [?e :community/type]] db)
ffirst
(d/entity db)))
(:community/type community)
(:community/neighborhood community)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment