将新数据附加到 Datomic 中的实体
Append new data to entity in Datomic
在尝试更新帖子的数据库标签数据时,我有一个看起来像
的函数
(defn add-tag-to-post [eid email tags]
(d/transact conn [{:db/id eid,
:author/email email,
:post/tag tags}]))
不幸的是,这不会保留标签(除非我要按时间查询)。我想简单地附加到标签列表而不是写一个新的。
示例:
{:title "Straight edges",
:content "fold instead of tearing it. ",
:tags "scissor-less edges", ;;check out these awesome tags
:author "me@website.hax"
:eid 1759}
(add-tag-to-post 1759 "me@website.hax" "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 "me@website.hax"
:eid 1759}
如何实现?
直接查询实体的生命周期是否更有意义?
您需要使 :post/tag
属性具有 :cardinality/many
- 请参阅 http://docs.datomic.com/schema.html 中的 :db/cardinality
。
默认情况下,属性具有 :cardinality/one
,当它们被覆盖时会自动收回旧值。 :cardinality/many
取消了该行为。
在尝试更新帖子的数据库标签数据时,我有一个看起来像
的函数(defn add-tag-to-post [eid email tags]
(d/transact conn [{:db/id eid,
:author/email email,
:post/tag tags}]))
不幸的是,这不会保留标签(除非我要按时间查询)。我想简单地附加到标签列表而不是写一个新的。
示例:
{:title "Straight edges",
:content "fold instead of tearing it. ",
:tags "scissor-less edges", ;;check out these awesome tags
:author "me@website.hax"
:eid 1759}
(add-tag-to-post 1759 "me@website.hax" "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 "me@website.hax"
:eid 1759}
如何实现?
直接查询实体的生命周期是否更有意义?
您需要使 :post/tag
属性具有 :cardinality/many
- 请参阅 http://docs.datomic.com/schema.html 中的 :db/cardinality
。
默认情况下,属性具有 :cardinality/one
,当它们被覆盖时会自动收回旧值。 :cardinality/many
取消了该行为。