File size: 1,409 Bytes
fea99b3 | 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 | // Code generated by ent, DO NOT EDIT.
package db
import (
"context"
"fmt"
"entgo.io/ent/dialect/sql"
"github.com/openmeterio/openmeter/pkg/pagination/v2"
"github.com/samber/lo"
)
// Cursor runs the query and returns a cursor-paginated response.
// Ordering is always by created_at asc, id asc.
func (_m *Example1Query) Cursor(ctx context.Context, cursor *pagination.Cursor) (pagination.Result[*Example1], error) {
if cursor != nil {
if err := cursor.Validate(); err != nil {
return pagination.Result[*Example1]{}, fmt.Errorf("invalid cursor: %w", err)
}
_m.Where(func(s *sql.Selector) {
s.Where(
sql.Or(
sql.GT(s.C("created_at"), cursor.Time),
sql.And(
sql.EQ(s.C("created_at"), cursor.Time),
sql.P(func(b *sql.Builder) {
b.WriteString("CAST(")
b.WriteString(s.C("id"))
b.WriteString(" AS TEXT) > ")
b.Args(cursor.ID)
}),
),
),
)
})
}
_m.Order(func(s *sql.Selector) {
s.OrderBy(sql.Asc(s.C("created_at")), sql.Asc(s.C("id")))
})
items, err := _m.All(ctx)
if err != nil {
return pagination.Result[*Example1]{}, err
}
if items == nil {
items = make([]*Example1, 0)
}
result := pagination.Result[*Example1]{
Items: items,
}
if len(items) > 0 {
last := items[len(items)-1]
result.NextCursor = lo.ToPtr(pagination.NewCursor(last.CreatedAt, fmt.Sprint(last.ID)))
}
return result, nil
}
|