datascript

Richie 2023-09-04T16:57:35.051809Z

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.

Richie 2023-09-04T16:59:52.846639Z

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.

Richie 2023-09-04T22:01:11.251789Z

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

Richie 2023-09-04T23:35:10.053729Z

Well. I guess one answer is that that's how datomic works. https://docs.datomic.com/client-api/datomic.client.api.html#var-index-range

Niki 2023-09-05T18:01:46.074689Z

Yes

Niki 2023-09-05T18:02:00.311439Z

Nobody asked for ranges inside one attribute so far, so

Niki 2023-09-05T18:04:14.954739Z

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)))

Niki 2023-09-05T18:04:51.840629Z

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

Richie 2023-09-05T22:40:54.317799Z

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!