-
Notifications
You must be signed in to change notification settings - Fork 1
/
offset_paginator.go
64 lines (52 loc) · 1.19 KB
/
offset_paginator.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
57
58
59
60
61
62
63
64
package paging
import (
"strings"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
const defaultLimitVal = 50
// OffsetPaginator is the paginator for offset based pagination
type OffsetPaginator struct {
Limit int
Offset int
PageInfo PageInfo
orderBy string
}
// NewOffsetPaginator creates a new offset paginator
func NewOffsetPaginator(
page *PageArgs,
totalCount int64,
defaultLimit ...*int,
) OffsetPaginator {
if page == nil {
page = &PageArgs{}
}
limit := defaultLimitVal
if len(defaultLimit) > 0 && defaultLimit[0] != nil {
limit = *defaultLimit[0]
}
if page.First != nil {
limit = *page.First
}
offset := DecodeOffsetCursor(page.After)
orderBy := "created_at"
if len(page.sortByCols) > 0 {
orderBy = strings.Join(page.sortByCols, ", ")
}
if page.isDesc {
orderBy = orderBy + " DESC"
}
return OffsetPaginator{
Limit: limit,
Offset: offset,
PageInfo: NewOffsetBasedPageInfo(&limit, totalCount, offset),
orderBy: orderBy,
}
}
// QueryMods returns the sqlboilder query mods with pagination concerns
func (p *OffsetPaginator) QueryMods() []qm.QueryMod {
return []qm.QueryMod{
qm.Offset(p.Offset),
qm.Limit(p.Limit),
qm.OrderBy(p.orderBy),
}
}