-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
39 lines (32 loc) · 999 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"fmt"
"github.com/manuelarte/pagorminator"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Product struct {
gorm.Model
Code string
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("file:mem?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
_ = db.Use(pagorminator.PaGormMinator{})
_ = db.AutoMigrate(&Product{})
migrateProducts := []*Product{{Code: "1", Price: 1}, {Code: "10", Price: 10},
{Code: "20", Price: 20}, {Code: "21", Price: 21}}
db.CreateInBatches(&migrateProducts, len(migrateProducts))
fmt.Printf("%d products created\n", len(migrateProducts))
var products []*Product
pageRequest := pagorminator.UnPaged()
db.Clauses(pageRequest).Find(&products)
fmt.Printf("Unpaged(TotalElements: %d, TotalPages: %d)\n",
pageRequest.GetTotalElements(), pageRequest.GetTotalPages())
}