GoLang ORM – GORM

Work Flow Kubernetes Client

In this blogs, I am creating a Kubernetes Client using GoLang to add custom feature which is not present in Kubernetes Vanilla Installations or Kubernetes Flavours such as EKS or AKS. To achieve end goal it is required to interact with database using Go. In this Blog I’ll showcase that we can use the Go-ORM or GORM to interact with a sqlite3/Postgres database in a simple manner.

ORM’s or Object Relationship Managers act almost as brokers between us developers and our underlying database technology. They allow us to essentially work with object’s, much as we normally would and then save these objects without having to craft complex SQL statements.

They effectively reduce the complexity of our codebase in scenarios where you don’t wish to work with SQL but need a database.

Installation

If you are using VS code as editor, you need to run below command:-
go mod tidy
If you are using CLI, to install the jinzhu/gorm, you will have to perform the following go get command:
go get -u github.com/jinzhu/gorm
After you’ve done this, you should be able to import the jinzhu/gorm into any of your go based projects.

As you can see, I wanted to write a go REST API that saved Object details to a database when a certain API endpoint was hit.

We first thing we need to describe our object in a go struct like so:-

type Ingress struct {
gorm.Model
ID string gorm:"primary_key" json:"id,omitempty"
Name string json:"name,omitempty"
..
..
..
}

–Ingress.go

Once we have defined our Ingress model we can then go about exposing an API endpoint that could save new Ingress to our sqlite3/Postgres database.

Define API

So, we are going to create API which features CRUD endpoints. These will return all Ingress, add a new Ingress, delete Ingress and update Ingress. Therefore I have defined the definition of API to be invoked in server.go and using handlers.go to function the requests.

--server.go and handlers.go

Now I have create store.go as database interface between the Ingress API calls and database. This will help on using other objects which would the require the database interface…

With the help of our new GORM, the creation of these endpoints should be far simpler than they would have been, should we have went down a standard raw SQL route.

We can then start this new API by running go run main.go . This API represents the base from which we will build our ORM based solution.

The next step of our project is creating a database. For the purpose, we are going to use a sqlite3 database due to its ease of use and setup. I am using sqllite for my dev environment but for higher environments using Postgres. You can swap to using another database technology fairly easily using the GORM by switching dialects.

Within our allIngress() function we basically want to query for all the Ingress records within our database and then encode this into JSON and return this as the response. We can query all of the ingress within our database by calling db.Find(&ingresses).

Updating our New Ingress Endpoint

We now want to update our newIngress() function so that it can insert new ingress into our database. This will need to parse data from the query parameters of the request made to our API.

This will have to parse the path params of our endpoint and then use these path params to populate a new Ingress object that we will then insert into our sqlite database by calling db.Create(&Ingress{..: .., ..: .., so on})

Our Delete Ingress Endpoint

Our deleteIngress() function will delete a ingress that matches the same name passed into it via a path parameter. It’s rather basic and doesn’t handle the cases where more than one ingress exists within the database with the same but it serves a good example in this project db.Ingress(&..).

Full Source Code

If you want the full source code for this project then please check out this gist:

https://gist.github.com/mkjmkumar/e2413a48cc9d7b33ec75b444e891d422