Back to directory
slicebit avatar

qb

The database toolkit for go. Read more below about its uses, features, and usage.

Clone repository

git clone https://github.com/slicebit/qb.git

512

Stars

26

Forks

16

Watchers

LGPL-2.1

License

alt text

qb - the database toolkit for go

Build Status Coverage Status License (LGPL version 2.1) Go Report Card GoDoc

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.

About qb

qb is a database toolkit for easier db queries in go. It is inspired from python's best orm, namely sqlalchemy. qb is an orm(sqlx) as well as a query builder. It is quite modular in case of using just expression api and query building stuff.

Documentation

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.

Features

  • 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

go get -u github.com/slicebit/qb

If you want to install test dependencies then;

go get -u -t github.com/slicebit/qb

Quick Start

package main

import (
	"fmt"
	"github.com/slicebit/qb"
	_ "github.com/mattn/go-sqlite3"
    _ "github.com/slicebit/qb/dialects/sqlite"
)

type User struct {
	ID       string `db:"id"`
	Email    string `db:"email"`
	FullName string `db:"full_name"`
	Oscars   int    `db:"oscars"`
}

func main() {

	users := qb.Table(
		"users",
		qb.Column("id", qb.Varchar().Size(40)),
		qb.Column("email", qb.Varchar()).NotNull().Unique(),
		qb.Column("full_name", qb.Varchar()).NotNull(),
		qb.Column("oscars", qb.Int()).NotNull().Default(0),
		qb.PrimaryKey("id"),
	)

	db, err := qb.New("sqlite3", "./qb_test.db")
	if err != nil {
		panic(err)
	}

	defer db.Close()

	metadata := qb.MetaData()

	// add table to metadata
	metadata.AddTable(users)

	// create all tables registered to metadata
	metadata.CreateAll(db)
	defer metadata.DropAll(db) // drops all tables

	ins := qb.Insert(users).Values(map[string]interface{}{
		"id":        "b6f8bfe3-a830-441a-a097-1777e6bfae95",
		"email":     "[email protected]",
		"full_name": "Jack Nicholson",
	})

	_, err = db.Exec(ins)
	if err != nil {
		panic(err)
	}

	// find user
	var user User

	sel := qb.Select(users.C("id"), users.C("email"), users.C("full_name")).
		From(users).
		Where(users.C("id").Eq("b6f8bfe3-a830-441a-a097-1777e6bfae95"))

	err = db.Get(sel, &user)
	fmt.Printf("%+v\n", user)
}

Credits

Releases

0.2

Jul 8, 2016

0.2 - splinter

Download .zip

Changelog

minor fixes in documentation minor fixes in code linting errors fixes in travis build. travis now runs postgres 9.5.+

Enjoy!!

0.2-beta

Pre-release

Jun 25, 2016

0.2 beta

Download .zip

This release contains bugfixes of 0.2 alpha 2 & some additional features. Fixes: #60, #72, #71, #68, #67, #65, #66, #64, #63 Now, qb 0.2 is freezed. Breaking changes can only happen in 0.3 release. 0....

0.2-alpha2

Pre-release

Jun 19, 2016

0.2 alpha 2

Download .zip

The new expression api is introduced. It is more decentralized & constraintful than the builder Removed the builder #61 Logger is currently disabled but it will be reintroduced in 0.2 beta.

Until 0.2...

0.2-alpha

Pre-release

Jun 4, 2016

0.2 alpha

Download .zip

This release contains the new expression api. Until 0.2beta, there could be breaking changes. Fixes #24, #30, #32, #35, #36, #56, #58

0.1

May 14, 2016

0.1 - st.james infirmary

Download .zip

Changelog

fix #51, fix #52 improved test coverage to 100%

Enjoy!!