Type safe SQL builder with code generation and automatic query result data mapping
Type safe SQL builder with code generation and automatic query result data mapping
Jet is a complete solution for efficient and high performance database access, combining a type-safe SQL builder with code generation and automatic query result mapping.
Jet currently supports the following database engines:
PostgreSQLMySQLSQLiteThis list is not exclusive, as many other databases implement compatible wire protocols. For example, CockroachDB uses the
PostgreSQL wire protocol, and MariaDB is based on the MySQL protocol. Both databases are tested and known to work with Jet.
Support for additional databases may be introduced in future releases.
Jet is the easiest, and the fastest way to write complex type-safe SQL queries as a Go code and map database query result into complex object composition.
[!Note] Jet is not an ORM.
https://medium.com/@go.jet/jet-5f3667efa0cc
Auto-generated type-safe SQL Builder. Statements supported:
(DISTINCT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET, FOR, LOCK_IN_SHARE_MODE, UNION, INTERSECT, EXCEPT, WINDOW, sub-queries)(VALUES, MODEL, MODELS, QUERY, ON_CONFLICT/ON_DUPLICATE_KEY_UPDATE, RETURNING),(SET, MODEL, WHERE, RETURNING),(WHERE, ORDER_BY, LIMIT, RETURNING),(IN, NOWAIT), (READ, WRITE)Auto-generated Data Model types - Go types mapped to database type (table, view or enum), used to store result of database queries. Can be combined to create complex query result destination.
Query execution with result mapping to arbitrary destination.
To install Jet package, you need to install Go and set your Go workspace first.
Go version 1.24+ is required
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 using one of the following methods:
go install github.com/go-jet/jet/v2/cmd/jet@latest
[!Tip] Jet generator is installed to the directory named by the
GOBINenvironment variable, which defaults to$GOPATH/binor$HOME/go/binif theGOPATHenvironment variable is not set.
git clone https://github.com/go-jet/jet.git
cd jet && go build -o <target_directory> ./cmd/jet
[!Tip] Make sure
target_directoryis included in your system’sPATHenvironment variable to allow global access to the jet command.
For this quick start example we will use PostgreSQL sample 'dvd rental' database. Full database dump can be found in ./tests/testdata/init/postgres/dvds.sql. A schema diagram illustrating the relevant part of the database is available here.
To generate jet SQL Builder and Data Model types from running postgres database, we need to call jet generator with postgres
connection parameters and destination folder path.
Assuming we are running local postgres database, with user user, user password pass, database jetdb and
schema dvds we will use this command:
jet -dsn=postgresql://user:pass@localhost:5432/jetdb?sslmode=disable -schema=dvds -path=./.gen
Connecting to postgres database: postgresql://user:pass@localhost:5432/jetdb?sslmode=disable
Retrieving schema information...
FOUND 15 table(s), 7 view(s), 1 enum(s)
Cleaning up destination directory...
Generating table sql builder files...
Generating view sql builder files...
Generating enum sql builder files...
Generating table model files...
Generating view model files...
Generating enum model files...
Done
Procedure is similar for MySQL, CockroachDB, MariaDB and SQLite. For example:
jet -source=mysql -dsn="user:pass@tcp(localhost:3306)/dbname" -path=./.gen
jet -dsn=postgres://user:pass@localhost:26257/jetdb?sslmode=disable -schema=dvds -path=./.gen #cockroachdb
jet -dsn="mariadb://user:pass@tcp(localhost:3306)/dvds" -path=./.gen # source flag can be omitted if data source appears in dsn
jet -source=sqlite -dsn="/path/to/sqlite/database/file" -schema=dvds -path=./.gen
jet -dsn="file:///path/to/sqlite/database/file" -schema=dvds -path=./.gen # sqlite database assumed for 'file' data sources
*User has to have a permission to read information schema tables.
As indicated by the command output, Jet will perform the following actions:
tables, views, and enums within the dvds schema../.gen/jetdb/dvds.Generated files folder structure will look like this:
…
Types from the table, view, and enum packages are used to write type-safe SQL queries in Go, while types from the model types are combined to store
results of the SQL queries.
[!Note] It is possible to customize the default Jet generator behavior. All the aspects of generated SQLBuilder and model types are customizable(see wiki).
First we need to import postgres SQLBuilder and generated packages from the previous step:
import (
// dot import so go code would resemble as much as native SQL
// dot import is not mandatory
. "github.com/go-jet/jet/v2/examples/quick-start/.gen/jetdb/dvds/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/examples/quick-start/.gen/jetdb/dvds/enum"
"github.com/go-jet/jet/v2/examples/quick-start/.gen/jetdb/dvds/model"
)
Let's say we want to retrieve the list of all actors who acted in films longer than 180 minutes, film language is 'English', film category is not 'Action' and film rating is not 'R'.
…
[!Tip] Package(dot) import is used, so the statements look as close as possible to the native SQL.
Note that every column has a type. String columns, such as Language.Name and Category.Name can only be compared with
string columns and expressions. Similarly, Actor.ActorID, FilmActor.ActorID, Film.Length are integer columns
and can only be compared with integer columns and expressions. The same type safety rules apply to arrays and their
element types.
How to Get a Parametrized SQL Query from the Statement?
query, args := stmt.Sql()
query - parametrized query
args - query arguments
…
[English Action 180 Trailers]
How to Get Debug SQL from Statement?
debugSql := stmt.DebugSql()
debugSql - this query string can be copy-pasted into sql editor and executed.
Click to see debug sql[!Warning] Debug SQL is not intended to be used in production. For debug purposes only!!!
…
Well-formed SQL is just a first half of the job. Let's see how can we make some sense of result set returned executing above statement. Usually this is the most complex and tedious work, but with Jet it is the easiest.
First, we need to define the structure in which to store the query result. This can be achieved by combining autogenerated model types, or by using custom model types(see wiki for more information).
Let's say this is our desired structure made of autogenerated types:
var dest []struct {
model.Actor
Films []struct {
model.Film
Language model.Language
Categories []model.Category
}
}
The Films field is a slice because an actor can appear in multiple films,
and each film is associated with a single language. The Language field,
on the other hand, is a single model struct. A Film can belong to multiple
categories.
[!Note] There is no limitation of how big or nested destination can be.
Now, let's execute the above statement on an open database connection (or
transaction) db and store the result in the dest variable.
err := stmt.Query(db, &dest)
handleError(err)
And that's it.
The dest variable now contains a list of all actors (each with a list of
films they acted in). Each film includes information about its language and
a list of categories it belongs to. This list is filtered to include only
films longer than 180 minutes, where the film language is 'English',
the film category is not 'Action' and 'Trailers' are one of the film's special features.
[!Tip]
It is recommended to enable Strict Scan on application startup, especially when destination contains custom model types. For more details, see the wiki.
Lets print dest as a JSON to see:
jsonText, _ := json.MarshalIndent(dest, "", "\t")
fmt.Println(string(jsonText))
…
What if, we also want to have a list of films per category and actors per category, with the same search conditions.
In that case we can reuse above statement stmt, and just change our destination:
var dest2 []struct {
model.Category
Films []model.Film
Actors []model.Actor
}
err = stmt.Query(db, &dest2)
handleError(err)
Click to see `dest2` json…
Complete code example can be found at ./examples/quick-start/quick-start.go
This example represent probably the most common use case. Detail info about additional statements, features and use cases can be found at project Wiki page.
What are the benefits of writing SQL in Go using Jet?
The biggest benefit is speed. Speed is being improved in 3 major areas:
Writing SQL queries becomes faster and more efficient, as developers benefit from SQL code completion and type safety directly within Go code. Automatic scanning to arbitrary structures eliminates much of the headache and boilerplate required to structure database query results, reducing both complexity and development time.
Speed of ExecutionWhile ORM libraries can introduce significant performance penalties when
multiple tables are involved, due to multiple round-trips to the database
(i.e., the N+1 query problem), Jet will always perform better. Developers
can write queries of any complexity and retrieve results with a single database call.
As a result, handler time lost to latency between the server and database
remains constant. Handler execution ti
Support stdlib UUIDs
Please add a PGX adapter
MySQL: stringQuote doesn't escape backslashes — FixedLiteral values can break out of string literals (injection)
Generate table indexes
Postgres: Returning Old & New Values