Blog
In this post, we will look at listing users with all their addresses. Both Golang Database Library Orm Example - Many to Many
January 2, 2022
users
and addresses
tables are joined with a pivot table, user_addresses
, which holds the foreign key of each of
users
and address
table primary key.
Golang Database Library Orm Example Benchmark
December 28, 2021
Golang Database Library Orm Example Conclusion
December 28, 2021
In this post, we will look at getting a list of countries along with its addresses. There are three ways of loading this kind of relationship:Golang Database Library Orm Example - One to Many
December 15, 2021
Read more >
countries
to addresses
table,array agg
(for postgres) function or group concat
for mysql/mariadb.
In this post, we will look at how deleting a single record in a table works for these various libraries and ORMs. In general, this operation is very simple because we only care about deleting and checking for an error. In short, we want to achieveGolang Database Library Orm Example - Delete
December 15, 2021
DELETE FROM users where id=$1;
In this post we will see a simple update of a resource by using its ID. We will try to perform an
We only show how to do a full As a good practice, we scan user’s request to a custom Golang Database Library Orm Example - Update
December 15, 2021
UPDATE <TABLE> SET <COLUMN1>=<VALUE1>
etc, if not, we simply retrieve the item first, set to new
values, and then save the record.PUT
updating instead of a PATCH
, where a patch means we only
update certain fields only, leaving other columns untouched.UserUpdateRequest
struct. Then we hand
over this struct to our database access layer.
In this post, we will look at how these libraries and ORM deal with fetching a single record given
an ID. There should not be any drama as we only be doing a simple query as follows:Golang Database Library Orm Example - Get
December 14, 2021
SELECT * FROM users WHERE id = $1;
In this post, we will look at comparing listing of user resource. We limit the record to the first
thirty records. Basically in all of them we want to make the following queryGolang Database Library Orm Example - List
December 14, 2021
SELECT * FROM users ORDER BY id LIMIT 30 OFFSET 0;
In this post, we compare and contrast how these libraries and ORMs handle a record insertion. As a standard approach to all example in these blog series, our controller accepts and parses
client request to a custom ‘request’ struct, and if required, parses query parameter(s). We hash the
password and together with All CRUD operations are done in You should perform validation in production. In our case, we skip this part to keep this blog series
focused.Golang Database Library Orm Example - Create
December 14, 2021
CreateUserRequest
we try to insert to database. Then the struct
is passed down to our data access layer.type CreateUserRequest struct {
ID uint `json:"id"`
FirstName string `json:"first_name"`
MiddleName string `json:"middle_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Password string `json:"password"`
}
crud.go
files of each of the libraries and ORMs directories.
There are a multitude of ways to interact with a SQL database in Go. The most obvious path is to
simply use This post is only an introduction to these packages. I will briefly touch on each of them on what
it is and database operations that we are going to test with. In the next posts, we will cover
common use cases for each. Full source code is available at https://github.com/gmhafiz/golang-database-library-orm-example.Golang Database Library and ORM Example - Introduction
December 14, 2021
database/sql
package from the standard library plus a database driver. It is easy to use
and can be sufficient to meet all of your requirements. Using raw SQL directly means you can
leverage what you already know, SQL, and craft complex queries in many ways that an ORM may not
support. However, retrieving (scanning in Go lingo) results from database is verbose and can be
tedious - something that alternatives shine. This series of posts will show how different popular
libraries and ORM available of Go are used. They are sqlx, sqlc,
squirrel, gorm,
sqlboiler, and ent.