tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.49k stars 309 forks source link

Is there any doc to demo idiom way to implement pagination in datascript? #449

Closed idhowardgj94 closed 1 year ago

idhowardgj94 commented 1 year ago

As title, I'm trying to adopt datascript to my practice frontend project, and use it as an in-memory database in the browser,

Just wonder how to implement pagination using datascript? seems that the query result is not lazy collections, I think that performance would be suck if I just use cljs function like drop / take

tonsky commented 1 year ago

You are right! Queries are not lazy, but d/datoms is. It’s way faster, too. You can use (d/datoms db :aevt <attr>) go get all entities with specified attribute, then drop/take resulting collection. See https://docs.datomic.com/on-prem/query/indexes.html#datoms-api and https://docs.datomic.com/on-prem/clojure/index.html#datomic.api/datoms

idhowardgj94 commented 1 year ago

Thx for kindly reply. I've test datom api, now I'm wondering about how to get other attributes in a entity?

I'm not sure if my understanding is right, but I think that an entity may contains several attributes and value, and while I use datom :aevt I only get the attribute I select. Is there any way can I use, like query base on datom result? Or there is other way to achieve this job

tonsky commented 1 year ago

See pull, esp. pull-many https://docs.datomic.com/on-prem/query/pull.html. There’s also entity API https://docs.datomic.com/on-prem/overview/entities.html

idhowardgj94 commented 1 year ago

solved by using @tonsky's suggestion

;; :word is the attribute I selected
(->> (d/datoms (get-ds-db) :aevt :word)
       (take 10)
       (map :e)
       (#(d/pull (get-ds-db) '[*] (first %))))