Gorm-linq is a library that provides a LINQ-like interface for the GORM ORM library. It allows you to write more expressive queries and perform common database operations in a more concise and readable way.
To install Gorm-linq, use go get
:
go get github.com/STRockefeller/gorm-linq
First, import the linq
package:
import "github.com/STRockefeller/gorm-linq"
Then, create a new gorm.DB
instance and pass it to linq.NewDB
along with the struct type you want to work with:
db, err := gorm.Open(mysql.Open("dsn"), &gorm.Config{})
if err != nil {
// handle error
}
type User struct {
ID int
Name string
}
users := linq.NewDB[User](db)
You can then use gorm-linq
methods to query the database:
// Find all users where Name starts with "A"
var result linq.Linq[User]
err := users.Where(User{Name: "A%"}).Find(context.Background(), &result)
// Find the first user where ID is 42
var user User
err := users.Where(User{ID: 42}).Take(context.Background(), &user)
// Update all users where Name starts with "A"
rowsAffected, err := users.Where(User{Name: "A%"}).Updates(context.Background(), User{Name: "NewName"})
// Delete all users where Name starts with "A"
rowsAffected, err := users.Delete(context.Background(), User{Name: "A%"})
// Find all users for update
var result linq.Linq[User]
err := users.FindForUpdate(context.Background(), linq.NoWait(), &result)
You can also chain multiple gorm-linq
methods together to build more complex queries:
// Find all users where Name starts with "A" and ID is less than 100, ordered by Name
var result linq.Linq[User]
err := users.Where(User{Name: "A%"}).WhereRaw("ID < ?", 100).Order("Name").Find(context.Background(), &result)
gorm-linq
also supports upserts:
// Upsert a single user
err := users.Upsert(context.Background(), []User{{ID: 42, Name: "NewName"}}, clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{"name"}),
})
You can also use gorm-linq
with GORM's Scope
method to apply a function to the underlying gorm.DB
instance:
users.Scope(func(db *gorm.DB) *gorm.DB {
return db.Where("deleted_at IS NULL")
}).Find(context.Background(), &result)
Gorm-linq is released under the MIT License. See LICENSE file for details.