Orange is a lightweight, simple Object relational Mapper for Golang. Orange offers a simple API for building meaningful database queries, and good abstractions on top of standard database/sq
Orange is inspired by gorm
- Simple API
- Fast
- Auto migrations
- Multiple database support( currently only postgresql but mysql and sqlite are work in progress)
- Zero dependency( only the standard library)
- Simple SQL query building API
This is my understanding of Object Relational Mapping with Golang. Instead of
writing a blog post, I took the liberty to implement orange
. It has almost all
the things you might need to interact with databases with Golang.
The source code is geared toward people who want to harness the power of Go. There is a lot of myths around reflections in Go, I have almost used all the techniques you will need to master reflections.
THIS IS NOT FOR PRODUCTION USE, unless you know what you are doing in which case your contribution is welcome.
go get github.com/gernest/orange
The following is a simple example to showcase the power of orange, for comprehensive API please check The Orange Documentation
package orange_test
import (
"fmt"
"github.com/gernest/orange"
// Include the driver for your database
_ "github.com/lib/pq"
)
type golangster struct {
ID int64
Name string
}
func Example() {
// Open a database connection
connectionSTring := "user=postgres dbname=orange_test sslmode=disable"
db, err := orange.Open("postgres", connectionSTring)
if err != nil {
panic(err)
}
// Register the structs that you want to map to
err := db.Register(&golangster{})
if err != nil {
panic(err)
}
// Do database migrations( tables will be created if they dont exist
err = db.Automigrate()
if err != nil {
panic(err)
}
// Make sure we are connected to the database we want
name := db.CurrentDatabase()
fmt.Println(name) // on my case it is orange_test
// Insert a new record into the database
err = db.Create(&golangster{Name: "hello"})
if err != nil {
panic(err)
}
// count the number of records
var count int
db.Count("*").Bind(&count)
fmt.Println(count) // in my case 1
// Retrieve a a record with name hello
result := golangster{}
db.Find(&result, &golangster{Name: "hello"})
fmt.Println(result) // on my case { 1, "hello"}
}
These are some of the things I will hope to add when I get time
- Delete record
- Support mysql
- support sqlite
- more comprehensive tests
- improve perfomace
- talk about orange
Contributions of all kinds are welcome
Geofrey Ernest twitter @gernesti
MIT see LICENCE