Fork me on GitHub
#xtdb
<
2021-04-13
>
bartuka19:04:56

I am wondering how can I perform casting during a query? I have fields in crux that are datetime, but I want to get them back as date

refset19:04:22

so, you essentially want to do something like (.toDate ?my-datetime) just before returning the results?

refset19:04:37

you can't use Clojure's Java interop like that directly within a query, but you can wrap the call in a Clojure function that can be used as a custom predicate

refset19:04:04

like

(defn datetime->date [dt]
  (.toDate dt))

(crux/q db '{:find [d]
             :where [[:my-e :date my-date]
                     [(my.ns/datetime->date my-date) d]]}

bartuka20:04:16

thanks @U899JBRPF I will test this. My idea is that I am using aggregation and want to aggregate by date not datetime

bartuka20:04:38

worked just fine. Great! Thanks again @U899JBRPF

🙏 2
bartuka20:04:26

I noticed the need to specify the namespace, so do you have more docs on custom predicates?

refset21:04:58

No additional docs at the moment...but that's pretty much all there is to it. You can also destructure the output like :in

refset21:04:23

We have various examples in the tests

bartuka23:04:45

cool, thanks for pointing these out

jarohen12:04:27

if you're not using the date within the query, it might be more straightforward to operate on the resultset instead - (->> (crux/q ...) (map first) (map ->date))

jarohen12:04:12

or (map #(update % 0 ->date)), there's a whole load of different ways 🙂