Skip to content

Commit

Permalink
docs: update docs to reflect usage of new Codec option
Browse files Browse the repository at this point in the history
sashahilton00 committed Sep 27, 2024
1 parent 354c14b commit d766606
Showing 1 changed file with 54 additions and 33 deletions.
87 changes: 54 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ A paginator doing cursor-based pagination based on [GORM](https://github.com/go-
- GORM `column` tag supported.
- Error handling enhancement.
- Exporting `cursor` module for advanced usage.
- Implement custom codec for cursor encoding/decoding.

## Installation

@@ -133,46 +134,66 @@ We first need to create a `paginator.Paginator` for `User`, here are some useful

4. By default the library encodes cursors with `base64`. If a custom encoding/decoding implementation is required, this can be implemented and passed as part of the configuration:

```go
func CreateUserPaginator(/* ... */) {
p := paginator.New(
&paginator.Config{
Rules: []paginator.Rule{
{
Key: "ID",
},
{
Key: "JoinedAt",
Order: paginator.DESC,
SQLRepr: "users.created_at",
NULLReplacement: "1970-01-01",
},
},
Limit: 10,
// supply a custom implementation for the encoder/decoder
CursorCodecFactory: NewCustomCodec,
// Order here will apply to keys without order specified.
// In this example paginator will order by "ID" ASC, "JoinedAt" DESC.
Order: paginator.ASC,
},
)
// ...
return p
}
```

Where the `NewCustomCodec` parameter is a function with the following signature:
First implement your custom codec such that it conforms to the `CursorCodec` interface:


```go
func(encoderFields []cursor.EncoderField, decoderFields []cursor.DecoderField) CursorCodec
type CursorCodec interface {
// Encode encodes model fields into cursor
Encode(
fields []pc.EncoderField,
model interface{},
) (string, error)
// Decode decodes cursor into model fields
Decode(
fields []pc.DecoderField,
cursor string,
model interface{},
) ([]interface{}, error)
}
type customCodec struct {}
func (cc *CustomCodec) Encode(fields []pc.EncoderField, model interface{}) (string, error) {
...
}
func (cc *CustomCodec) Decode(fields []pc.DecoderField, cursor string, model interface{}) ([]interface{}, error) {
...
}
```

Returning an implementation conforming to the `CursorCodec` interface:
Then pass an instance of your codec during initialisation:

```go
type CursorCodec interface {
Encode(model interface{}) (string, error)
Decode(cursor string, model interface{}) (fields []interface{}, err error)
func CreateUserPaginator(/* ... */) {
codec := &customCodec{}
p := paginator.New(
&paginator.Config{
Rules: []paginator.Rule{
{
Key: "ID",
},
{
Key: "JoinedAt",
Order: paginator.DESC,
SQLRepr: "users.created_at",
NULLReplacement: "1970-01-01",
},
},
Limit: 10,
// supply a custom implementation for the encoder/decoder
CursorCodec: codec,
// Order here will apply to keys without order specified.
// In this example paginator will order by "ID" ASC, "JoinedAt" DESC.
Order: paginator.ASC,
},
)
// ...
return p
}
```

0 comments on commit d766606

Please sign in to comment.