in datomic, how is it possible to find out what keys are available for reverse lookup?

StackOverflow https://stackoverflow.com/questions/15629085

  •  29-03-2022
  •  | 
  •  

Question

The following uses a simplified datomic setup:

:account/user -> string
:account/email -> ref

:email/name -> string
:email/type -> keyword

if I have entity containing account information, it is easy to know that it has email information

(keys <account entity>) 
;; => [:account/user :account/email]

(:account/email <account entity>) 
;; => <email entity>

But on the flipside, if I look at the keys of the email entity, I do not know that it has linked account information.

(keys <email entity>) 
;; => [:email/name :email/type]

(:account/_email <email entity>) 
;; => <account entity>

how would one find out that :account/_email is a valid key without trial and error?

Was it helpful?

Solution

To check if a key is valid you can use:

(.containsKey <email entity> :account/_email)
;; => true

In order to get all valid entity keys including the reverse ones:

(.touch <email entity>)
(keys (.cache <email entity>))

Note that (keys) called directly on entity returns only forward keys.

Tested on similar schema.

Side note: apart from

(:account/_email <email entity>)

you can also query to get accounts that have linked specified email:

(q '[:find ?a :in $ ?e :where [?a :account/email ?e] ] (db conn) (:db/id <email entity>))

OTHER TIPS

Try the following query (not tested):

(q '[:find ?attrs
     :in $ ?e
     :where [_ ?attrs ?e]]
    my-db my-entity)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top