Code Monkey home page Code Monkey logo

gorm-metakit's Introduction

Pagination-Metakit

Pagination-Metakit is a Go package designed to simplify pagination and sorting functionalities for applications using GORM (Go Object-Relational Mapping) and Pure SQL. This package provides a Metadata structure to manage pagination and sorting parameters, and a GORM scope function to apply these settings to database queries, but not for only GORM, Pagination-Metakit also support pure sql pagination.

Overview

  • Pagination: Easily handle pagination with customizable page size.
  • Default Settings: Provides sensible defaults for page, page size, and sort direction.
  • Dual Functionality: Supports both GORM and pure SQL pagination.

Installation

To install the package, use go get:

go get github.com/Nicolas-ggd/paginate-metakit

Usage

Metadata Structure

The Metadata structure holds the pagination and sorting parameters:

type Metadata struct {
    Page          int    `form:"page" json:"page"`
    PageSize      int    `form:"page_size" json:"page_size"`
    Sort          string `form:"sort" json:"sort"`
    SortDirection string `form:"sort_direction" json:"sort_direction"`
    TotalRows     int64  `json:"total_rows"`
    TotalPages    int64  `json:"total_pages"`
}

Example Usage of GPaginate function

package main

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"fmt"
	"github.com/Nicolas-ggd/paginate-metakit"
	"net/http"
)

func main() {
	r.GET("/items", func(c *gin.Context) {
		var metadata metakit.Metadata

		// Bind metakit Metadata struct 
		err := c.ShouldBind(&metadata)
		if err != nil {
			c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err})
			return
		}

		// Count total rows
		var totalRows int64
		db.Model(&YourModel{}).Count(&totalRows)
		metadata.TotalRows = totalRows

		// Fetch paginated and sorted results
		var results []YourModel
		db.Scopes(metakit.GormPaginate(&metadata)).Find(&results)

		c.JSON(http.StatusOK, gin.H{"metadata": metadata, "results": results})
	})

	r.Run()
}

Example usage of SQLPaginate function

package main

import (
    "database/sql"
    "log"
    "net/http"
    "github.com/gin-gonic/gin"
    "github.com/Nicolas-ggd/paginate-metakit"
    _ "github.com/lib/pq"
)

func main() {
    db, err := sql.Open("postgres", "user=youruser dbname=yourdb sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    r := gin.Default()
    
    r.GET("/items", func(c *gin.Context) {
        var metadata metakit.Metadata
        
        // Bind metakit Metadata struct
        err := c.ShouldBind(&metadata)
        if err != nil {
            c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err})
            return
        }
        
        // Count total rows
        row := db.QueryRow("SELECT COUNT(*) FROM your_table")
        err = row.Scan(&metadata.TotalRows)
        if err != nil {
            c.JSON(http.StatusInternalServerError, gin.H{"error": err})
            return
        }

        // Fetch paginated and sorted results
        query := "SELECT * FROM your_table"
        rows, err := metakit.SQLPaginate(db, query, &metadata)
        if err != nil {
            c.JSON(http.StatusInternalServerError, gin.H{"error": err})
            return
        }
        defer rows.Close()

        var results []YourModel
        for rows.Next() {
            var item YourModel
            // Scan your results here
            results = append(results, item)
        }
        
        c.JSON(http.StatusOK, gin.H{"metadata": metadata, "results": results})
    })

    r.Run()
}

Contributing

This project is licensed under the MIT License. See the LICENSE file for details.

License

This project is licensed under the MIT License. See the LICENSE file for details.

gorm-metakit's People

Contributors

nicolas-ggd avatar

Stargazers

Nikolai Mishin avatar  avatar Saujana Shafi avatar Qusha avatar Yehoyada avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.