查找对所有给定实体都是引用的实体

时间:2017-08-07 06:05:02

标签: datomic

我有一个角色实体,它引用:role/members中的用户实体。

我需要返回所有给定:role/name成员的所有用户,例如:

(ns myns.core
  (:require
   [datomic.api :as dat]))

(def conn (dat/connect "datomic:mem://my-datomic"))

(defn tempid
  []
  (dat/tempid :db.part/user))

(def first-user-tempid (tempid))
(def second-user-tempid (tempid))
(def third-user-tempid (tempid))

(def first-user {:db/id first-user-temp-eid
                 :user/name "first user"})

(def second-user {:db/id second-user-temp-eid
                  :user/name "second user"})

(def third-user {:db/id third-user-temp-eid
                 :user/name "third user"})

(def first-role {:db/id (tempid)
                 :role/name "first role"
                 :role/members [first-user third-user]})

(def second-role {:db/id (tempid)
                  :role/name "second role"
                  :role/members [second-user third-user]})

(def tx-report @(dat/transact conn
                              [first-user
                               second-user
                               third-user
                               first-role
                               second-role]))

(defn members-of
  [db role-names]
  ....)

(->> ["first role"
      "second role"]
     (members-of (:db-after tx-report))
     (map :user/name)) => ["third user"]

0 个答案:

没有答案