Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
github.com/vcraescu/go-paginator
A simple way to implement pagination in Golang.
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/vcraescu/go-paginator"
"github.com/vcraescu/go-paginator/adapter"
"time"
)
type Post struct {
ID uint `gorm:"primary_key"`
Title string
Body string
PublishedAt time.Time
}
func main() {
db, err := gorm.Open("sqlite3", "my_db.db")
if err != nil {
panic(fmt.Errorf("db connection error: %s", err))
}
db.AutoMigrate(&Post{})
var posts []Post
q := db.Model(Post{}).Where("published_at > ?", time.Now())
p := paginator.New(adapter.NewGORMAdapter(q), 10)
p.SetPage(2)
if err = p.Results(&posts); err != nil {
panic(err)
}
for _, post := range posts {
fmt.Println(post.Title)
}
}
Some of other methods available:
p.HasNext()
p.HasPrev()
p.HasPages()
p.PageNums()
An adapter must implement the Adapter
interface which has 2 methods:
This way you can create your own adapter for any kind of data source you want to paginate.
type Adapter interface {
Nums() int
Slice(offset, length int, data interface{}) error
}
To paginate a GORM query builder.
q := db.Model(Post{}).Where("published_at > ?", time.Now())
p := paginator.New(adapter.NewGORMAdapter(q), 10)
To paginate a slice.
var pages []int
p := paginator.New(adapter.NewSliceAdapter(pages), 10)
View models contains all necessary logic to render the paginator inside a template.
Use it if you want to render a paginator similar to the one from Google search.
< Prev 2 3 4 5 6 7 8 9 10 11 Next >
func main() {
db, err := gorm.Open("sqlite3", "my_db.db")
if err != nil {
panic(fmt.Errorf("db connection error: %s", err))
}
db.AutoMigrate(&Post{})
var posts []Post
q := db.Model(Post{}).Where("published_at > ?", time.Now())
p := paginator.New(adapter.NewGORMAdapter(q), 10)
p.SetPage(7)
view := view.New(&p)
fmt.Println(view.Pages()) // [2 3 4 5 6 7 8 9 10 11]
fmt.Println(view.Next()) // 8
fmt.Println(view.Prev()) // 6
fmt.Println(view.Current()) // 7
}
Paginator is licensed under the MIT License.
FAQs
Unknown package
Did you know?
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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.