# 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

Deprecated: use ApplyCollectionOperatorsEx instead ApplyCollectionOperators applies collection operators to gorm instance db.
No description provided by the author
Deprecated: use ApplyFieldSelectionEx instead ApplyFieldSelection applies field selection operator fs to gorm instance db.
ApplyFieldSelectionEx applies field selection operator fs to gorm instance db.
Deprecated: use ApplyFilteringEx instead ApplyFiltering applies filtering operator f to gorm instance db.
ApplyFiltering applies filtering operator f to gorm instance db.
ApplyPagination applies pagination operator p to gorm instance db.
ApplyPaginationEx applies pagination operator p to gorm instance db.
Deprecated: use ApplySortingEx instead ApplySorting applies sorting operator s to gorm instance db.
ApplySorting applies sorting operator s to gorm instance db.
BeginFromContext will extract transaction wrapper from context and start new transaction.
BeginWithOptionsFromContext will extract transaction wrapper from context and start new transaction, options can be specified to control isolation level for transaction.
FieldSelectionStringToGorm is a shortcut to parse a string into FieldSelection struct and receive a list of associations to preload.
Deprecated: Use FilteringToGormEx instead FilteringToGorm returns GORM Plain SQL representation of the filtering expression.
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.
HandleJSONFiledPath translate field path to JSONB path for postgres jsonb.
TODO: add supprt for embeded objects.
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.
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.
NewDefaultPbToOrmConverter creates default converter for all collection operators.
No description provided by the author
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

DefaultFieldSelectionConverter performs default convertion for FieldSelection collection operator.
DefaultFilteringConditionConverter performs default convertion for Filter collection operator.
DefaultFilteringConditionProcessor processes filter operator conversion.
DefaultPaginationConverter performs default convertion for Paging collection operator.
DefaultPbToOrmConverter performs default convertion for all collection operators.
DefaultSortingCriteriaConverter performs default convertion for Sorting collection operator.
No description provided by the author
Transaction serves as a wrapper around `*gorm.DB` instance.

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
MigrationVersionValidator has a function for checking the database version.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author