如何優雅的使用 GORM 進行分頁?

GORM[1] 是 Go 中使用最廣泛的 ORM 包,但儘管如此,它缺少一些 “基本” 功能。其中一個缺失的功能就是分頁(Pagination)。

分頁是管理應用程序中大型數據集的一個重要功能。它是一種限制和顯示數據庫中部分總數據的方法,這樣就不需要一次性檢索整個表,這樣可以極大的提高接口性能,降低超時失敗的概率。

雖然 GORM 提供了關於如何使用 scopes[2] 進行分頁的文檔,但在靈活性和可用性方面仍有改進的空間。在這裏,我將介紹一種使用 GORM 的 Clauses[3] 進行分頁的替代方法。

使用 Scopes 的分頁

GORM 的官網文檔介紹了使用 Scopes 進行分頁的代碼示例:

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
    return func (db *gorm.DB) *gorm.DB {
        // validate page and pageSize
        ...
        offset := (page - 1) * pageSize
        return db.Offset(offset).Limit(pageSize)
    }
}

page := 0
pageSize := 10
db.Scopes(Paginate(page, pageSize)).Find(&users)

因此,爲了使用分頁功能,我們需要使用以下代碼 db.Scopes(Paginate(page, pageSize))…

使用 Clauses 的分頁

一種不同且可以說更優雅的方法是使用 GORM Clauses,這種方法與使用 scope 略有不同,因爲 Clauses 負責修改數據庫查詢,特別是 WHERE 子句。

讓我們先定義分頁結構體:

type Pagination struct {
    page          int
    pageSize      int
}

func (p *Pagination) GetPage() int {
    return p.page
}
func (p *Pagination) GetPageSize() int {
    return p.pageSize
}

然後,讓我們實現兩個接口,以便在 Gorm Clauses 函數中使用這個結構體:

clause.Expression
gorm.StatementModifier

所以結構看起來是這樣的:

func (p *Pagination) ModifyStatement(stm *gorm.Statement) {
    // We modify statement to add the pagination
    db := stm.DB
    stm.DB.Limit(p.size).Offset((p.page - 1) * p.pageSize)
}

func (p *Pagination) Build(_ clause.Builder) {
    // The Build method is left empty since pagination does not require any additional SQL clauses.
}

之後,可以按照以下方式使用分頁:

pagination := Pagination{
    page: 0,
    pageSize: 10,
}
db.Clauses(&pagination).Find(&users)

爲了使這種方法可重用並增強其功能,這裏介紹一個小衆的包 PaGorminator[4]:一個利用此功能進行分頁的庫,同時添加了其他功能,如無分頁請求和自動填充元數據,如總頁數和總計數。

使用方法如下:

// Add plugin
_ = db.Use(pagorminator.PaGormMinator{})
...
pageRequest, _ := pagorminator.PageRequest(0, 10)
db.Clauses(pageRequest).Find(&users)
// this will apply pagination, and also populate pageRequest with:
// - the total number of pages
// - total count

參考資料

[1] 

GORM: https://gorm.io/index.html

[2] 

scopes: https://gorm.io/docs/scopes.html

[3] 

Clauses: https://gorm.io/gen/clause.html

[4] 

PaGorminator: https://github.com/manuelarte/pagorminator

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/K-DKZWC7VRzmcQzyqD6z0w