package
0.16.1
Repository: https://github.com/ssh2go/atlas-app-toolkit.git
Documentation: pkg.go.dev

# README

GORM

This package contains a set of utilities for famous GORM library. If used together they can significantly help you to enable persistency in your application.

Collection Operators

The package provides some helpers which are able to apply collection operators defined in query package to a GORM query.

Applying query.Filtering

...
// given that Person is a protobuf message and PersonORM is a corresponding GORM model
db, assoc, err = gorm.ApplyFiltering(ctx, db, filtering, &PersonORM{}, &Person{})
if err != nil {
    ...
}
// Join required associations(for nested field support)
db, err = gorm.JoinAssociations(ctx, db, assoc, &PersonORM{})
if err != nil {
    ...
}
var people []Person
db.Find(&people)
...

Applying query.Sorting

...
db, assoc, err = gorm.ApplySorting(ctx, db, sorting, &PersonORM{}, &Person{})
if err != nil {
    ...
}
// Join required associations(for nested field support)
db, err = gorm.JoinAssociations(ctx, db, assoc, &PersonORM{})
if err != nil {
    ...
}
var people []Person
db.Find(&people)
...

Applying query.Pagination

...
db = gorm.ApplyPagination(ctx, db, pagination, &PersonORM{}, &Person{})
if err != nil {
    ...
}
var people []Person
db.Find(&people)
...

Applying query.FieldSelection

...
db, err = gorm.ApplyFieldSelection(ctx, db, fields, &PersonORM{}, &Person{})
if err != nil {
    ...
}
var people []Person
db.Find(&people)
...

Applying everything

...
db, err = gorm.ApplyCollectionOperators(ctx, db, &PersonORM{}, &Person{}, filtering, sorting, pagination, fields)
if err != nil {
    ...
}
var people []Person
db.Find(&people)
...

Transaction Management

We provide transaction management by offering gorm.Transaction wrapper and gorm.UnaryServerInterceptor. The gorm.Transaction works as a singleton to prevent an application of creating more than one transaction instance per incoming request. The gorm.UnaryServerInterceptor performs management on transactions. Interceptor creates new transaction on each incoming request and commits it if request finishes without error, otherwise transaction is aborted. The created transaction is stored in context.Context and passed to the request handler as usual.

NOTE Client is responsible to call txn.Begin() to open transaction.

// add gorm interceptor to the chain
  server := grpc.NewServer(
    grpc.UnaryInterceptor(
      grpc_middleware.ChainUnaryServer( // middleware chain
        ...
        gorm.UnaryServerInterceptor(), // transaction management
        ...
      ),
    ),
  )
import (
	"github.com/infobloxopen/atlas-app-toolkit/gorm"
)

func (s *MyService) MyMethod(ctx context.Context, req *MyMethodRequest) (*MyMethodResponse, error) {
	// extract gorm transaction from context
	txn, ok := gorm.FromContext(ctx)
	if !ok {
		return panic("transaction is not opened") // don't panic in production!
	}
	// start transaction
	gormDB := txn.Begin()
	if err := gormDB.Error; err != nil {
		return nil, err
	}
	// do stuff with *gorm.DB
	return &MyMethodResponse{...}, nil
}

Migration version validation

The toolkit does not require any specific method for database provisioning and setup. However, if golang-migrate or the infobloxopen fork of it is used, a couple helper functions are provided here for verifying that the database version matches a required version without having to import the entire migration package.

# Packages

No description provided by the author

# Functions

ApplyCollectionOperators applies collection operators to gorm instance db.
ApplyFieldSelection applies field selection operator fs to gorm instance db.
ApplyFiltering applies filtering operator f to gorm instance db.
ApplyPagination applies pagination operator p to gorm instance db.
ApplySorting applies sorting operator s to gorm instance db.
No description provided by the author
FieldSelectionStringToGorm is a shortcut to parse a string into FieldSelection struct and receive a list of associations to preload.
FieldSelectionToGorm receives FieldSelection struct and returns a list of associations to preload.
FilteringToGorm returns GORM Plain SQL representation of the filtering expression.
FilterStringToGorm is a shortcut to parse a filter string using default FilteringParser implementation and call FilteringToGorm on the returned filtering expression.
FromContext returns the *Transaction value stored in ctx, if any.
HandleFieldPath converts fieldPath to appropriate db string for use in where/order by clauses according to obj GORM model.
JoinAssociations joins obj's associations from assoc to the current gorm query.
JoinInfo extracts the following information for assoc association of obj: - association table name - source join keys - target join keys.
LogicalOperatorToGorm returns GORM Plain SQL representation of the logical operator.
MaxVersionFrom returns a MigrationVersionValidator with a target based on the highest numbered migration file detected in the given directory.
MergeWithMask will take the fields of `source` that are included as paths in `mask` and write them to the corresponding fields of `dest`.
NewContext returns a new Context that carries value txn.
No description provided by the author
NullConditionToGorm returns GORM Plain SQL representation of the null condition.
No description provided by the author
NumberConditionToGorm returns GORM Plain SQL representation of the number condition.
No description provided by the author
StringConditionToGorm returns GORM Plain SQL representation of the string condition.
UnaryServerInterceptor returns grpc.UnaryServerInterceptor that manages a `*Transaction` instance.
VerifyMigrationVersion checks the schema_migrations table of the db passed against the ValidVersion function of the given validator, returning an error for an invalid version or a dirty database.
VersionExactly returns a MigrationVersionValidator with a specific target version.
VersionRange returns a MigrationVersionValidator with a given lower and upper bound.

# Variables

No description provided by the author
No description provided by the author

# Structs

No description provided by the author
Transaction serves as a wrapper around `*gorm.DB` instance.

# Interfaces

MigrationVersionValidator has a function for checking the database version.