gopaq

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2022 License: MIT Imports: 1 Imported by: 0

README

codecov

GOPAQ - Gorm Paginated Query

Limit-offset pagination for GORM queries.

Example

package main

import (
	"fmt"
	"os"

	"github.com/DAtek/gopaq"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)


type Plant struct {
	Id   uint
	Type string
	Name string
}

// You have to implement this function
func getSession() *gorm.DB {
    return nil
}

func main() {
	session := getSession()

	for i := 0; i < 10; i++ {
		session.Create(&Plant{})
	}

	plant1 := &Plant{Name: "Banana"}
	session.Create(plant1)

	plant2 := &Plant{Name: "Apple"}
	session.Create(plant2)

	query := session.Model(&Plant{}).Order("name DESC")
	result, _ := gopaq.FindWithPagination(query, []*Plant{}, 1, 2)

	fmt.Printf("plant1: %v\n", result.Items[0])
	fmt.Printf("plant2: %v\n", result.Items[1])
	fmt.Printf("returned items: %v\n", len(result.Items))
	fmt.Printf("total: %v\n", result.Total)
}

Output:

plant1: &{11  Banana}
plant2: &{12  Apple}
returned items: 2
total: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLimit = 50

Functions

This section is empty.

Types

type PaginatedQueryResult

type PaginatedQueryResult[T interface{}] struct {
	Total uint
	Items T
}

func FindWithPagination

func FindWithPagination[T interface{}](
	query *gorm.DB,
	items T,
	page uint,
	pageSize uint,
) (*PaginatedQueryResult[T], error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL