diff --git a/README.md b/README.md index 96661e9..8d7f8f7 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,11 @@ db.Scopes(pagorminator.WithPagination(pagorminator.PageRequestOf(0, 10))).Find(& ## 🎓 Examples -### Simple +- [Simple](./examples/simple/main.go) -Simple query with no filters +Simple query with no filters (where clause) -### Filtered +- Filtered Using where to filter diff --git a/examples/simple/go.mod b/examples/simple/go.mod index 5b76658..427468f 100644 --- a/examples/simple/go.mod +++ b/examples/simple/go.mod @@ -10,6 +10,7 @@ require ( require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/manuelarte/pagorminator v0.0.0-20241109213332-29069dcc8d67 // indirect github.com/mattn/go-sqlite3 v1.14.24 // indirect - golang.org/x/text v0.19.0 // indirect + golang.org/x/text v0.20.0 // indirect ) diff --git a/examples/simple/go.sum b/examples/simple/go.sum index 8bf3d26..a4ce51c 100644 --- a/examples/simple/go.sum +++ b/examples/simple/go.sum @@ -2,10 +2,14 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/manuelarte/pagorminator v0.0.0-20241109213332-29069dcc8d67 h1:HhZkJJJ8/xalo8HK4ENMG+7YsfvliSv8ALti4Sa2Y9s= +github.com/manuelarte/pagorminator v0.0.0-20241109213332-29069dcc8d67/go.mod h1:e7ZYAl1XwI3uc0rOXmfF4FToPSS+C65DM4sPXwRNkKs= github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= +golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= diff --git a/examples/simple/main.go b/examples/simple/main.go index dd71bfe..d0c6d60 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -1,7 +1,8 @@ package main import ( - "github.com/manuelarte/pagorminator/pagorminator" + "fmt" + "github.com/manuelarte/pagorminator" "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -12,8 +13,12 @@ type Product struct { Price uint } +func (p Product) String() string { + return fmt.Sprintf("Product{Code: %s, Price: %d}", p.Code, p.Price) +} + func main() { - db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) + db, err := gorm.Open(sqlite.Open("file:mem?mode=memory&cache=shared"), &gorm.Config{}) if err != nil { panic("failed to connect database") } @@ -26,16 +31,12 @@ func main() { db.Create(&Product{Code: "D42", Price: 100}) // Read - var product Product - db.First(&product, 1) // find product with integer primary key - db.First(&product, "code = ?", "D42") // find product with code D42 - - // Update - update product's price to 200 - db.Model(&product).Update("Price", 200) - // Update - update multiple fields - db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields - db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"}) - - // Delete - delete product - db.Delete(&product, 1) + var products []*Product + pageRequest, _ := pagorminator.PageRequestOf(0, 1) + db.Scopes(pagorminator.WithPagination(pageRequest)).First(&products) + for _, product := range products { + fmt.Printf("PageRequest: {Page: %d, Size: %d, TotalElements: %d, TotalPages: %d\n", + pageRequest.GetPage(), pageRequest.GetSize(), pageRequest.GetTotalElements(), pageRequest.GetTotalPages()) + println(product.String()) + } }