Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add additional column ordering to keysetpagination #640

Merged
merged 11 commits into from Dec 1, 2022
60 changes: 56 additions & 4 deletions pagination/keysetpagination/paginator.go
Expand Up @@ -5,16 +5,23 @@ package keysetpagination

import (
"fmt"
"strings"

"github.com/gobuffalo/pop/v6"
)

type (
Item interface{ PageToken() string }
Item interface{ PageToken() string }

columnOrdering struct {
name string
order string
}
Paginator struct {
token, defaultToken string
size, defaultSize, maxSize int
isLast bool
additionalColumn columnOrdering
}
Option func(*Paginator) *Paginator
)
Expand All @@ -40,6 +47,25 @@ func (p *Paginator) Size() int {
return size
}

func parseToken(idField string, s string) map[string]string {
tokens := strings.Split(s, "/")
if len(tokens) != 2 {
return map[string]string{idField: s}
}

r := map[string]string{}

for _, p := range tokens {
parts := strings.Split(p, "=")
if len(parts) != 2 {
continue
}
r[parts[0]] = parts[1]
zepatrik marked this conversation as resolved.
Show resolved Hide resolved
}

return r
}

func (p *Paginator) IsLast() bool {
return p.isLast
}
Expand All @@ -61,12 +87,28 @@ func (p *Paginator) ToOptions() []Option {
// q := c.Where("foo = ?", foo).Scope(keysetpagination.Paginate[Item](paginator))
func Paginate[I Item](p *Paginator) pop.ScopeFunc {
var item I
id := (&pop.Model{Value: item}).IDField()
model := &pop.Model{Value: item}
id := model.IDField()
return func(q *pop.Query) *pop.Query {
eid := q.Connection.Dialect.Quote(id)

tokenParts := parseToken(id, p.Token())
idValue := tokenParts[id]
if column, ok := model.Columns().Cols[p.additionalColumn.name]; ok {
quoteName := q.Connection.Dialect.Quote(column.Name)
zepatrik marked this conversation as resolved.
Show resolved Hide resolved

value := tokenParts[column.Name]

q = q.
Where(fmt.Sprintf("%s > ? OR (%s = ? AND %s > ?)", quoteName, quoteName, eid), value, value, idValue).
Order(fmt.Sprintf("%s %s", quoteName, p.additionalColumn.order))
} else {
q = q.
Where(fmt.Sprintf(`%s > ?`, eid), idValue)
}
return q.
Limit(p.Size()+1).
Where(fmt.Sprintf(`%s > ?`, eid), p.Token()).
Limit(p.Size() + 1).
// we always need to order by the id field last
Order(fmt.Sprintf(`%s ASC`, eid))
}
}
Expand Down Expand Up @@ -127,6 +169,16 @@ func WithSize(size int) Option {
}
}

func WithColumn(name string, order string) Option {
return func(opts *Paginator) *Paginator {
opts.additionalColumn = columnOrdering{
name: name,
order: order,
}
return opts
}
}

func withIsLast(isLast bool) Option {
return func(opts *Paginator) *Paginator {
opts.isLast = isLast
Expand Down
21 changes: 18 additions & 3 deletions pagination/keysetpagination/paginator_test.go
Expand Up @@ -14,7 +14,8 @@ import (
)

type testItem struct {
ID string `db:"pk"`
ID string `db:"pk"`
CreatedAt string `db:"created_at"`
}

func (t testItem) PageToken() string {
Expand All @@ -32,7 +33,7 @@ func TestPaginator(t *testing.T) {
q = q.Scope(Paginate[testItem](paginator))

sql, args := q.ToSQL(&pop.Model{Value: new(testItem)})
assert.Equal(t, "SELECT test_items.pk FROM test_items AS test_items WHERE \"pk\" > $1 ORDER BY \"pk\" ASC LIMIT 11", sql)
assert.Equal(t, "SELECT test_items.created_at, test_items.pk FROM test_items AS test_items WHERE \"pk\" > $1 ORDER BY \"pk\" ASC LIMIT 11", sql)
assert.Equal(t, []interface{}{"token"}, args)
})

Expand All @@ -46,7 +47,7 @@ func TestPaginator(t *testing.T) {
q = q.Scope(Paginate[testItem](paginator))

sql, args := q.ToSQL(&pop.Model{Value: new(testItem)})
assert.Equal(t, "SELECT test_items.pk FROM test_items AS test_items WHERE `pk` > ? ORDER BY `pk` ASC LIMIT 11", sql)
assert.Equal(t, "SELECT test_items.created_at, test_items.pk FROM test_items AS test_items WHERE `pk` > ? ORDER BY `pk` ASC LIMIT 11", sql)
assert.Equal(t, []interface{}{"token"}, args)
})

Expand Down Expand Up @@ -156,3 +157,17 @@ func TestParse(t *testing.T) {
require.ErrorIs(t, err, strconv.ErrSyntax)
})
}

func TestPaginateWithAdditionalColumn(t *testing.T) {
c, err := pop.NewConnection(&pop.ConnectionDetails{
URL: "postgres://foo.bar",
})
require.NoError(t, err)
q := pop.Q(c)
paginator := GetPaginator(WithSize(10), WithToken("pk=token_value/created_at=timestamp"), WithColumn("created_at", "DESC"))
q = q.Scope(Paginate[testItem](paginator))

sql, args := q.ToSQL(&pop.Model{Value: new(testItem)})
assert.Equal(t, "SELECT test_items.created_at, test_items.pk FROM test_items AS test_items WHERE \"created_at\" > $1 OR (\"created_at\" = $2 AND \"pk\" > $3) ORDER BY \"created_at\" DESC, \"pk\" ASC LIMIT 11", sql)
assert.Equal(t, []interface{}{"timestamp", "timestamp", "token_value"}, args)
}