I am trying to implement resursive rule (loglcal OR) by following this video - Lucas Cavalcanti & Edward Wible - Exploring four hidden superpowers of Datomic
Here the code
(defn owns? [cid pid db]
(d/q '{:find [?pur]
:in [$ ?cus-id ?pur %]
:where
[(owns? ?cus-id ?pur)]}
db cid [:purchase/id pid] owner-rules))
(comment
(owns?
#uuid "0fb7ea94-44af-46fa-98ca-0ddb5eb23123"
#uuid "24a96e20-f526-4f7f-ba38-4f684caa5607"
(d/db bank-conn)))
While running the above example I am getting this error:
java.lang.IllegalArgumentException Cannot resolve key: 24a96e20-f526-4f7f-ba38-4f684caa5607
Here is the full code:
(def purchase-schema
[{:db/ident :purchase/id
:db/doc "The purchase ID"
:db/unique :db.unique/identity
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one}
{:db/ident :purchase/account
:db/doc "Purchase Account"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}])
(def account-schema
[{:db/ident :account/id
:db/doc "The Account ID"
:db/unique :db.unique/identity
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one}
{:db/ident :account/customer
:db/doc "Customer who own's this account"
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}])
(def customer-schema
[{:db/ident :customer/id
:db/doc "Customer ID"
:db/unique :db.unique/identity
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one}
{:db/ident :customer/name
:db/doc "Customer name"
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}])
;;
;; Create client
(def client (d/client {:server-type :dev-local
:system "dev"}))
;; Create connections
(def bank-conn (d/connect client {:db-name "bank"}))
;; Transact schemas
(comment
(d/transact bank-conn {:tx-data purchase-schema})
(d/transact bank-conn {:tx-data account-schema})
(d/transact bank-conn {:tx-data customer-schema})
(d/tx-range bank-conn {:start 6}))
(comment
(java.util.UUID/randomUUID))
;; Transact fake data
(def cid #uuid "0fb7ea94-44af-46fa-98ca-0ddb5eb23123")
(def sample-customer {:customer/id cid
:customer/name "Jon Snow"})
(def aid #uuid "c579a9f6-f6c3-4718-8ecf-c3003021ee9a")
(def sample-account {:account/id aid
:account/customer [:customer/id cid]})
(def pid #uuid "24a96e20-f526-4f7f-ba38-4f684caa5607")
(def sample-purchase {:purchase/id pid
:purchase/account [:account/id aid]})
(comment
(d/transact bank-conn {:tx-data [sample-customer]})
(d/transact bank-conn {:tx-data [sample-account]})
(d/transact bank-conn {:tx-data [sample-purchase]}))
(comment
(d/q '[:find ?cus
:in $ ?cid ?pid
:where
[?pur :purchase/id ?pid]
[?pur :purchase/account ?acc]
[?acc :account/customer ?cus]
[?cus :customer/id ?cid]]
(d/db bank-conn)
(:customer/id sample-customer)
(:purchase/id sample-purchase)))
;; owns v1
(defn ownsv1? [cid pid db]
(d/q '[:find ?cus
:in $ ?customer-id ?purchase-id
:where
[?pur :purchase/id ?purchase-id]
[?pur :purchase/account ?acc]
[?acc :account/customer ?cus]
[?cus :customer/id ?customer-id]]
db cid pid))
(comment
(ownsv1?
(:customer/id sample-customer)
(:purchase/id sample-purchase)
(d/db bank-conn)))
;; recursive rule (logical OR)
(def owner-rules
'[[(owns? ?cus-id ?e)
[?e :customer/id ?cus-id]]
[(owns? ?cus-id ?e)
[?e ?ref-attr ?r]
(owns? ?cus-id ?r)]])
(defn owns? [cid pid db]
(d/q '{:find [?pur]
:in [$ ?cus-id ?pur %]
:where
[(owns? ?cus-id ?pur)]}
db cid [:purchase/id pid] owner-rules))
;;
(comment
(owns?
(:customer/id sample-customer)
(:purchase/id sample-purchase)
(d/db bank-conn)))