Skip to content

xiexianbin/gorm-paginate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gorm-paginate

Go Report Card License Go.Dev reference

English | 简体中文

GORM paginate plugin(program implementation for)

Feature

  • Paging

    • page: current page (default 1)
    • size: number of records per page (default 10)
  • Sorting

    • order_by: sort field and direction (e.g. created_at desc, name)
  • Filtering conditions

    • Format: <field>_<operator>=<value>.
      • E.g. age_gt=20 (where age > 20), name_like=John% (where LIKE 'John%')
    • Supported comparisons
      • eq equals (default)
      • ne is not equal to
      • gt greater than
      • gte greater than equals
      • lt less than
      • lte less than equals
      • between Range search
      • like fuzzy match
      • notlike non-fuzzy match
      • is
      • isnot
      • in

Examples

  1. Simple paging and sorting
GET http://localhost:8080/users?size=10&page=0&order_by=-name,id
  • Equivalent SQL
SELECT * FROM users ORDER BY name DESC, id ASC LIMIT 10 OFFSET 0
  • JSON Response:
{
  "page": 1,
  "size": 10,
  "order_bys": [
    {
      "field": "name",
      "direction": "desc"
    },
    {
      "field": "id",
      "direction": "asc"
    }
  ],
  "wheres": [],
  "comments": [],
  "items": [
    {
      "ID": 29,
      "CreatedAt": "2025-03-23T16:12:58.166418+08:00",
      "UpdatedAt": "2025-03-23T16:12:58.166418+08:00",
      "DeletedAt": null,
      "Name": "zxpet",
      "Age": 38,
      "Balance": 28,
      "AccountManager": "bbc"
    },
    ...
  ],
  "total": 200,
  "total_pages": 20
}
  1. Conditional Search
GET http://localhost:8080/users?size=10&page=1&age_gt=16&name_like=e%&balance_between=20,250&account_manager_in=zhangsi,lisi
  • Equivalent SQL
SELECT * FROM users WHERE age > 16 AND name LIKE "e%" and balance BETWEEN 20 AND 250 and account_manager in ("zhangsan", "lisi") LIMIT 10 OFFSET 0
  • JSON Response:
{
  "page": 1,
  "size": 10,
  "order_bys": [],
  "wheres": [
    {
      "field": "account_manager",
      "operator": "in",
      "value": [
        "zhangsi",
        "lisi"
      ]
    },
    {
      "field": "age",
      "operator": "gt",
      "value": "16"
    },
    {
      "field": "name",
      "operator": "like",
      "value": "e%"
    }
  ],
  "comments": [
    "invalid operator: between for field: balance"
  ],
  "items": [
    {
      "ID": 113,
      "CreatedAt": "2025-03-23T17:11:45.97259+08:00",
      "UpdatedAt": "2025-03-23T17:11:45.97259+08:00",
      "DeletedAt": null,
      "Name": "etiax",
      "Age": 22,
      "Balance": 12,
      "AccountManager": "zhangsi"
    }
  ],
  "total": 200,
  "total_pages": 20
}

License

© xiexianbin, 2025~time.Now

Released under the Apache License

About

Gorm Pagination plugin

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages