sova
sova

Reputation: 5650

Append new data to entity in Datomic

In trying to update database tag data on posts I have a function that looks like

(defn add-tag-to-post [eid email tags]
  (d/transact conn [{:db/id eid,
                     :author/email email,
                     :post/tag tags}]))

Unfortunately, this does not preserve tags (unless I were to query by time). I would like to simply append to the tags list instead of write a new one.

Example:

{:title "Straight edges", 
 :content "fold instead of tearing it.  ", 
 :tags "scissor-less edges", ;;check out these awesome tags
 :author "[email protected]"
 :eid 1759}  


(add-tag-to-post 1759 "[email protected]" "art")
;;desired behavior: adds tag "art" to the list of tags

(get-post-by-eid 1759)
;;returns 

{:title "Straight edges", 
 :content "fold instead of tearing it.  ", 
 :tags "art", ;;not cumulative tag addition ;/
 :author "[email protected]"
 :eid 1759}  

How can this be achieved?

Does it make more sense to simply query over the lifetime of the entity instead?

Upvotes: 0

Views: 279

Answers (1)

CmdrDats
CmdrDats

Reputation: 89

You'll need to make your :post/tag attribute have :cardinality/many - see :db/cardinality in http://docs.datomic.com/schema.html.

By default, attributes have :cardinality/one which automatically retracts old values when they are overwritten. :cardinality/many cancels out that behaviour.

Upvotes: 1

Related Questions