Search code examples
datomic

Datomic: How do you add relationship properties, e.g. the time a person met another person?


I have some experience with Neo4j / cypher. In cypher relationships are first-class citizens, so they can have properties, which I really like.

I’m very new to datomic / datalog, sorry in advance if I’m asking a dumb question… How do I do something similar with datomic? Or do I simply approach the problem from a wrong perspective and should learn a new way of thinking about this?


Solution

  • Very briefly: in Datomic, a typical approach is to have one entity for each edge. You mind want to have a attribute named, say, :myapp.persons-pair/person-id-1+2, defined as a composite of the Entity IDs of both participants:

    [{:db/ident       :myapp.persons-pair/person-entid1+entid2
      :db/valueType   :db.type/tuple
      :db/tupleAttrs  [:myapp.persons-pair/person-1
                       :myapp.persons-pair/person-2]
      :db/cardinality :db.cardinality/one
      :db/unique      :db.unique/identity}
     {:db/ident       :myapp.persons-pair/person-1
      :db/valueType   :db.type/ref
      :db/cardinality :db.cardinality/one}
     {:db/ident       :myapp.persons-pair/person-2
      :db/valueType   :db.type/ref
      :db/cardinality :db.cardinality/one}
     {:db/ident       :myapp.persons-pair/met-at-instant
      :db/valueType   :db.type/instant
      :db/cardinality :db.cardinality/one}]