Fork me on GitHub
#datascript
<
2023-09-04
>
Richie16:09:35

Hey! Why does index-range only support :avet? I have a :db.cardinality/many :occurrences that are instants and I want to get the instants that fall in a range for an entity. I can use seek-datoms with :eavt and then use take-while to ensure that I only consume while it's still the desired entity and attribute. index-range seems like the more appropriate function. It would be perfect if I wanted all :occurrences inside of a range but I only want to query for one entity.

Richie16:09:52

I see that both index-range and seek-datoms call slice under the hood. So, it indexes the b-tree to get the starting datom and walks from there.

Richie22:09:11

This is what I'm doing with take-while .

Niki18:09:00

Nobody asked for ranges inside one attribute so far, so

Niki18:09:14

I would’ve cached eid, but other that that, it should work

(let [eid (:db/id (ds/entity (get-ds-db) [:title title]))]
  (->>
    (ds/seek-datoms (get-ds-db) :eavt eid :occurrences (jt.instant/minus-seconds now 5))
    (take-while
      #(and
         (= eid (:e %))
         (= :occurrences (:a %))))
    (mapv :v)))

Niki18:09:51

TBH even with index-range, I used it probably once, so not sure how popular those are

Richie22:09:54

Ok thanks. I’ll keep going with this since it works. If I’m up for some fun then I’ll look at using slice. Thanks again!