-
Notifications
You must be signed in to change notification settings - Fork 125
Home
Jet is a framework for writing type-safe SQL queries in Go, with ability to easily
convert database query result to desired arbitrary object structure.
Jet currently supports PostgreSQL
, MySQL
, CockroachDB
, MariaDB
and SQLite
. Future releases will add support for additional databases.
Jet is the easiest and fastest way to write complex SQL queries and map database query result
into complex object composition. It is not an ORM.
https://medium.com/@go.jet/jet-5f3667efa0cc
Use the command bellow to add jet as a dependency into go.mod
project:
$ go get -u github.com/go-jet/jet/v2
Jet generator can be installed in one of the following ways:
- (Go1.16+) Install jet generator using go install:
go install github.com/go-jet/jet/v2/cmd/jet@latest
Jet generator is installed to the directory named by the GOBIN environment variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH environment variable is not set.
- Install jet generator to specific folder:
git clone https://github.com/go-jet/jet.git
cd jet && go build -o dir_path ./cmd/jet
Make sure dir_path
folder is added to the PATH environment variable.
Jet requires running database instance with already defined database schema(with tables, views, enums etc), so that jet
generator can generate SQL Builder and Model files. File generation is very fast, and can be even added as pre-build step, but recommendation is to include generated files in the project repo.
Sample command:
jet -dsn=postgresql://jet:jet@localhost:5432/jetdb?sslmode=disable -schema=dvds -path=./.gen
Detail info about jet generator can be found at Generator.
Then next step is to import generated SQL Builder and Model files and write SQL queries in Go:
import . "some_path/.gen/jetdb/dvds/table"
import "some_path/.gen/jetdb/dvds/model"
To write SQL queries for PostgreSQL
and CockroachDB
import:
. "github.com/go-jet/jet/postgres"
To write SQL queries for MySQL
and MariaDB
import:
. "github.com/go-jet/jet/mysql"
To write SQL queries for SQLite
import:
. "github.com/go-jet/jet/sqlite"
*Dot import so that Go code resemble as much as native SQL. Dot import is not mandatory.
Write SQL:
// sub-query
rRatingFilms :=
SELECT(
Film.FilmID,
Film.Title,
Film.Rating,
).FROM(
Film,
).WHERE(
Film.Rating.EQ(enum.FilmRating.R),
).AsTable("rFilms")
// export column from sub-query
rFilmID := Film.FilmID.From(rRatingFilms)
// main-query
query := SELECT(
Actor.AllColumns,
FilmActor.AllColumns,
rRatingFilms.AllColumns(),
).FROM(
rRatingFilms.
INNER_JOIN(FilmActor, FilmActor.FilmID.EQ(rFilmID)).
INNER_JOIN(Actor, Actor.ActorID.EQ(FilmActor.ActorID),
).ORDER_BY(
rFilmID,
Actor.ActorID,
)
Store result into desired destination:
var dest []struct {
model.Film
Actors []model.Actor
}
err := query.Query(db, &dest)
- Home
- Generator
- Model
- SQL Builder
- Query Result Mapping (QRM)
-
FAQ
- How to execute jet statement in SQL transaction?
- How to construct dynamic projection list?
- How to construct dynamic condition?
- How to use jet in multi-tenant environment?
- How to change model field type?
- How to use custom(or currently unsupported) functions and operators?
- How to use IN/NOT_IN with dynamic list of values?
- Scan stopped working after naming a destination type