Instagram
youtube
Facebook
  • 2 years, 2 months ago
  • 5157 Views

ORM Tutorial with Examples in Golang using GORM

Yaman Birla
Table of Contents

Gorm is the most popular Database library for Go. it fully featured an object-relational mapping library for Golang Gorm and could be a developer-friendly tool for changing knowledge between incompatible kinds of systems. The specialty of GORM is to provide SQL builders, RAW SQL, auto migration tools, and extendable plugins for customization. And all of these features came with tests so it would be helpful for the developers for their work.

So what exactly is ORM?

ORM stands for Object-Relational-Mapper and it makes easier to write code. Consider the following scenario: we have a database on which we need to perform CRUD operations. CRUD stands for Create, Retrieve, Update, and Delete in this context. Since Go emphasizes simplicity, it seems sensible that we would use a database's native language, such as SQL, PostGRE, etc, to interface with it. These ORMs assist us in writing quick SQL queries in the language we are most familiar with, as well as developing a SQL wrapper for the language. 

 

Installing GORM

In your command line interface (for windows) run the following command:

go get gorm.io/gorm

 

And on your text editor import it as:

import (

  "gorm.io/gorm"

  "gorm.io/driver/sqlite"

)

 

Importing G-ORM Drivers

Firstly select a suitable code editor in your system, I prefer VScode and create a repository for your golang codes. Now, create a new file named main.go in your repository and import that in like:


package main

 

import (

    "gorm.io/driver/sqlite"

    "gorm.io/gorm"

)

 


Before proceeding further, make sure to make a database like SQLite and create a file accordingly,

Now, make a struct which is a user defined data structure so that we can input stuff inside in that format:


type Product struct {

    gorm.Model

    Code  string

    Price uint

}

Now, moving on to creating a database schema inside the main function:


func main() {

    db, err := gorm.Open(sqlite.Open("test_file.db"), &gorm.Config{})

    if err != nil {

        panic("failed to connect database")

    }

 

    db.AutoMigrate(&Product{})

 

    // Create

    db.Create(&Product{Code: "P1", Price: 100})

 


Now, we create a product P0 for our database.

Now, Golang CRUD Operation

CRUD Operations includes - (Read, Update, Delete).

So, Now we cn read the product from our database by using id or the attributes i.e. product code:


var product Product
db.First(&product, 0)
db.First(&product, "code = ?", "P0")

Now, that our product P0 has been created we can apply the update or delete operations accordingly.

db.Model(&product).Update("Price", 1200) //updating the price of product

db.Delete(&product, 1) //deletion of a product

 

We're still utilising SQL queries, but through a wrapper package that's easier to use for those who aren't as familiar with database languages.

However, there are many better things to come 

 

Add a comment: