GithubHelp home page GithubHelp logo

goq's Introduction

๐ŸŒฑ Beta version

Goq

circleci appveyor

SQL-based DB access library for Gophers

Features

SQL-based API

Goq provides the low-level API which just wraps SQL clauses as Go methods, Instead of abstracting a way of query construction as an opinionated API like typical frameworks. That is, you already know most of the Goq API if you know SQL.

Flexible Result Mapping

Using Goq, you can collect result rows fetched from DB into various format structures: a slice of your model, single map, map of slices, combination of them, etc.

Custom Query Builder Generation

Goq can generate your custom query builder by go generate based on your models mapped to DB tables. This helps you write a query with more type safety and readability.

What does it look like?

import (
    "fmt"

    _ "github.com/lib/pq"
    "github.com/ryym/goq"
)

func main() {
    // Connect to DB.
    db, err := goq.Open("postgres", conn)
    panicIf(err)
    defer db.Close()

    // Initialize your builder.
    q := NewBuilder(db.Dialect())

    // Write a query.
    query := q.Select(
        q.Countries.ID,
        q.Countries.Name,
        q.Cities.All(),
    ).From(
        q.Countries,
    ).Joins(
        q.InnerJoin(q.Cities).On(
            q.Cities.CountryID.Eq(q.Countries.ID),
        ),
    ).Where(
        q.Countries.Population.Lte(500000),
        q.Cities.Name.Like("New%"),
    ).OrderBy(
        q.Countries.Name,
        q.Cities.Name,
    )

    var countries []Country
    var citiesByCountry map[int][]City

    // Collect results.
    err = db.Query(query).Collect(
        q.Countries.ToUniqSlice(&countries),
        q.Cities.ToSliceMap(&citiesByCountry).By(q.Countries.ID),
    )
    panicIf(err)

    fmt.Println("Complete!", countries, citiesByCountry)
}

Out of Scope Features

Goq is not a DB management framework so does not support any of these:

  • schema migration
  • schema generation from Go code
  • model generation from schema

Resources

Inspiration

Goq is inspired by ScalikeJDBC which is a Scala library providing SQL-based API.

goq's People

Contributors

ryym avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.