Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gitee.com/ostaer/gin-pagination

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gitee.com/ostaer/gin-pagination

  • v0.0.0-20230323131028-de0db9399f23
  • Go
  • Socket score

Version published
Created
Source
使用说明

1.引入

import "gitee.com/ostaer/gin-pagination"

2.使用中间件

app := gin.New()
paginationHandler := pagination.New(&pagination.CustomeParser{})
app.Get("/test", paginationHandler)

3.gorm引用查询数据

测试请求如middleware_test.go 110-115行

GET /test3?page=1&&size=3&&order=desc&&order_by=id&&is_child=true&&child_relation=1&&search.1.key=age&&search.1.value=5&&search.1.exactly=true&&search.2.key=email&&search.2.value=user-1,user-2&&search.2.exactly=false

查询处理如middleware_test.go 211-214行

// SELECT count(*) FROM `users`  WHERE ((`age` = "5") or ((`email` like "%user-1%") or (`email` like "%user-2%")))
DB.Model(&User{}).Scopes(pagination.PaginationScope(query, ip.Count)).Count(&count)
// SELECT * FROM `users`  WHERE ((`age` = "5") or ((`email` like "%user-1%") or (`email` like "%user-2%"))) ORDER BY id desc LIMIT 3 OFFSET 0
DB.Model(&User{}).Scopes(pagination.PaginationScope(query)).Find(&users)

4.拼接query

var query = pagination.PaginationQuery{
    PageNum: 1, PageSize: 10,
}
query.Search = append(query.Search, pagination.SearchGroup{
    SearchTerm: pagination.SearchTerm{
        Raw: &pagination.RawQuery{
            Query: "user_name = ? or nick_name = ?",
            Args: []interface{}{
                "张三", "四",
            },
        },
    },
})

自己组装gorm的Scope

var scope = func(db *gorm.DB) *gorm.DB {
	d := &pagination.PaginationQueryDB{DB: db, Query: query}
	d.ParseLimit().ParsePagination().ParseOrder()
	return d.DB
}
a.conn.Model(&model.Account{}).Scopes(scope).Where("id in ?", ids).Find(&accounts)

更多例子 参考middleware_test.go文件

文件说明
文件说明
model.go分页查询相关struct的定义, 如PaginationQuery
customParser.go负责解析查询参数到PaginationQuery
resolver.go负责解析PaginationQuery到gorm.DB Scopes
middleware.go实现兼容gin的中间件

FAQs

Package last updated on 23 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc