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:
From our partners:
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.
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: "alice@example.com"})
// Read the user back out
var user User
if err := db.First(&user, "email = ?", "alice@example.com").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.
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