Skip to content

Commit

Permalink
breaking change: refactor EnsureLimits method to emphasize fix error …
Browse files Browse the repository at this point in the history
…limit parameters (#6)

* breaking change: Refactored EnsureLimits method to emphasize fix error parameter

* Update middleware.go
  • Loading branch information
molon authored Dec 10, 2024
1 parent 011e921 commit 389062e
Show file tree
Hide file tree
Showing 8 changed files with 619 additions and 140 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ p := relay.New(
// Keyset-based pagination
return gormrelay.NewKeysetAdapter[*User](db)(ctx, req)
}),
// maxLimit / limitIfNotSet
relay.EnsureLimits[*User](100, 10),
// defaultLimit / maxLimit
relay.EnsureLimits[*User](10, 100),
// Append primary sorting fields, if any are unspecified
relay.EnsurePrimaryOrderBy[*User](
relay.OrderBy{Field: "ID", Desc: false},
Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func GetSkip(ctx context.Context) Skip {

type ctxKeyNodeProcessor struct{}

func WithNodeProcessor[T any](ctx context.Context, processor func(node T) T) context.Context {
func WithNodeProcessor[T any](ctx context.Context, processor func(ctx context.Context, node T) (T, error)) context.Context {
return context.WithValue(ctx, ctxKeyNodeProcessor{}, processor)
}

func GetNodeProcessor[T any](ctx context.Context) func(node T) T {
processor, _ := ctx.Value(ctxKeyNodeProcessor{}).(func(node T) T)
func GetNodeProcessor[T any](ctx context.Context) func(ctx context.Context, node T) (T, error) {
processor, _ := ctx.Value(ctxKeyNodeProcessor{}).(func(ctx context.Context, node T) (T, error))
return processor
}
2 changes: 2 additions & 0 deletions gormrelay/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func scopeKeyset(after, before *map[string]any, orderBys []relay.OrderBy, limit

if limit > 0 {
exprs = append(exprs, clause.Limit{Limit: &limit})
} else {
db.AddError(errors.New("limit must be greater than 0"))
}

return db.Clauses(exprs...)
Expand Down
Loading

0 comments on commit 389062e

Please sign in to comment.