On this post I will add my notes related to the handling of SQL Queries in GoLang. Especially complex queries. Its work in progress so I will keep adding my notes here.
A bit of background: Recently I wrote few straightforward micro-services e.g. user management, notifications, comments, etc. That actually requires to interact with the Postgres database for CRUD operations.
So far its good, but when you really dive into the code then you see raw sql queries. It looks okay for time being , but as soon the requirements extends it looks like not fair for a developer to write a bit ugly and repeated code e.g.
- Query to get a user by Id
- Query to get a user by email
- Query to get a user by zip-code
- Query to get users by created date
- Query to get users by password expire date
To solve the above ,
The first step is to look around GitHub and see if you can find any ORM similar to Gorrila Mux in routing. If you have a bit python Django or Java background then temptation goes towards a similar like Django or java based.
I have already done the first step and many more, but still not convinced what has been offered so far from Github.
Many repos offers SQL Query Builder related stuff, that I am still analysing. As I am working on a serious project, so possibly I will write my own sql query builder, and mapping results from query to models.
In between found this post to read:
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. It is not an ORM.