Skip to content
/ yowrap Public

Lightweight Go package designed to effortlessly wrap the Spanner Ecosystem Yo package for enhanced functionality

License

Notifications You must be signed in to change notification settings

jferrl/yowrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌮 yowrap 🌮

GoDoc Test Status codecov Go Report Card

Lightweight Go package designed to effortlessly wrap the Yo package for enhanced functionality.

Context

Yo is a command-line tool that generates Go code from SQL DDL files. The generated code is used to interact with Google Cloud Spanner databases, representing the table and a set of methods to interact with the table.

Yo is a great tool to generate the boilerplate code needed to interact with a Google Cloud Spanner database. However, the generated code is not intended to be modified by the user, as it is overwritten every time the yo tool is executed. This pkg is designed to extend the generated code functionality creating a more capable model.

This package is designed to be used in conjunction with the yo tool, not as a replacement.

See Yo for more information about the yo tool.

Features

  • Commit transaction: This feature allows the client to commit a model transaction directly from the model instance without using the spanner.Mutation outside the model. Now, the model has the responsibility of writing the changes to the database.
  • Custom model hooks: This feature allows the client to define custom hooks that will be executed before or after a specific method is called. This is useful for adding custom logic to the model methods.
  • Inheric model methods: All the method signatures from the generated code are inherited by the model. This allows the client to call the generated methods directly from the model instance.

Usage

Use yowrap as a wrapper for the generated model but committing the transaction outside the model. This mantains the ability to use the generated methods-

package main

import (
    "context"
    "fmt"
    "log"

    "cloud.google.com/go/spanner"
    "github.com/jferrl/yowrap"
)


func main() {
    spannerClient, err := spanner.NewClient(ctx, "projects/my-project/instances/my-instance/databases/my-database")
    if err != nil {
        log.Fatalf("Failed to create a Spanner client: %v", err)
    }
    defer spannerClient.Close()

    // Go struct generated by the yo tool
    user := &User{
        ID:    1,
        Name:  "John Doe",
        Email: " [email protected]",
    }

    // Create a new user model
    user := yowrap.NewModel(user)

    mut := user.Insert(ctx)

    _, err = spannerClient.Apply(ctx, []*spanner.Mutation{mut})
    if err != nil {
        log.Fatalf("Failed to apply the mutation: %v", err)
    }
}

Use yowrap as a wrapper for the generated model and commit the transaction directly from the model.

package main

import (
    "context"
    "fmt"
    "log"

    "cloud.google.com/go/spanner"
    "github.com/jferrl/yowrap"
)

func main() {
    spannerClient, err := spanner.NewClient(ctx, "projects/my-project/instances/my-instance/databases/my-database")
    if err != nil {
        log.Fatalf("Failed to create a Spanner client: %v", err)
    }
    defer spannerClient.Close()

    // Go struct generated by the yo tool
    user := &User{
        ID:    1,
        Name:  "John Doe",
        Email: " [email protected]",
    }

    // Create a new user model
    user := yowrap.NewModel(user, yowrap.WithSpannerClientOption(spannerClient))

    // Commit the transaction
    _, err = user.Apply(ctx, yowrap.Insert)
    if err != nil {
        log.Fatalf("Failed to insert and commit the user: %v", err)
    }
}

Use yowrap with a custom hook.

package main

import (
    "context"
    "fmt"
    "log"

    "cloud.google.com/go/spanner"
    "github.com/jferrl/yowrap"
)

func main() {
    spannerClient, err := spanner.NewClient(ctx, "projects/my-project/instances/my-instance/databases/my-database")
    if err != nil {
        log.Fatalf("Failed to create a Spanner client: %v", err)
    }
    defer spannerClient.Close()

    // Go struct generated by the yo tool
    user := &User{
        ID:    1,
        Name:  "John Doe",
        Email: " [email protected]",
    }

    // Create a new user model
    user := yowrap.NewModel(user, yowrap.WithSpannerClientOption(spannerClient))

    // Add a custom hook
    user.On(yowrap.BeforeInsert, func(_ context.Context, m *yowrap.Model[*user.User], _ *spanner.ReadWriteTransaction) error {
        log.Println("Before insert")
        return nil
    })

    // Commit the transaction
    _, err = user.Apply(ctx, yowrap.Insert)
    if err != nil {
        log.Fatalf("Failed to insert and commit the user: %v", err)
    }
}

License

Copyright 2024, jferrl

yowrap is released under the MIT license.

About

Lightweight Go package designed to effortlessly wrap the Spanner Ecosystem Yo package for enhanced functionality

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages