-
Notifications
You must be signed in to change notification settings - Fork 0
/
pag_first_map.clj
41 lines (40 loc) · 2.19 KB
/
pag_first_map.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(ns com.github.ivarref.clj-paginate.impl.pag-first-map
(:require [com.github.ivarref.clj-paginate.impl.bst2 :as bst2]
[com.github.ivarref.clj-paginate.impl.utils :as u]))
(defn paginate-first [m
{:keys [max-items
f
batch-f
sort-attrs
context
filter
sort-fn]
:or {f identity
batch-f identity
context nil}}
cursor-str]
(let [vecs (into [] (vals m))
decoded-cursor (u/maybe-decode-cursor cursor-str)
cursor (-> (merge {:context context}
(when filter {:filter filter})
decoded-cursor))
sort-fn (if sort-fn sort-fn (apply juxt sort-attrs))
nodes-plus-1 (if-let [from-value (get cursor :cursor)]
(do
(when (not= (count from-value) (count sort-attrs))
(throw (ex-info "Mismatch in size of :node-id-attrs and :cursor" {:node-id-attrs sort-attrs
:cursor from-value})))
(bst2/after-value2 vecs (zipmap sort-attrs from-value) sort-fn (inc max-items)))
(bst2/from-beginning vecs sort-fn (inc max-items)))
edges (u/get-edges (take max-items nodes-plus-1) batch-f f sort-attrs cursor)
hasPrevPage (or (when (not-empty nodes-plus-1)
(not= (first nodes-plus-1)
(first (bst2/from-beginning vecs sort-fn 1))))
(and (empty? nodes-plus-1)
(some? cursor-str)))]
{:edges edges
:pageInfo {:hasPrevPage (true? hasPrevPage)
:hasNextPage (= (count nodes-plus-1) (inc max-items))
:startCursor (or (get (first edges) :cursor) cursor-str)
:endCursor (or (get (last edges) :cursor) cursor-str)
:totalCount (bst2/total-count vecs)}}))