Clojure 针对多个标签添加的多个用户电子邮件

Clojure 针对多个标签添加的多个用户电子邮件,clojure,datomic,data-retrieval,Clojure,Datomic,Data Retrieval,在系统中,用户可以向现有条目添加标记,如下所示: (defn add-tag-to-post [eid email tags] (let [cast-eid (Long. eid)] (d/transact conn [{:db/id cast-eid, :author/email email, :post/tag tags}]))) 我认为,这应该放在一个交易中,该交易包

在系统中,用户可以向现有条目添加标记,如下所示:

(defn add-tag-to-post [eid email tags]
  (let [cast-eid (Long. eid)]
       (d/transact conn [{:db/id cast-eid,
                          :author/email email,
                          :post/tag tags}])))
我认为,这应该放在一个交易中,该交易包含电子邮件和添加了实体id的标签


我希望能够查询此数据的所有更改,并查看哪个用户的电子邮件添加了哪个标记。当前,当用户添加新标记时,所有电子邮件字段都反映了最近用户的电子邮件:

(defn get-all-post-history-by-eid [eid]
   (->> (d/q '[:find ?title ?content ?tags ?email ?eid
               :in $ ?eid
               :where
              [?eid post/title ?title]
              [?eid post/content ?content]
              [?eid author/email ?email]
              [?eid post/tag ?tags]] (d/db conn) eid)
         (map (fn [[title content tags email eid]]
              {:title title
               :content content
               :tags tags
               :email email
               :eid eid}))
         (sort-by :email)))
返回

({:title "Straight edges...", 
  :content "If ...", 
  :tags "art",                        <diff tag
  :email "randomlady@mailnator.hax",  <same email
  :eid 1759} 
 {:title "Straight edges ....", 
  :content "If ... ", 
  :tags "scissor-less edges",         <diff tag
  :email "randomlady@mailnator.hax",  < same email
  :eid 1759} 
 {:title "Straight edges ...", 
  :content "If ...", 
  :tags "paper",                      <diff tag
  :email "randomlady@mailnator.hax",  < same email
  :eid 1759} ... )

编辑#2:所以,我想我在这页文字的帮助下(认知上)取得了突破

基本上,Datomic尊重实体,这意味着如果您更改实体属性(当您记录属性的新值时,不会发生更改/删除),那么您实际上是在请求该属性的最新值

这意味着,对于添加的每个标记,创建一个新实体是合适的,并且在该实体中,它可以有一个对其调整的帖子的引用

所以从看起来像

EID  Attribute      Value     "Time/Transaction ID"
 37  :post/title     "Bees"           7
 37  :author/email   "v@a.x"          7
 37  :post/tag       "hive"           7
 37  :author/email   "diana@p"        8
 37  :post/tag       "mind"           8
如果使用新的:author/email更改属性:post/tag,您仍然在修改ID为37的实体,这将有效地“覆盖”这些值(标签是有序/多的,因此一个是加法的,但email将“覆盖”)

我想要的行为更像是

EID  Attribute      Value     "Time/Transaction ID"
 37  :post/title     "Bees"           7
 37  :author/email   "v@a.x"          7


 38  :post/tag       "honeycomb"      8
 38  :author/email   "squire@o"       8
 38  :tag/post (ref)   37             8
也就是说,ID为38的实体具有所有新值,并且成功地指向/引用实体37

写问题很有帮助!一旦我得到了实现,我将尝试更新这个问题。希望这能帮助一些人,以防他们需要它。

它很有效

(defn get-all-post-history-by-eid [pid]
  (->> (d/q '[:find ?title ?content ?tags ?email ?pid
              :in $ ?pid
              :where
              [?pid post/title ?title]
              [?pid post/content ?content]
              [?tid tag/post ?pid]         <new schema stuff
              [?tid tag/value ?tags]       <means new way to see the world
              [?tid author/email ?email]] (d/db conn) pid)
       (map (fn [[title content tags email pid]]
              {:title title
               :content content
               :tags tags
               :email email
               :eid pid}))
       (sort-by :email)))
tag/post在架构中的值类型为ref, tag/value具有基数/many,是一个字符串

这样,标签和帖子就解耦了。现在,当我查询一篇文章的所有历史记录时

vhax.dbmethods>  (get-all-post-history-by-eid 1759)
结果是

({:title "Straight edges without scissors", 
  :content "If you ever ... ", 
  :tags "skizzorz", 
  :email "so.ku@mail.hax", :bid 1759} 

 {:title "Straight edges without scissors", 
  :content "If you ever ...  ", 
  :tags "snips", 
  :email "vild@hax.pong", :bid 1759})
反映任何电子邮件添加的标签。成功今天我真的觉得Datomic很适合我

vhax.dbmethods>  (get-all-post-history-by-eid 1759)
({:title "Straight edges without scissors", 
  :content "If you ever ... ", 
  :tags "skizzorz", 
  :email "so.ku@mail.hax", :bid 1759} 

 {:title "Straight edges without scissors", 
  :content "If you ever ...  ", 
  :tags "snips", 
  :email "vild@hax.pong", :bid 1759})