从Datomic实体获取所有字段 [英] Get all fields from a Datomic entity

查看:164
本文介绍了从Datomic实体获取所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Datomic查询和规则文档的嵌入部分说明:


像SQL这样的查询语言是围绕客户机 - 服务器模型
定位的,在单个会话中,您必须同时具有:




  • 回答你的基本问题,例如

  • 恢复报告和处理所需的任何其他信息,例如:他们的姓名和电子邮件地址是什么。



后者不是真正的查询,它只是一个机械导航
相关信息。


虽然我明白两个不同方面的正交性如何,我认为我经常需要检索



据我所知,查询通常具有以下格式:



<$> p $ p> (datomic.api / q'[:find?name?age?email
:where
[?e:myapp / name?name]
[?e:myapp / age?age]
[?e:myapp / email?email]]
(db conn))

如果我想检索具有N个属性的实体,我会让他们在每个查询中列出它们,这看起来很乏味,容易出错。



如何告诉Datomic检索带有它们持久化的所有字段的实体,而无需明确指定它们?

$ b $

.api / entity 提供了这样的功能。



它只是有getcha的返回地图有一个自定义的表示, code>:db / id 。可以访问这些字段,但打印它们需要将地图合并到常规Clojure地图中。


The 'Embedded' section of the Datomic Queries and Rules document says:

Query languages like SQL are oriented around a client-server model where, in a single conversaton, you are going to have to both:

  • Answer your fundamental question, e.g. who bought socks this month.
  • Recover any additional information required for reporting and processing, e.g. what are their names and email addresses.

The latter is not really a query, it is just a mechanical navigation to related information.

While I appreciate how the orthogonality of the two different mentioned aspects is honored, I think I'll often need to retrieve a whole entity, whichever its attributes are.

As far as I know, queries typically have this form:

(datomic.api/q '[:find ?name ?age ?email
                 :where
                 [?e :myapp/name ?name]
                 [?e :myapp/age ?age]
                 [?e :myapp/email ?email]]
               (db conn))

If I wanted to retrieve entities that have N attributes, I'd have them to list them all in each query, which seems tedious and error-prone to me.

How to tell Datomic to retrieve the entities with all the fields they were persisted with, without having to specify them explicitly?

解决方案

datomic.api/entity provides such functionality.

It just has the gotcha that the returned map has a custom representation which hides all fields but :db/id. One can access those fields, but printing them requires merging the map into a regular Clojure map.

这篇关于从Datomic实体获取所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆