Durazzo is a simple and efficient ORM library for Go that provides a powerful, flexible, and easy-to-use interface for interacting with relational databases using raw SQL queries and model-based migrations. Still under development new features will come up soon.
To install Durazzo, run the following Go command:
$ go get github.com/EraldCaka/durazzo
Once installed, you can begin using Durazzo in your Go projects to interact with databases easily.
Durazzo works by first setting up a connection to your database, and then interacting with the database using models. Here’s how to get started with Durazzo:
- Create a model (e.g.,
User
):
type User struct {
ID int `durazzo:"primary_key"`
Name string `durazzo:"size:100"`
Email string `durazzo:"unique"`
}
- Initialize Durazzo:
package main
import (
"github.com/EraldCaka/durazzo/pkg/durazzo"
"log"
)
func main() {
db := durazzo.NewDurazzo(durazzo.Config{
Driver: durazzo.Postgres,
DSN: "postgresql://username:password@localhost:5432/yourdbname?sslmode=disable",
})
err := db.AutoMigrate(&User{})
if err != nil {
log.Fatal("Error during migration:", err)
}
userBody := User{ID: 1, Name: "erald", Email: "erald@gmail.com"}
if err := newDurazzo.Insert(&userBody).Run(); err != nil {
log.Fatalf("Error inserting user: %v", err)
}
}
Durazzo makes it easy to work with your database through interfaces or raw SQL queries. Here are the common operations:
To insert a new record:
user := User{Name: "erald", Email: "erald@yahoo.com"}
err := db.Insert(&user).Run()
Fetching records from the database:
var users []User
err := db.Select(&users).Where("email", "erald@yahoo.com").Run()
To update a record:
err := db.Update(&User{}).Set("email", "erald@gmail.com").Where("name", "erald").Run()
To delete a record:
err := db.Delete(&User{}).Where("name", "erald").Run()
Durazzo allows you to execute raw SQL queries directly, for operations such as joins, complex selects, and more.
var users1 []User
err = newDurazzo.Raw("SELECT * FROM user ORDER BY id ASC LIMIT $1", 2).Model(&users1).Run()
Durazzo comes with built-in support for testing your database operations.
Once your database is set up for testing, you can execute tests using the following command:
$ make test
© EraldCaka, 2024
Released under the MIT License