Discussion:
dynamic params to filter in queries
Tiago Frazão Valério
2017-04-04 00:01:40 UTC
Permalink
Hi, I'm new with Datomic and would like to have some help =D

I have the schema above:

;;contracts
{:db/id #db/id[:db.part/db]
:db/ident :contract/uuid
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity
:db/index true
:db/doc "The globally unique UUID for receivable"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/externalUUID
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "Operations's module uuid"
:db.install/_attribute :db.part/db}

{:db/id #db/id[:db.part/db]
:db/ident :contract/status
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "Contract's status"
:db.install/_attribute :db.part/db}

{:db/id #db/id[:db.part/db]
:db/ident :contract/number
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "Contract's number"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/date
:db/valueType :db.type/instant
:db/cardinality :db.cardinality/one
:db/doc "Contract's date"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/amount
:db/valueType :db.type/bigdec
:db/cardinality :db.cardinality/one
:db/doc "Contract's amount"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/disbursement
:db/valueType :db.type/bigdec
:db/cardinality :db.cardinality/one
:db/doc "Contract's disbursement"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/seller
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "Contract's Seller"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/buyer
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "Contract's Buyer"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/signers
:db/isComponent true
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/doc "Contract's signers"
:db.install/_attribute :db.part/db}
{:db/id #db/id[:db.part/db]
:db/ident :contract/operation
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one
:db/doc "Contracts's Operation"
:db.install/_attribute :db.part/db}

;;contract status
{:db/id #db/id[:db.part/user]
:db/ident :contract.status/signed}
{:db/id #db/id[:db.part/user]
:db/ident :contract.status/notSigned}]]}

I'm trying to query the contracts according to the user's filters in
frontend, so I did the function above:

(defn find-by-buyer
([db buyer-uuid]
(find-by-buyer db buyer-uuid {}))
([db buyer-uuid {{:keys [number status start-date end-date]} :filter}]
(->> (d/q '[:find [(pull ?e spec) ...]
:in $ ?id spec [?n] ?status ?start ?end
:where
[?e :contract/buyer ?b]
[?b :organization/uuid ?id]
[?e :contract/number ?number]
[(clojure.string/starts-with? ?number ?n)]
[?e :contract/status ?status]
[?e :contract/date ?date]
[(.after ?date ?start)]
[(.before ?date ?end)]] db buyer-uuid contract-pull-spec
[number] :contract.status/notSigned (utils/date->date-start-of-day
start-date) (utils/date->date-start-of-day end-date))
(sort-by :contract/date #(compare %2 %1)))))

But I have the folloing problem: When the user opens the contract interface
for the first time, it gives me the error: "Caused by: java.lang.Exception:
processing clause: [?e :contract/status ?status], message: Cannot resolve
key"
I believe thats because the status is an empty string. If I replace status
by hard code ":contract.status/notSigned" or ":contract.status/signed" the
query executes just fine.

So, how can I work with dynamic params so when the user does not select a
filter, the query executes correctly? Can anyone give me an example?

Thanks!
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...