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  Improve Responsiveness With Session Affinity On Cloud Run

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  Rethinking Your VM Strategy With Spot VMs

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
  • Technology

Apple supercharges its tools and technologies for developers to foster creativity, innovation, and design

  • June 9, 2025
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

Stay Connected!
LATEST
  • 1
    Pure Accelerate 2025: All the news and updates live from Las Vegas
    • June 18, 2025
  • 2
    ‘This was a very purposeful strategy’: Pure Storage unveils Enterprise Data Cloud in bid to unify data storage, management
    • June 18, 2025
  • What is cloud bursting?
    • June 18, 2025
  • 4
    There’s a ‘cloud reset’ underway, and VMware Cloud Foundation 9.0 is a chance for Broadcom to pounce on it
    • June 17, 2025
  • What is confidential computing?
    • June 17, 2025
  • Oracle adds xAI Grok models to OCI
    • June 17, 2025
  • Fine-tune your storage-as-a-service approach
    • June 16, 2025
  • 8
    Advanced audio dialog and generation with Gemini 2.5
    • June 15, 2025
  • 9
    A Father’s Day Gift for Every Pop and Papa
    • June 13, 2025
  • 10
    Global cloud spending might be booming, but AWS is trailing Microsoft and Google
    • June 13, 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
  • Google Cloud, Cloudflare struck by widespread outages
    • June 12, 2025
  • What is PC as a service (PCaaS)?
    • June 12, 2025
  • 3
    Crayon targets mid-market gains with expanded Google Cloud partnership
    • June 10, 2025
  • By the numbers: Use AI to fill the IT skills gap
    • June 11, 2025
  • 5
    Apple services deliver powerful features and intelligent updates to users this autumn
    • June 11, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.