Documentation ¶
Overview ¶
Package pageboy is a pagination library with GORM.
Index ¶
- type Cursor
- func (cursor *Cursor) BuildNextPagingUrls(base *url.URL) *CursorPagingUrls
- func (cursor *Cursor) GetNextAfter() pbc.CursorString
- func (cursor *Cursor) GetNextBefore() pbc.CursorString
- func (cursor *Cursor) Order(orders ...pbc.Order) *Cursor
- func (cursor *Cursor) Paginate(columns ...string) *Cursor
- func (cursor *Cursor) Scope() func(db *gorm.DB) *gorm.DB
- func (cursor *Cursor) Validate() error
- type CursorPagingUrls
- type Pager
- type PagerSummary
- type ValidationError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cursor ¶
type Cursor struct { Before pbc.CursorString `json:"before" query:"before"` After pbc.CursorString `json:"after" query:"after"` Limit int `json:"limit" query:"limit"` Reverse bool `json:"reverse" query:"reverse"` // contains filtered or unexported fields }
Cursor is a builder that build a GORM scope that specifies a range from the cursor position of records. You can read it from query or json.
Example ¶
db := openDB() 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 (*Cursor) BuildNextPagingUrls ¶
func (cursor *Cursor) BuildNextPagingUrls(base *url.URL) *CursorPagingUrls
BuildNextPagingUrls returns URLs for the user to access from the next cursor position.
You can use GetNextBefore and GetNextAfter if you want to customize the behavior.
func (*Cursor) GetNextAfter ¶
func (cursor *Cursor) GetNextAfter() pbc.CursorString
GetNextAfter returns a value of query to access if it exists some records after the current position.
func (*Cursor) GetNextBefore ¶
func (cursor *Cursor) GetNextBefore() pbc.CursorString
GetNextBefore returns a value of query to access if it exists some records before the current position.
func (*Cursor) Order ¶ added in v0.2.0
Order set the pagination orders, and returns self. The orders must be same order as columns that set to arguments of Paginate.
type CursorPagingUrls ¶
type CursorPagingUrls struct {
Next string `json:"next,omitempty"`
}
CursorPagingUrls is for the user to access from the next cursor position. If it is no records at target of next, Next will be empty.
type Pager ¶
type Pager struct { Page int `json:"page" query:"page"` PerPage int `json:"per_page" query:"per_page"` // contains filtered or unexported fields }
Pager is a builder that build a GORM scope that specifies a range of records.
Example ¶
db := openDB() type User struct { gorm.Model Name string } db.Migrator().DropTable(&User{}) db.AutoMigrate(&User{}) db.Create(&User{Name: "Alice"}) db.Create(&User{Name: "Bob"}) db.Create(&User{Name: "Carol"}) // Default Values. pager := &Pager{Page: 1, PerPage: 2} // Update values from a http request. // Fetch Records. var users []User db.Scopes(pager.Scope()).Order("id ASC").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 Summary. j, _ := json.Marshal(pager.Summary()) fmt.Println(string(j))
Output: len(users) == 2 users[0].Name == "Alice" users[1].Name == "Bob" {"page":1,"per_page":2,"total_count":3,"total_page":2}
type PagerSummary ¶
type PagerSummary struct { Page int `json:"page" query:"page"` PerPage int `json:"per_page" query:"per_page"` TotalCount int64 `json:"total_count" query:"total_count"` TotalPage int `json:"total_page" query:"total_page"` }
PagerSummary is summary of the query.
type ValidationError ¶ added in v1.0.0
ValidationError is a validation error.
func (*ValidationError) Error ¶ added in v1.0.0
func (err *ValidationError) Error() string