| |
|
|
| package db |
|
|
| import ( |
| "context" |
| "fmt" |
|
|
| "entgo.io/ent/dialect/sql" |
| "github.com/openmeterio/openmeter/pkg/pagination/v2" |
| "github.com/samber/lo" |
| ) |
|
|
| |
| |
| 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 |
| } |
|
|