https://github.com/ryym/goq
Yet another SQL builder and ORM mapper in Go
https://github.com/ryym/goq
go orm sql
Last synced: 11 months ago
JSON representation
Yet another SQL builder and ORM mapper in Go
- Host: GitHub
- URL: https://github.com/ryym/goq
- Owner: ryym
- License: mit
- Created: 2018-01-08T14:51:31.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-03-29T06:10:19.000Z (about 6 years ago)
- Last Synced: 2025-03-10T20:44:40.011Z (over 1 year ago)
- Topics: go, orm, sql
- Language: Go
- Homepage:
- Size: 5.54 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
🌱 Beta version
# Goq
[](https://circleci.com/gh/ryym/goq)
[](https://ci.appveyor.com/project/ryym/goq)
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`](https://blog.golang.org/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?
```go
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
- [Getting Started](https://github.com/ryym/goq/wiki/Getting-Started)
- [API document (GoDoc)](https://godoc.org/github.com/ryym/goq)
- [Sample application](https://github.com/ryym/goq-bookmark)
## Inspiration
Goq is inspired by [ScalikeJDBC](http://scalikejdbc.org/)
which is a Scala library providing SQL-based API.