entdb

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package entdb extends the ent db library and satisfies matt's needs for consistency

Index

Constants

View Source
const (
	DefaultCacheTTL = 1 * time.Second
)

Variables

View Source
var (
	// ErrUnsupportedMWDriver is returned when an unsupported multiwrite driver is used
	ErrUnsupportedMWDriver = errors.New("unsupported multiwrite driver")

	// ErrUnsupportedDialect is returned when an unsupported dialect is used
	ErrUnsupportedDialect = errors.New("unsupported dialect")
)

Functions

func CheckDialect added in v0.2.4

func CheckDialect(d string) (string, error)

func CheckMultiwriteSupport added in v0.2.4

func CheckMultiwriteSupport(d string) bool

func Healthcheck

func Healthcheck(client *entsql.Driver) func(ctx context.Context) error

Healthcheck pings the DB to check if the connection is working

func NewTestClient added in v0.2.6

func NewTestClient(ctx context.Context, entOpts []ent.Option) (*ent.Client, *testutils.TC, error)

NewTestClient creates a entdb client that can be used for TEST purposes ONLY

Types

type Config added in v0.2.3

type Config struct {
	// Debug to print debug database logs
	Debug bool `yaml:"debug" split_words:"true" default:"false"`
	// SQL Driver name from dialect.Driver
	DriverName string `yaml:"driverName" split_words:"true" default:"sqlite3"`
	// MultiWrite enabled writing to two databases
	MultiWrite bool `yaml:"multiWrite" split_words:"true" default:"false"`
	// Primary write database source (required)
	PrimaryDBSource string `yaml:"primaryDBSource" split_words:"true" default:"datum.db?mode=memory&_fk=1"`
	// Secondary write database source (optional)
	SecondaryDBSource string `yaml:"secondaryDBSource" split_words:"true" default:"backup.db?mode=memory&_fk=1"`
	// CacheTTL to have results cached for subsequent requests
	CacheTTL time.Duration `yaml:"cacheTTL" split_words:"true" default:"1s"`
}

Config Settings

type EntClientConfig

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

EntClientConfig configures the entsql drivers

func NewDBConfig

func NewDBConfig(c Config, l *zap.SugaredLogger) *EntClientConfig

NewDBConfig returns a new database configuration

func (*EntClientConfig) GetPrimaryDB

func (c *EntClientConfig) GetPrimaryDB() *entsql.Driver

func (*EntClientConfig) GetSecondaryDB

func (c *EntClientConfig) GetSecondaryDB() *entsql.Driver

func (*EntClientConfig) NewMultiDriverDBClient

func (c *EntClientConfig) NewMultiDriverDBClient(ctx context.Context, opts []ent.Option) (*ent.Client, error)

NewMultiDriverDBClient returns a ent client with a primary and secondary, if configured, write database

type MultiWriteDriver

type MultiWriteDriver struct {
	// Wp (write-primary), Ws (write-secondary) Drivers
	Wp, Ws dialect.Driver
}

MultiWriteDriver allows you to write to a primary and secondary database

func (*MultiWriteDriver) BeginTx

func (d *MultiWriteDriver) BeginTx(ctx context.Context, opts *sql.TxOptions) (dialect.Tx, error)

BeginTx adds an log-id for the transaction and calls the underlying driver BeginTx command if it is supported.

func (*MultiWriteDriver) Close

func (d *MultiWriteDriver) Close() error

Close the underlying connections

func (*MultiWriteDriver) Dialect

func (d *MultiWriteDriver) Dialect() string

Dialect returns the dialect name of the primary driver

func (*MultiWriteDriver) Exec

func (d *MultiWriteDriver) Exec(ctx context.Context, query string, args, v any) error

Exec logs its params and calls the underlying driver Exec method for both write drivers

func (*MultiWriteDriver) Query

func (d *MultiWriteDriver) Query(ctx context.Context, query string, args, v any) error

Query will query the primary write database

func (*MultiWriteDriver) Tx

Tx wraps the Exec and Query operations in transaction.

Jump to

Keyboard shortcuts

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