storage

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2021 License: MIT Imports: 3 Imported by: 8

README

ao-concepts storage module

CI codecov

This module provides some useful helpers for efficient work with gorm.io/gorm.

Information

If you are interested in contributing to this project, feel free to open a issue to discus a new feature, enhancement or improvement. If you found a bug or security vulnerability in this package, please start a issue, or open a PR against master.

Installation

go get -u github.com/ao-concepts/storage

Storage

The storage Controller is the storage provider for your application. It is used to create transactions that access yout storage system. The log parameter is optional. You cann pass a custom logger there.

You can use the Controller to start transactions. There is also a UseTransaction function that can be used to wrap a function that should be executed within a transaction. The transaction will be rolled back if the error returned is not nil.

dialector := sqlite.Open(":memory:")

c := storage.New(dialector, nil, nil)

Repositories

The Repository struct is intended to be embedded into a custom repository struct:

import (
   "github.com/ao-concepts/storage"
   "gorm.io/gorm"
)

type User struct {
   gorm.Model
   Name string
}

type UserRepository struct {
   storage.Repository
}

func (r *UserRepository) GetAll(tx *storage.Transaction) (users []User, err error) {
   return tx.Gorm().Find(&users).Error
}

Used packages

This project uses some really great packages. Please make sure to check them out!

Package Usage
github.com/stretchr/testify Testing
gorm.io/gorm Database access

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	MaxOpenConnections     int
	MaxIdleConnections     int
	SkipDefaultTransaction bool
}

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

Controller of storage

func New

func New(dialector gorm.Dialector, config *Config, log Logger) (c *Controller, err error)

New storage controller consructor. Pass nil to `log` to use the default logger of gorm. default settings:

  • SetMaxIdleConns: 0 = unlimited
  • SetMaxOpenConns: 0 = unlimited

func (*Controller) Begin

func (c *Controller) Begin() (tx *Transaction, err error)

Begin a transaction

func (*Controller) Gorm

func (c *Controller) Gorm() *gorm.DB

Gorm returns the used gorm handler.

func (*Controller) UseTransaction

func (c *Controller) UseTransaction(fn HandlerFunc) (err error)

UseTransaction executes a HandlerFunc within a storage transaction

type HandlerFunc

type HandlerFunc func(tx *Transaction) (err error)

HandlerFunc is to be handled by a storage transaction

type Logger

type Logger interface {
	CreateGormLogger() logger.Interface
}

Logger interface

type Repository

type Repository struct{}

Repository embeddable struct for database repositories (embedd this in your repository struct)

func (*Repository) Delete

func (m *Repository) Delete(tx *Transaction, entry interface{}) (err error)

Delete some existing data entry

func (*Repository) Insert

func (m *Repository) Insert(tx *Transaction, entry interface{}) (err error)

Insert data entry to database

func (*Repository) Remove

func (m *Repository) Remove(tx *Transaction, entry interface{}) (err error)

Remove (completely) some existing data entry

func (*Repository) Update

func (m *Repository) Update(tx *Transaction, entry interface{}) (err error)

Update some existing data entry

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

Transaction for a storage action

func (*Transaction) Commit

func (tx *Transaction) Commit() (err error)

Commit a transaction

func (*Transaction) Gorm

func (tx *Transaction) Gorm() *gorm.DB

Gorm returns the gorm transaction

func (*Transaction) Rollback

func (tx *Transaction) Rollback() (err error)

Rollback a transaction

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL