-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cursor_test.go
62 lines (48 loc) · 1.48 KB
/
cursor_test.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pageboy
import (
"encoding/json"
"fmt"
"net/url"
"gorm.io/gorm"
)
func ExampleCursor() {
db := openDB()
// Please execute it only once immediately after opening DB.
RegisterCallbacks(db)
type User struct {
gorm.Model
Name string
Age int
}
db.Migrator().DropTable(&User{})
db.AutoMigrate(&User{})
db.Create(&User{Name: "Alice", Age: 18})
db.Create(&User{Name: "Bob", Age: 22})
db.Create(&User{Name: "Carol", Age: 15})
// Get request url.
url, _ := url.Parse("https://localhost/path?q=%E3%81%AF%E3%82%8D%E3%83%BC")
// Default Values. You can also use `NewCursor()`.
cursor := &Cursor{Limit: 2, Reverse: false}
// Update values from a http request.
// Fetch Records.
var users []User
db.Scopes(cursor.Paginate("Age", "ID").Order("ASC", "DESC").Scope()).Find(&users)
fmt.Printf("len(users) == %d\n", len(users))
fmt.Printf("users[0].Name == \"%s\"\n", users[0].Name)
fmt.Printf("users[1].Name == \"%s\"\n", users[1].Name)
// Return the paging.
j, _ := json.Marshal(cursor.BuildNextPagingUrls(url))
fmt.Println(string(j))
// Output:
// len(users) == 2
// users[0].Name == "Carol"
// users[1].Name == "Alice"
// {"next":"https://localhost/path?after=18_1\u0026q=%E3%81%AF%E3%82%8D%E3%83%BC"}
}
func ExampleCursor_Order() {
cursor := &Cursor{Limit: 2, Reverse: false}
// For usually
cursor.Paginate("CreatedAt", "ID").Order("DESC", "ASC").Scope()
// For PostgresSQL
cursor.Paginate("CreatedAt", "ID").Order("DESC NULLS LAST", "ASC NULLS FIRST").Scope()
}