gpt4 book ai didi

clojure - 如何为 datomic 编写可重用的规范查询

转载 作者:行者123 更新时间:2023-12-04 22:48:22 24 4
gpt4 key购买 nike

我在编写我认为可重用的一般数据查询时遇到了麻烦。

例如,跟进这篇文章 is there a canonical way to grab all idents from a particular datomic partition? ,我安装了以下架构

{[63 :account/password] 
[64 :account/firstName]
[65 :account/lastName]
[62 :account/username]
[69 :email/prority]
[68 :email/address]}

我想要一个只显示具有给定命名空间的属性的函数。

该函数显示“:account”命名空间中的所有属性
(d/q '[:find ?e ?ident :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ":account")]] (d/db *conn*))

;; => [62 :account/username] [63 :account/password]
;; [64 :account/firstName] [65 :account/lastName]

但是,当我想编写一个可以接受输入的函数时,我必须在各处加上引号才能使其工作。
(defn get-ns-attrs [?ns db]
(d/q [':find '?e '?ident ':where
['?e ':db/ident '?ident]
['_ ':db.install/attribute '?e]
[(list '.toString '?ident) '?val]
[(list '.startsWith '?val (str ":" ?ns))]] db))

(get-ns-attrs "account" (d/db *conn*))
;; => [62 :account/username] [63 :account/password]
;; [64 :account/firstName] [65 :account/lastName]

(get-ns-attrs "email" (d/db *conn*))
;; => [69 :email/prority] [68 :email/address]

有一个更好的方法吗?

- - - 更新 - - - -

供人们尝试的完整代码在这里:
(ns schema.start
(:require [datomic.api :as d])
(:use [clojure.pprint :only [pprint]]))

(def *uri* "datomic:mem://login-profile")
(d/create-database *uri*)
(def *conn* (d/connect *uri*))

(defn boolean? [x]
(instance? java.lang.Boolean x))

(defn db-pair [attr kns val f]
(list (keyword (str "db/" (name attr)))
(f val kns)))

(defn db-enum [val kns]
(keyword (str "db." (name kns) "/" (name val))))

(def DB-KNS
{:ident {:required true
:check keyword?}
:type {:required true
:check #{:keyword :string :boolean :long :bigint :float
:double :bigdec :ref :instant :uuid :uri :bytes}
:attr :valueType
:fn db-enum}
:cardinality {:required true
:check #{:one :many}
:fn db-enum}
:unique {:check #{:value :identity}
:fn db-enum}
:doc {:check string?}
:index {:check boolean?}
:fulltext {:check boolean?}
:component? {:check keyword?}
:no-history {:check boolean?}})

(defn process-kns [m kns params res]
(let [val (m kns)]
(cond (nil? val)
(if (:required params)
(throw (Exception. (str "key " kns " is a required key")))
res)

:else
(let [chk (or (:check params) (constantly true))
f (or (:fn params) (fn [x & xs] x))
attr (or (:attr params) kns)]
(if (chk val)
(apply assoc res (db-pair attr kns val f))
(throw (Exception. (str "value " val " failed check"))))))))

(defn schema [m]
(loop [db-kns# DB-KNS
output {}]
(if-let [entry (first db-kns#)]
(recur (rest db-kns#)
(process-kns m (first entry) (second entry) output))
(assoc output
:db.install/_attribute :db.part/db
:db/id (d/tempid :db.part/db)))))

(def account-schema
[(schema {:ident :account/username
:type :string
:cardinality :one
:unique :value
:doc "The username associated with the account"})
(schema {:ident :account/password
:type :string
:cardinality :one
:doc "The password associated with the account"})
(schema {:ident :account/firstName
:type :string
:cardinality :one
:doc "The first name of the user"})
(schema {:ident :account/lastName
:type :string
:cardinality :one
:doc "The first name of the user"})
(schema {:ident :account/otherEmails
:type :ref
:cardinality :many
:doc "Other email address of the user"})
(schema {:ident :account/primaryEmail
:type :ref
:cardinality :one
:doc "The primary email address of the user"})])

(def email-schema
[(schema {:ident :email/address
:type :string
:cardinality :one
:unique :value
:doc "An email address"})
(schema {:ident :email/priority
:type :long
:cardinality :one
:doc "An email address's priority"})])

(d/transact *conn* account-schema)
(d/transact *conn* email-schema)

(defn get-ns-attrs1 [?ns db]
(d/q [':find '?e '?ident ':where
['?e ':db/ident '?ident]
['_ ':db.install/attribute '?e]
[(list '.toString '?ident) '?val]
[(list '.startsWith '?val (str ":" ?ns))]] db))

(defn get-ns-attrs2 [?ns db]
(d/q '[:find ?e ?ident :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ~(str ":" ?ns))]] db))


(get-ns-attrs1 "account" (d/db *conn*))
(get-ns-attrs1 "email" (d/db *conn*))

(get-ns-attrs2 "account" (d/db *conn*))
(get-ns-attrs2 "email" (d/db *conn*))

最佳答案

经过多读一些,我发现 :in关键字是所有这一切的关键。示例在教程的“高级查询”部分 - http://docs.datomic.com/tutorial.html .

这是列出 :account 中所有属性的等效查询。命名空间

(d/q '[:find ?e ?ident ?ns :in $ ?ns :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns)]]
(d/db *conn*)
"account")
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>

这是函数中的等价物
(defn get-ns-attrs [_ns db]
(d/q '[:find ?e ?ident :in $ ?ns :where
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns) ]] db (str _ns)))

(get-ns-attrs :account (d/db *conn*))

;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>

如果您需要更多的模块化,可以使用 % 进一步分割该功能。传递一组规则:
(def rule-nsAttrs
'[[nsAttrs ?e ?ident ?ns]
[?e :db/ident ?ident]
[_ :db.install/attribute ?e]
[(.toString ?ident) ?val]
[(.startsWith ?val ?ns)]])

(defn get-ns-attrs [_ns db]
(d/q '[:find ?e ?ident :in $ % ?ns :where
(nsAttrs ?e ?ident ?ns)]
(d/db *conn*)
[rule-nsAttrs]
(str _ns)))

(get-ns-attrs :account (d/db *conn*))
;; => #<HashSet [[68 :account/firstName], [67 :account/password], [71 :account/primaryEmail], [66 :account/username], [69 :account/lastName], [70 :account/otherEmails]]>

关于clojure - 如何为 datomic 编写可重用的规范查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14751771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com