Introduction

qb is a database toolkit for easier implementation for db heavy apps in go. The aim is to build a lightweight architecture on top of sqlx library for painless db implementation in go. It is inspired from python's most favorite sql package called sqlalchemy

Features

  • Support for postgres(9.5.+), mysql, mariadb & sqlite
  • An expression api which can build almost any common sql queries and table ddls
  • A transactional session api implemented on top of query builder that have commonly used db queries such as Find(), All(), etc.
  • A metadata api where tables can be generated and created from structs
  • A struct to table mapper with use of tagging
  • A query to struct mapper with use of sqlx library
  • Foreign key reference definitions with use of tagging
  • Single & composite column indices with use of tagging
  • Relationships (coming soon..)

Raison d'être

The reason this package is being developed is mainly because of my personal curiosity. It is currently a hobby project of mine. However, there are more several reasons. I have played with most of the ormish libraries in go. At the time, neither of them were complete and there is a quite well post about that which resonates the point.

Moreover, there is this tweet I had posted about the go db/orm community;

1286

From my perspective, I think qb solves most of the problems I'm suffering when using an orm in go and hopefully, it would be useful to anyone that has the similar problems with me.

Installation

To install qb, a simple go get would do the trick;

go get -v -u github.com/aacanakin/qb

To get the test dependencies add a -t flag;

go get -v -u -t github.com/aacanakin/qb

Moreover, glide is also supported;

glide get github.com/aacanakin/qb

Quick Start

package main

import (
    "fmt"
    "github.com/aacanakin/qb"
    "github.com/nu7hatch/gouuid"
)

type User struct {
    ID       string `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
    err := db.CreateAll()
    if err != nil {
      panic(err)
    }

    userID, _ := uuid.NewV4()
    db.Add(&User{
        ID:       userID.String(),
        Email:    "[email protected]",
        FullName: "Robert De Niro",
    })

    err = db.Commit() // insert user
    fmt.Println(err)

    var user User
    db.Find(&User{ID: userID.String()}).First(&user)

    fmt.Println("id", user.ID)
    fmt.Println("email", user.Email)
    fmt.Println("full_name", user.FullName)

    db.Metadata().DropAll() // drops all tables

}