This project is currently pre 1.
Currently, it's not feature complete. It can have potential bugs. There are no tests covering concurrency race conditions. It can crash especially in concurrency. Before 1.x releases, each major release could break backwards compatibility.
qb is a database toolkit for easier db usage in go. It is inspired from python's most favorite orm sqlalchemy. qb is an orm as well as a query builder. It is quite modular in case of using just expression api and query building stuff.
The documentation is hosted in readme.io which has great support for markdown docs. Currently, the docs are about 80% - 90% complete. The doc files will be added to this repo soon. Moreover, you can check the godoc from here. Contributions & Feedbacks in docs are welcome.
- Support for postgres(9.5.+), mysql & sqlite3
- Powerful expression API for building queries & table ddls
- Struct to table ddl mapper where initial table migrations can happen
- Transactional session api that auto map structs to queries
- Foreign key definitions
- Single & Composite column indices
- Relationships (soon.. probably in 0.3 milestone)
Installation with glide;
glide get github.com/aacanakin/qb0.1 installation with glide;
glide get github.com/aacanakin/qb#0.1Installation using go get;
go get -u github.com/aacanakin/qbIf you want to install test dependencies then;
go get -u -t github.com/aacanakin/qbpackage main
import (
"fmt"
"github.com/aacanakin/qb"
"github.com/nu7hatch/gouuid"
)
type User struct {
ID string `db:"_id" qb:"type:uuid; constraints:primary_key"`
Email string `qb:"constraints:unique, notnull"`
FullName string `qb:"constraints:notnull"`
Bio string `qb:"type:text; constraints:null"`
}
func main() {
db, err := qb.New("postgres", "user=postgres dbname=qb_test sslmode=disable")
if err != nil {
panic(err)
}
defer db.Close()
// add table to metadata
db.AddTable(User{})
// create all tables registered to metadata
db.CreateAll()
userID, _ := uuid.NewV4()
db.Add(&User{
ID: userID.String(),
Email: "robert@de-niro.com",
FullName: "Robert De Niro",
})
err = db.Commit() // insert user
if err != nil {
fmt.Println(err)
return
}
var user User
db.Find(&User{ID: userID.String()}).One(&user)
fmt.Println("id", user.ID)
fmt.Println("email", user.Email)
fmt.Println("full_name", user.FullName)
db.DropAll() // drops all tables
}package main
import (
"fmt"
"github.com/aacanakin/qb"
)
func main() {
db, _ := qb.New("sqlite3", ":memory:")
defer db.Close()
db.Dialect().SetEscaping(true)
actors := qb.Table(
"actor",
qb.Column("id", qb.Varchar().Size(36)),
qb.Column("name", qb.Varchar().NotNull()),
qb.PrimaryKey("id"),
)
db.Metadata().AddTable(actors)
err := db.CreateAll()
if err != nil {
panic(err)
}
ins := actors.Insert().Values(map[string]interface{}{
"id": "3af82cdc-4d21-473b-a175-cbc3f9119eda",
"name": "Robert De Niro",
})
_, err = db.Engine().Exec(ins)
if err != nil {
panic(err)
}
sel := actors.
Select(actors.C("name"), actors.C("id")).
Where(actors.C("name").Eq("Robert De Niro"))
var name string
var id string
db.Engine().QueryRow(sel).Scan(&name, &id)
fmt.Printf("<User name=%s id=%s/>\n", name, id)
// outputs
// <User name=Robert De Niro id=3af82cdc-4d21-473b-a175-cbc3f9119eda/>
}