背景
我们有多个通过事件进行通信的服务。许多事件使用事件中存在的(全局)唯一代理ID来引用某个实体。例如,“ CustomerRegisteredEvent”(E)可能包含注册客户的ID。当使用其他数据库时,我通常可以持久保留一个“客户”实体,其ID对应于(E)中的ID(和其他值)。
在datomic中,我通常会看到使用tempid
来为新实体生成ID,但是我不清楚在预先知道UUID时是否应该使用这种方法吗?
问题
如果不是,通常是否只是为“原始”(事件)ID创建一个新属性?就像是:
{:db/id #db/id[:db.part/db] :db/ident :customer/uuid :db/valueType :db.type/string :db/cardinality :db.cardinality/one :db/doc "The original UUID of the customer" :db.install/_attribute :db.part/db}
只需忽略Datomic:db/id
作为内部细节(就像您将提交的Git哈希忽略为内部细节一样)。使用问题中的解决方案(2),但您可能要使用内置类型:db.type/uuid
而不是字符串。
您可能还会对Tupelo-Datomic库感兴趣,该库包含许多与Datomic进行交互的辅助功能和便利功能。
享受!
PS不要忽略d/squuid
用于生成半顺序UUID的Datomic函数,这是在Datomic中生成新UUID的更有效方法
更新:将数据添加到Datomic有点令人困惑,并且比需要的要复杂得多。这就是为什么您可以使用Tupelo-Datomic简化整个操作的原因:
(td/transact *conn*
(td/new-entity { :person/name "James Bond" :location "London" :weapon/type #{ :weapon/gun :weapon/wit } } )
(td/new-entity { :person/name "M" :location "London" :weapon/type #{ :weapon/gun :weapon/guile } } )
(td/new-entity { :person/name "Dr No" :location "Caribbean" :weapon/type :weapon/gun } ))
Tupelo-Datomic为您做的一件事是默默地为以下目的添加样板: {:db/id (d/tempid -partition) }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句