给定以下文档
{:xt/id 1
:line-item/quantity 23
:line-item/item 20
:line-item/description "Item line description"}我想将数量更新为25
到目前为止,我需要首先查询数据库,获取完整的文档,合并更改,然后再处理新的文档。
有没有一种方法可以在不进行上述操作的情况下仅在数量上进行合并?
谢谢
发布于 2021-09-24 07:15:49
为此,您应该能够使用transaction functions。这些将允许您指定多个步骤,并将它们下推到事务日志中,以确保它们按顺序执行(即,在将事务函数调用本身推入到事务日志中的时间点,您将始终检索要更新的最新文档)。
对于您的特定示例,我认为它看起来像这样(未经测试):
(xt/submit-tx node [[::xt/put
{:xt/id :update-quantity
;; note that the function body is quoted.
;; and function calls are fully qualified
:xt/fn '(fn [ctx eid new-quantity]
(let [db (xtdb.api/db ctx)
entity (xtdb.api/entity db eid)]
[[::xt/put (assoc entity :line-item/quantity new-quantity)]]))}]])这将创建事务函数本身,然后您只需调用它来进行更改:
;; `[[::xt/fn <id-of-fn> <id-of-entity> <new-quantity>]]` -- the `ctx` is automatically-injected
(xt/submit-tx node [[::xt/fn :update-quantity 1 25]])https://stackoverflow.com/questions/69291427
复制相似问题