A better ORM for Go and database/sql.
It uses non-empty interfaces, code generation (go generate), and initialization-time reflection
as opposed to interface{}, type system sidestepping, and runtime reflection. It will be kept simple.
Supported SQL dialects:
- PostgreSQL (tested with
github.com/lib/pq). - MySQL (tested with
github.com/go-sql-driver/mysql). - SQLite3 (tested with
github.com/mattn/go-sqlite3). - Microsoft SQL Server (tested with
github.com/denisenkom/go-mssqldb).
-
Make sure you are using Go 1.6+.
-
Install or update
reformpackage and command:go get -u gopkg.in/reform.v1/reform(see about versioning below). -
Define a model –
structrepresenting a table or view row. For example, store this in fileperson.go://go:generate reform //reform:people type Person struct { ID int32 `reform:"id,pk"` Name string `reform:"name"` Email *string `reform:"email"` CreatedAt time.Time `reform:"created_at"` UpdatedAt *time.Time `reform:"updated_at"` }
Magic comment
//reform:peoplelinks this model topeopletable or view in SQL database. First value inreformtag is a column name.pkmarks primary key. Use pointers for nullable fields. -
Run
reform [package or directory]orgo generate [package or file]. This will createperson_reform.goin the same package with typePersonTableand methods onPerson. -
See documentation how to use it. Simple example:
// Use reform.NewDB to create DB. // Save record (performs INSERT or UPDATE). person := &Person{ Name: "Alexey Palazhchenko", Email: pointer.ToString("[email protected]"), } if err := DB.Save(person); err != nil { log.Fatal(err) } // ID is filled by Save. person2, err := DB.FindByPrimaryKeyFrom(PersonTable, person.ID) if err != nil { log.Fatal(err) } fmt.Println(person2.(*Person).Name) // Delete record. if err = DB.Delete(person); err != nil { log.Fatal(err) } // Find records by IDs. persons, err := DB.FindAllFrom(PersonTable, "id", 1, 2) if err != nil { log.Fatal(err) } for _, p := range persons { fmt.Println(p) }
reform was born during summer 2014 out of frustrations with existing Go ORMs. All of them have a method
Save(record interface{}) which can be used like this:
orm.Save(User{Name: "gopher"})
orm.Save(&User{Name: "gopher"})
orm.Save(nil)
orm.Save("Batman!!")Now you can say that last invocation is obviously invalid, and that it's not hard to make an ORM to accept both first and second versions. But there are two problems:
- Compiler can't check it. Method's signature in
godocwill not tell us how to use it. We are essentially working against those tools by sidestepping type system. - First version is still invalid, since one would expect
Save()method to set record's primary key afterINSERT, but this change will be lost due to passing by value.
First proprietary version of reform was used in production even before go generate announcement.
This free and open-source version is the fourth milestone on the road to better and idiomatic API.
We are following Semantic Versioning, using gopkg.in and filling a changelog.
We use branch v1-stable (default on Github) for v1 development and tags v1.Y.Z for releases.
All v1 releases are SemVer-compatible, breaking changes will not be applied.
Canonical import path is gopkg.in/reform.v1.
go get -u gopkg.in/reform.v1/reform will install latest released version.
To install not yet released v1 version one can do checkout manually while preserving import path:
cd $GOPATH/src/gopkg.in/reform.v1
git fetch
git checkout origin/v1-stable
go install -v gopkg.in/reform.v1/reform
Branch v2-unstable is used for v2 development. It doesn't have any releases yet, and no compatibility is guaranteed.
Canonical import path is gopkg.in/reform.v2-unstable.
- github.com/AlekSi/pointer is very useful for working with reform structs with pointers.
- There should be zero
pkfields for Struct and exactly onepkfield for Record. Composite primary keys are not supported. pkfield can't be a pointer (== nildoesn't work).- Database row can't have a Go's zero value (0, empty string, etc.) in primary key column.