entutils

package
v1.0.0-beta.182 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetOrdering

func GetOrdering(order sortx.Order) []sql.OrderTermOption

func JSONBIn

func JSONBIn(field string, key string, values []string) func(*sql.Selector)

JSONBIn returns a function that filters the given JSONB field by the given key and value Caveats: - PostgreSQL only - The field must be a JSONB field - The value must be a string (no support for other types, ->> converts all values to string) - This might not work if there's a join involved in the query, so add unit tests

func NewTxContext

func NewTxContext(ctx context.Context, tx *TxDriver) context.Context

func RunInTransaction

func RunInTransaction[R any](txCtx context.Context, txDriver *TxDriver, cb func(ctx context.Context, tx *TxDriver) (*R, error)) (*R, error)

func StartAndRunTx

func StartAndRunTx[R any](ctx context.Context, src TxCreator, cb func(ctx context.Context, tx *TxDriver) (*R, error)) (*R, error)

Types

type IDMixin

type IDMixin struct {
	mixin.Schema
}

IDMixin adds the ID field to the schema

func (IDMixin) Fields

func (IDMixin) Fields() []ent.Field

Fields of the IDMixin.

func (IDMixin) Indexes

func (IDMixin) Indexes() []ent.Index

type MetadataAnnotationsMixin

type MetadataAnnotationsMixin struct {
	mixin.Schema
}

NamespaceMixin can be used for namespaced entities

func (MetadataAnnotationsMixin) Fields

func (MetadataAnnotationsMixin) Fields() []ent.Field

Fields of the IDMixin.

type NamespaceMixin

type NamespaceMixin struct {
	mixin.Schema
}

NamespaceMixin can be used for namespaced entities

func (NamespaceMixin) Fields

func (NamespaceMixin) Fields() []ent.Field

Fields of the IDMixin.

type RawEntConfig

type RawEntConfig struct {
	// driver used for executing database requests.
	Driver dialect.Driver
	// debug enable a debug logging.
	Debug bool
	// log used for logging on debug mode.
	Log func(...any)
}

type TimeMixin

type TimeMixin struct {
	mixin.Schema
}

TimeMixin adds the created_at and updated_at fields to the schema

func (TimeMixin) Fields

func (TimeMixin) Fields() []ent.Field

Fields of the TimeMixin.

type Transactable

type Transactable interface {
	Commit() error
	Rollback() error
}

type TxCreator

type TxCreator interface {
	// Creates a TxDriver from a hijacked ent transaction (the driver of it).
	// Example:
	//
	// type dbAdapter struct {
	// 	db *db.Client
	// }
	//
	// // we have to implement the TxCreator interface
	// func (d *dbAdapter) Tx(ctx context.Context) (context.Context, *entutils.TxDriver, error) {
	//     // HijackTx gets generated when using expose.tpl
	// 	txCtx, rawConfig, eDriver, err := d.db.HijackTX(ctx, &sql.TxOptions{
	// 		ReadOnly: false,
	// 	})
	//
	// 	if err != nil {
	// 		return nil, nil, fmt.Errorf("failed to hijack transaction: %w", err)
	// 	}
	// 	return txCtx, entutils.NewTxDriver(eDriver, rawConfig), nil
	// }
	Tx(ctx context.Context) (context.Context, *TxDriver, error)
}

Able to start a new transaction

type TxDriver

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

func GetTxDriver

func GetTxDriver(ctx context.Context) (*TxDriver, error)

func NewTxDriver

func NewTxDriver(driver Transactable, cfg *RawEntConfig) *TxDriver

func (*TxDriver) Commit

func (t *TxDriver) Commit() error

func (*TxDriver) GetConfig

func (t *TxDriver) GetConfig() *RawEntConfig

func (*TxDriver) Rollback

func (t *TxDriver) Rollback() error

type TxDriverContextKey

type TxDriverContextKey string

type TxHijacker

type TxHijacker interface {
	HijackTx(ctx context.Context, opts *sql.TxOptions) (context.Context, *RawEntConfig, Transactable, error)
}

type TxUser

type TxUser[T any] interface {
	// Creates a new instance of the adapter using the provided transaction.
	// Example:
	//
	// type dbAdapter struct {
	//     db *db.Client
	// }
	//
	// func (d *dbAdapter) WithTx(ctx context.Context, tx *entutils.TxDriver) SomeDB[db1.Example1] {
	//     // NewTxClientFromRawConfig gets generated when using expose.tpl
	//     txClient := db.NewTxClientFromRawConfig(ctx, *tx.GetConfig())
	//     res := &db1Adapter{db: txClient.Client()}
	//     return res
	// }
	WithTx(ctx context.Context, tx *TxDriver) T
}

Able to use an existing transaction

type ULID

type ULID struct {
	ulid.ULID
}

ULID implements a valuer (and Scanner) that can serialize string ULIDs into postgres instead of the binary representation, as postgres interprets those as UTF-8 strings

func Ptr

func Ptr(u *ulid.ULID) *ULID

func Wrap

func Wrap(u ulid.ULID) ULID

func (*ULID) ULIDPointer

func (v *ULID) ULIDPointer() *ulid.ULID

func (ULID) Value

func (v ULID) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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