modulepackage
1.0.1
Repository: https://github.com/strockefeller/gorm-linq.git
Documentation: pkg.go.dev
# README
Gorm-linq
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.
Installation
To install Gorm-linq, use go get
:
go get github.com/STRockefeller/gorm-linq
Usage
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)
License
Gorm-linq is released under the MIT License. See LICENSE file for details.
# Functions
No description provided by the author
NewDB creates a new instance of 'DB' by initializing the 'db' field with the provided 'gorm.DB' instance.
NewJoinOn returns a closure function that adds a join clause to a SQL query.
NewJoinQuery returns a closure function that can be used to join tables in a database query.
No description provided by the author
No description provided by the author
The `WhereInCondition` function returns a closure that takes a slice of any type `T` as input and returns a `QueryString` struct.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
DB is a generic struct that wraps around the 'gorm.DB' struct from the 'gorm' package.
QueryString is a struct that represents a SQL query string and its arguments.
# Interfaces
No description provided by the author
Contains a function called Where which returns an array of QueryString objects.
No description provided by the author