aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Engineering
  • Public Cloud

Golang’s GORM Support For Cloud Spanner Is Now Generally Available

  • aster.cloud
  • November 9, 2023
  • 4 minute read

Introduction

GORM is an Object Relational Mapping (ORM) library for Golang. ORM converts data between incompatible type systems using object-oriented programming languages. An ORM library is a library that implements this technique and provides an object-oriented layer between relational databases and object-oriented programming languages. Do you have an application that you developed with GORM and now you want to migrate to a cloud database? Google Cloud Spanner now supports the GORM Object Relational Mapper. You can reuse existing GORM code with the Cloud Spanner, or write new features while leveraging existing knowledge.

The Cloud Spanner Go ORM implements all the classes and interfaces that are needed to use Cloud Spanner with GORM. Add the spanner GORM module to your project and connect to Cloud Spanner as follows:


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Installing

Simple install the package to your $GOPATH with the go tool from shell:

$ go get -u github.com/googleapis/go-gorm

Connecting

Connect to Cloud Spanner with GORM:

import (
    _ "github.com/googleapis/go-sql-spanner"
    spannergorm "github.com/googleapis/go-gorm-spanner"
)

// Creates a prepared statement when executing any SQL and caches them to speed up future calls
db, err := gorm.Open(spannergorm.New(spannergorm.Config{
    DriverName: "spanner",
    DSN:        "projects/MY-PROJECT/instances/MY-INSTANCE/databases/MY-DATABASE",
}), &gorm.Config{PrepareStmt: true})
if err != nil {
    log.Fatal(err)
}

// Print singers with more than 500 likes.
type Singers struct {
    ID    int `gorm:"primarykey;autoIncrement:false"`
    Name  string
    Likes int
}
var singers []Singers
if err := db.Where("likes > ?", 500).Find(&singers).Error; err != nil {
    log.Fatal(err)
}
for t := range singers {
    fmt.Println(t.ID, t.Name)
}

Note that as GORM implementation uses the Go client to connect to Spanner, it is not necessary to specify a custom endpoint to connect to the Spanner emulator(a local, in-memory emulator, which you can use to develop and test your applications for free without creating a GCP Project or a billing account). The driver will automatically connect to the emulator if the SPANNER_EMULATOR_HOST environment variable has been set.

Read More  Multi-Cloud, Still An Actual Thing After All These Years

Usage

The Cloud Spanner GORM supports standard GORM features, like querying data using both positional and named parameters, managing associations and executing transactions. GORM also supports specific Spanner features such as interleaved tables, mutations, and stale reads by unwrapping the underlying connection. This means that you can use GORM to write code that is both powerful and scalable.

For example, the following code snippet shows a simple write and read operation to Spanner, backed by Spanner’s infinite scalability. If your product takes off, you don’t have to rewrite anything for sharding, etc. Just add more nodes!

package main


import (
   "fmt"
   "log"


   "gorm.io/gorm"
   "gorm.io/gorm/logger"
   _ "github.com/googleapis/go-sql-spanner"
   spannergorm "github.com/googleapis/go-gorm-spanner"
)


func main() {
   // Open db connection.
   db, err := gorm.Open(spannergorm.New(spannergorm.Config{
      DriverName: "spanner",
      DSN:        "projects/my-project/instances/my-instance/databases/my-database",
   }), &gorm.Config{PrepareStmt: true, IgnoreRelationshipsWhenMigrating: true, Logger: logger.Default.LogMode(logger.Error)})
   if err != nil {
      log.Fatalf("failed to open database: %v", err)
   }


   type User struct {
      gorm.Model
      Name  string
      Email string
   }
   if err := db.AutoMigrate(&User{}); err != nil {
      log.Fatalf("failed to migrate: %v", err)
   }


   // Insert a new user
   db.Save(&User{Name: "Alice", Email: "[email protected]"})


   // Read the user back out
   var user User
   if err := db.First(&user, "email = ?", "[email protected]").Error; err != nil {
      log.Fatalf("Failed to find created data, got error: %v", err)
   }


   // Print the user's name
   fmt.Println(user.Name)
}

Interleaved Tables

Using interleaved tables for parent-child relationships in your schema can improve performance. Interleaving a child table with a parent means that the child records will be stored physically together with the parent. These relationships can be modeled and used as any other association in GORM.

Loading…

CREATE SEQUENCE IF NOT EXISTS albums_seq OPTIONS (sequence_kind = "bit_reversed_positive");


CREATE TABLE IF NOT EXISTS `albums` (
   `id` INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence albums_seq)),
   `created_at` TIMESTAMP,
   `updated_at` TIMESTAMP,
   `deleted_at` TIMESTAMP,
   `title` STRING(MAX),
   `marketing_budget` FLOAT64,
   `release_date` date,
   `cover_picture` BYTES(MAX),
   `singer_id` INT64,
   CONSTRAINT `fk_singers_albums` FOREIGN KEY (`singer_id`) REFERENCES `singers`(`id`)
) PRIMARY KEY (`id`);
CREATE SEQUENCE IF NOT EXISTS tracks_seq OPTIONS (sequence_kind = "bit_reversed_positive");


CREATE TABLE IF NOT EXISTS `tracks` (
   `id` INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence tracks_seq)),
   `created_at` TIMESTAMP,
   `updated_at` TIMESTAMP,
   `deleted_at` TIMESTAMP,
   `track_number` INT64,
   `title` STRING(MAX),
   `sample_rate` FLOAT64,
   ) PRIMARY KEY (`id`,`track_number`), INTERLEAVE IN PARENT albums ON DELETE CASCADE;

// Above schema can be mapped to below structs to work with GORM
type Album struct {
   gorm.Model
   Title           string
   MarketingBudget sql.NullFloat64
   ReleaseDate     spanner.NullDate
   CoverPicture    []byte
   SingerId        int64
   Singer          Singer
   Tracks          []Track `gorm:"foreignKey:id"`
}

// Track is interleaved in Album. The ID column is both the first part of the primary key of Track, and a
// reference to the Album that owns the Track.
type Track struct {
   gorm.Model
   TrackNumber int64 `gorm:"primaryKey;autoIncrement:false"`
   Title       string
   SampleRate  float64
   Album       Album `gorm:"foreignKey:id"`
}

Mutations

GORM will use Data Manipulation Language (DML) by default. You can run mutations by unwrapping the Spanner connection:

import (
    spannerdriver "github.com/googleapis/go-sql-spanner"
    spannergorm "github.com/googleapis/go-gorm-spanner"
)

db, err := gorm.Open(spannergorm.New(spannergorm.Config{
    DriverName: "spanner",
    DSN:        "projects/MY-PROJECT/instances/MY-INSTANCE/databases/MY-DATABASE",
}), &gorm.Config{PrepareStmt: true})
if err != nil {
    log.Fatal(err)
}
databaseSQL, _ := db.DB()
conn, _ := databaseSQL.Conn(context.Background())
type singer struct {
   gorm.Model
   FirstName string
   LastName  string
   Active    bool
}
m, err :=  spanner.InsertOrUpdateStruct("Singers", &singer{
   Model: gorm.Model{
     ID:        uint(1+rnd.Int()%(1000000-1)),
     CreatedAt: time.Now(),
     UpdatedAt: time.Now(),
   },
   FirstName: “test”,
   LastName:  “test”,
   Active:    true,
})
if err != nil {
   return err
}
conn.Raw(func(driverConn interface{}) error {
      return driverConn.(spannerdriver.SpannerConn).BufferWrite([]*spanner.Mutation{m})
})

Further samples

The GitHub repository contains a directory with multiple samples for common use cases for working with GORM and/or Cloud Spanner.

Read More  AWS Announces General Availability Of Amazon EC2 DL1 Instances

Limitations

Cloud Spanner features that are not supported in the GORM are listed here.

Getting involved

We’d love to hear from you, especially if you’re a Golang developer considering Cloud Spanner or an existing Cloud Spanner customer who is considering using database/sql for new projects. The project is open-source, and you can comment, report bugs, and open pull requests on Github.

We would like to thank Knut Olav Løite for their work on this project.

See also

Before you get started, you need to have a golang project. For a complete set of examples, you can find them in the examples directory for the driver. You can additionally refer:

  • Cloud Spanner product documentation
  • Getting started with Cloud Spanner in Go
  • Cloud Spanner Golang client library
  • Cloud Spanner Quotas & Limits

By: Rahul Yadav (Software Engineer, Google Cloud)
Originally published at: Google Cloud Blog

Source: cyberpogo.com


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • Cloud Spanner
  • Databases
  • Go
  • Golang
  • Google Cloud
  • GORM
  • Object Relational Mapping
  • ORM
  • Tutorial
You May Also Like
View Post
  • Engineering

Just make it scale: An Aurora DSQL story

  • May 29, 2025
View Post
  • Computing
  • Public Cloud
  • Technology

United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services

  • April 15, 2025
View Post
  • Engineering
  • Technology

Guide: Our top four AI Hypercomputer use cases, reference architectures and tutorials

  • March 9, 2025
View Post
  • Computing
  • Engineering

Why a decades old architecture decision is impeding the power of AI computing

  • February 19, 2025
DeepSeek R1 is now available on Azure AI Foundry and GitHub
View Post
  • Public Cloud
  • Technology

DeepSeek R1 is now available on Azure AI Foundry and GitHub

  • February 2, 2025
View Post
  • Engineering
  • Software Engineering

This Month in Julia World

  • January 17, 2025
View Post
  • Engineering
  • Software Engineering

Google Summer of Code 2025 is here!

  • January 17, 2025
View Post
  • Data
  • Engineering

Hiding in Plain Site: Attackers Sneaking Malware into Images on Websites

  • January 16, 2025

Stay Connected!
LATEST
  • 1
    Just make it scale: An Aurora DSQL story
    • May 29, 2025
  • 2
    Reliance on US tech providers is making IT leaders skittish
    • May 28, 2025
  • Examine the 4 types of edge computing, with examples
    • May 28, 2025
  • AI and private cloud: 2 lessons from Dell Tech World 2025
    • May 28, 2025
  • 5
    TD Synnex named as UK distributor for Cohesity
    • May 28, 2025
  • Weigh these 6 enterprise advantages of storage as a service
    • May 28, 2025
  • 7
    Broadcom’s ‘harsh’ VMware contracts are costing customers up to 1,500% more
    • May 28, 2025
  • 8
    Pulsant targets partner diversity with new IaaS solution
    • May 23, 2025
  • 9
    Growing AI workloads are causing hybrid cloud headaches
    • May 23, 2025
  • Gemma 3n 10
    Announcing Gemma 3n preview: powerful, efficient, mobile-first AI
    • May 22, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • Understand how Windows Server 2025 PAYG licensing works
    • May 20, 2025
  • By the numbers: How upskilling fills the IT skills gap
    • May 21, 2025
  • 3
    Cloud adoption isn’t all it’s cut out to be as enterprises report growing dissatisfaction
    • May 15, 2025
  • 4
    Hybrid cloud is complicated – Red Hat’s new AI assistant wants to solve that
    • May 20, 2025
  • 5
    Google is getting serious on cloud sovereignty
    • May 22, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.