Software applications face the need for interacting with databases all the time — It could be for storing new data or accessing the data in it. There are a lot of databases in existence each having their own advantages and disadvantages and their ways of interacting with other languages and applications.

For example, Structured Query language is the sort of an umbrella standard for all the relational database systems. While SQL is highly powerful, it also has a learning curve and for software developers who might not be thoroughly familiar with it. ORM finds its way right there. 

What is ORM?

Object-relational mapping is the idea that instead of everyone learning SQL and further refining their skills to a particular flavour of SQL based databases like MySQL or PostgreSQL, it is better to write in a language that they like and the SQL statements for what they want is automatically generated. They would allow programmers to work with objects as they generally would without having to craft SQL statements.

However, with the advantage of letting developing use the language of their will, ORM has certain disadvantages too. For one, ORMs take away some power of SQL. Also, ORM systems might be initially hard to set up. But after completing the initial setup, it is easy to use and is quite dependable up to certain operations.

ORM library for GO – GORM:

Golang development is an exciting language. Golang has its full-featured library for ORM – GORM that has a host of features that cater to various use cases with Databases.

GORM supports features of Associations, Hooks, Eager loading with Preload & Joins, General & Nested Transactions, Save Point, composite primary key/Indexes/Constraints, SQL Builder and many more.

Let us now see a brief tutorial as to how to use it.

Getting started with GORM:

As you would do with anything else, first you have to install the package and import your packages in the go file you want to use GORM with.

To install, use the command:

go get -u gorm.io/gorm

With Gorm installed, let us see two basic examples of using it. We will use SQLite in the following examples.

Connecting to the Database:

There are many ways of connecting to a database with a lot of initial customization options. A simple way is to use an already defined connection object and connect it to the database.

Here is the example code:

package main

import (

“gorm.io/gorm”

“gorm.io/driver/sqlite”

)

func main() {

Db , err := gorm.Open(sqlite.Open(“MyDatabase.db”),&gorm.Config{})

if err != nil {

  panic(“Connection failed”)

}

}

CRUD Operations:

CRUD – Create, Read, Update and Delete are the basic operations you would do in a database. Let us see the specific code snippets that show how each could be done using gorm.

For creating, use the Create method on the database connection object and give the required object to create. For reading, you could use the First method and identify the rows you want to read through primary keys or specific values of fields.

To update, first, you need to get the model object through Model function and then, invoke the update method on it. You can update either one or many fields.

Deleting is similar to the read method, you just call the delete method and pass the required parameters for identification of the table and the row.

All of this barely scratches the surface of GORM and there are lots to do with it!