-
Notifications
You must be signed in to change notification settings - Fork 347
/
paginate.go
56 lines (48 loc) · 1.2 KB
/
paginate.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package bunutils
import (
"context"
"github.com/uptrace/bun"
"github.com/determined-ai/determined/proto/pkg/apiv1"
)
// Paginate calculates pagination info for a Bun query and returns the SelectQuery for the
// caller to execute.
func Paginate(
ctx context.Context, q *bun.SelectQuery, offset, limit int,
) (*bun.SelectQuery, *apiv1.Pagination, error) {
// Count number of items without any limits or offsets.
total, err := q.Count(ctx)
if err != nil {
return nil, nil, err
}
// Calculate end and start indexes.
startIndex := offset
if offset > total || offset < -total {
startIndex = total
} else if offset < 0 {
startIndex = total + offset
}
endIndex := startIndex + limit
switch {
case limit == -2:
endIndex = startIndex
case limit == -1:
endIndex = total
case limit == 0:
endIndex = 100 + startIndex
if total < endIndex {
endIndex = total
}
case startIndex+limit > total:
endIndex = total
}
// Add start and end index to query.
q.Offset(startIndex)
q.Limit(endIndex - startIndex)
return q, &apiv1.Pagination{
Offset: int32(offset),
Limit: int32(limit),
Total: int32(total),
StartIndex: int32(startIndex),
EndIndex: int32(endIndex),
}, nil
}