goqu_crud_gen

package module
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: Unlicense Imports: 6 Imported by: 0

README

GOQU CRUD Generator

Go

Work in progress!

Generates a basic repository by db model definition.

Expected to be used by goqu and sqlx users.

What is generated?

TODO: list and describe generated structs and methods.

Generator creates a repository definition file containing the main CRUD methods:

  • Create
  • Get
  • Update
  • Delete, DeleteMany

... and some special methods:

  • WithTran currently must be used for wrapping each repo method call.
  • each, iter for user future use in custom repo methods.

Install

$ go get github.com/funvit/goqu-crud-gen/cmd/...

Usage

Define a db-model.

Rules:

  • model must have one field marked as primary key via adding option primary for tag db
    • supported field types:
      • standard (int64, string...)
      • any other which implements Scanner and Valuer interfaces (ex: github.com/google/uuid)
  • if model primary key field value is database-side generated int64 - use auto option for tag db
  • generated file will be placed near model definition

See examples for mode info.

Examples

See ./examples folder.

TODO

  • tests
  • tests with mysql in docker
  • string field maxlen rule by annotation?
  • new flag allowed customising WithTran method name. (-rename-with-tran)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTranInContext = errors.New("no transaction in context")

Functions

func Transaction

func Transaction(ctx context.Context, db *sqlx.DB, ct CtxTransaction, f func(ctx context.Context) error) error

Transaction calls func in transaction. Rollbacks if function return error.

Example:

err := Transaction(ctx, ct, func(ctx context.Context) error {
    m, err := ...
    if err != nil {
        return err
    }

    // other db operation
    return ...
})

Special method for generated repositories.

func TxFromContext

func TxFromContext(ctx context.Context) (*sqlx.Tx, error)

TxFromContext gets started transaction from context.

Types

type CtxTransaction

type CtxTransaction interface {
	TxFromContext(ctx context.Context) (*sqlx.Tx, error)
	NewContextWithTx(ctx context.Context, tx *sqlx.Tx) (context.Context, error)
}

CtxTransaction defines ctx transaction interface which allow transfer *sqlx.Tx via context.Context.

type Option

type Option func(ds *goqu.SelectDataset)

func WithFilter added in v0.10.1

func WithFilter(exp ...exp.Expression) Option

WithFilter option used to filter select.

func WithLimit added in v0.10.1

func WithLimit(u uint) Option

WithLimit option used to limit select.

func WithLockForUpdate

func WithLockForUpdate() Option

WithLockForUpdate option must be used for read methods to lock for update.

func WithOrder added in v0.10.1

func WithOrder(order ...exp.OrderedExpression) Option

WithOrder option used to order select.

type RepositoryOpt

type RepositoryOpt struct {
	CtxTran CtxTransaction
}

type RepositoryOption

type RepositoryOption func(o *RepositoryOpt)

func WithCtxTran

func WithCtxTran(ct CtxTransaction) RepositoryOption

WithCtxTran option used to set custom transaction getter from context.

type StdCtxTran

type StdCtxTran struct {
	DB *sqlx.DB
}

StdCtxTran implements CtxTransaction via sqlx.DB. Used as default for generated repository.

func (*StdCtxTran) NewContextWithTx

func (s *StdCtxTran) NewContextWithTx(ctx context.Context, tx *sqlx.Tx) (context.Context, error)

func (*StdCtxTran) TxFromContext

func (s *StdCtxTran) TxFromContext(ctx context.Context) (*sqlx.Tx, error)

Directories

Path Synopsis
cmd
Package examples contains examples.
Package examples contains examples.

Jump to

Keyboard shortcuts

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