Jet

SQL made easy for Go developers

GoJet
3 min readJul 23, 2019

Intro

With every new web application(with a SQL database), there is a decision to be made. Whether to use ORM or to write raw SQL.

If the developers decide for some kind of ORM solution, they will experience code is easier to update, maintain and reuse, without bothering too much with SQL. But after some time as the code base and a number of tables grows, handlers will start becoming slower and slower. This gets into the n-query problems with ORMs. Some ORMs allow performance tune object queries, but still the overall performance will be always slower than, if the queries were written in raw SQL.

If the developers decide for a raw SQL then they are most likely valuing performance rather than maintainability. Raw SQL is plain text, and does not have type safety and compile time check. So every time database changes developer has to manually adapt affected SQL queries, and hope that if something is missed, tests will catch the error.

Weather to use ORM or write raw SQL is in some regard balancing between maintainability and performance.

There is also a third option, not so uncommon — SQL Builder.

SQL Builder

There are all sorts of SQL builders, for most of the languages(not as much for Go). SQL Builder does not hide SQL behind API like ORM, but allows developers to write SQL queries, directly in the language of choosing. SQL Builder files can be auto-generated from database schema model, so if database is changed, there is no manually repetitive work involved.

SQL Builder automates a lot of things, except one. With SQL Builder it is possible to join multiple of tables with one query, and return result.
If number of joined tables is more than 2, and join between tables is not a simple 1–1, it is very hard to unpack the result into meaningful structure.

This was motivation for Jet.

Jet

Jet is a framework for writing type-safe SQL queries in Go, with ability to easily convert database query result into desired arbitrary object structure.

Bellow is piece of Go code that shows how easily is to join couple of tables with a couple of conditions, and store result into desired destination.

Note couple of things:
- all the types used in example are generated from database schema(by Jet generator) or they are part of Jet library.
- Go SQL code has to be syntax and type correct in SQL terms to compile.
- developers have freedom to construct desired structure from auto-generated model files.
- there is no limitation of how big or nested destination structure can be.

Above example is explained into more details at project readme page — https://github.com/go-jet/jet.
Detail info about additional features and use cases can be found at project wiki page — https://github.com/go-jet/jet/wiki

Release 2.0.0 update:

Jet currently supports PostgreSQL, MySQL and MariaDB. Future releases will add support for additional databases.

--

--