pilagod / gorm-cursor-paginator

A paginator doing cursor-based pagination based on GORM
https://github.com/pilagod/gorm-cursor-paginator
MIT License
192 stars 44 forks source link

Context not used? #37

Closed khalilsarwari closed 3 years ago

khalilsarwari commented 3 years ago

Hi,

If I am using context for the query I would like to paginate, it currently does not get passed on I believe.

For example,

stmt  := DB.WithContext(ctx).Where("user_id = ?",currentUser.ID)
result, nextp, err := p.Paginate(stmt, &cars)

I have something in ctx that is needed in the AfterFind of Car, and I think the pagination is replacing the ctx with another ctx?

pilagod commented 3 years ago

Yes, you're right. Currently context is automatically default to background context at here:

https://github.com/pilagod/gorm-cursor-paginator/blob/81f8cc8f39dec1dfe3a7d9facd0ed15c5c569ffd/paginator/paginator.go#L75

I didn't consider the custom context use case. I think we should let user decide their own context instead of providing a default one. Please give me some time to do testing for these scenarios.

@khalilsarwari Thanks for your finding, this is an important case 💪

pilagod commented 3 years ago

@khalilsarwari I have removed the context overridden line and release a new version v2.1.0. You can upgrade package and try to see if it solves the problem. 😃

Although clients now can use their own custom context, they also take the responsibility to manage db session between different query session. Refer to document for more details https://gorm.io/docs/method_chaining.html#Method-Chain-Safety-Goroutine-Safety

Thanks again for this significant finding! 💪