db

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package db provides utilities for interacting with BadgerDB v4.

Package db provides database adapters and utilities for interacting with various database systems.

Index

Constants

This section is empty.

Variables

View Source
var AdapterRegistry = make(map[string]AdapterFactory)

AdapterRegistry is a map that stores factory functions for creating database adapters.

Functions

func RegisterAdapter

func RegisterAdapter(dialectName string, factory AdapterFactory)

RegisterAdapter registers a new database adapter factory for a specific dialect.

Types

type Adapter

type Adapter interface {
	GetOptions() *options.Dialect
	GetDb() *sql.DB
	GetMigration() (*migrate.Migrate, error)
	Close() error
}

Adapter represents an interface for database adapters.

func GetAdapter

func GetAdapter(ctx context.Context, dialectName string) (Adapter, error)

GetAdapter creates a new database adapter based on the provided dialect name.

func NewClickhouseDB

func NewClickhouseDB(ctx context.Context, opts *options.Dialect) (Adapter, error)

NewClickhouseDB creates a new ClickHouse database adapter instance. It initializes a connection to the ClickHouse database using the provided configuration options. The function returns the initialized ClickHouse adapter and any error encountered during initialization.

func NewDialectDB

func NewDialectDB(ctx context.Context, dialectName string) (Adapter, error)

NewDialectDB creates a new database adapter based on the provided dialect name. It now uses the adapter registry to resolve and create the adapter instance.

func NewPostgresDB

func NewPostgresDB(ctx context.Context, opts *options.Dialect) (Adapter, error)

NewPostgresDB creates a new Postgres database adapter instance. It initializes a connection to the Postgresql database using the provided configuration options. The function returns the initialized Postgresql adapter and any error encountered during initialization.

func NewSqliteDB

func NewSqliteDB(ctx context.Context, opts *options.Options) (Adapter, error)

NewSqliteDB ...

type AdapterFactory

type AdapterFactory func(ctx context.Context, opts *options.Dialect) (Adapter, error)

AdapterFactory is a function that creates a new Adapter instance.

type BadgerDB

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

BadgerDB wraps a Badger DB with additional context and configuration.

func NewBadgerDB

func NewBadgerDB(ctx context.Context, opts badger.Options) (*BadgerDB, error)

NewBadgerDB creates a new BadgerDB instance with the provided Options. It defaults to using a background context if no context is provided.

Example usage:

ctx := context.Background()
db, err := NewBadgerDB(ctx, "/tmp/mydb", opts...)

func (*BadgerDB) Close

func (d *BadgerDB) Close() error

Close closes the BadgerDB.

Example usage:

err := db.Close()

func (*BadgerDB) DB

func (d *BadgerDB) DB() *badger.DB

func (*BadgerDB) Exists

func (d *BadgerDB) Exists(key string) (bool, error)

Exists checks if a key exists in the BadgerDB.

Returns a boolean indicating if the key exists and any error encountered. It returns true and nil error if the key exists, false and nil error if the key does not exist. If an error other than ErrKeyNotFound is encountered during the operation, it returns false and the error.

Example usage:

exists, err := db.Exists("myKey")

func (*BadgerDB) GarbageCollect

func (d *BadgerDB) GarbageCollect() error

GarbageCollect runs a value log garbage collection on the BadgerDB, provided the rewrite ratio is more than 0.7 (70%).

Example usage:

err := db.GarbageCollect()

func (*BadgerDB) Get

func (d *BadgerDB) Get(key string) ([]byte, error)

Get retrieves the value for a given key from the BadgerDB.

Example usage:

value, err := db.Get("myKey")

func (*BadgerDB) Write

func (d *BadgerDB) Write(ctx context.Context, key string, value []byte) error

Write sets the value for a given key in the BadgerDB.

Example usage:

err := db.Write("myKey", []byte("myValue"))

type Clickhouse

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

Clickhouse represents a ClickHouse database adapter.

func ToClickhouse

func ToClickhouse(adapter Adapter) *Clickhouse

func (*Clickhouse) Close

func (d *Clickhouse) Close() error

Close closes the connection of the ClickHouse adapter. It returns any error encountered during the connection closure process.

func (*Clickhouse) GetDb

func (d *Clickhouse) GetDb() *sql.DB

GetDb returns the underlying database connection of the ClickHouse adapter.

func (*Clickhouse) GetDriver

func (d *Clickhouse) GetDriver() driver.Conn

GetDriver returns the underlying database native connection of the ClickHouse adapter.

func (*Clickhouse) GetMigration

func (d *Clickhouse) GetMigration() (*migrate.Migrate, error)

GetMigration returns a new migration instance for the ClickHouse database. The migration instance is configured based on the adapter's dialect options. The function returns the migration instance and any error encountered during its creation.

func (*Clickhouse) GetOptions

func (d *Clickhouse) GetOptions() *options.Dialect

GetOptions returns the options of the ClickHouse adapter.

type Db

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

func (*Db) Close

func (d *Db) Close() error

func (*Db) GetDb

func (d *Db) GetDb() *sql.DB

func (*Db) GetMigration

func (d *Db) GetMigration() (*migrate.Migrate, error)

func (*Db) GetOptions

func (d *Db) GetOptions() *options.Dialect

type Dialect

type Dialect string
var (
	ClickhouseDialect Dialect = "clickhouse"
	PostgresDialect   Dialect = "postgres"
)

func (Dialect) String

func (d Dialect) String() string

type Postgres

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

Postgres represents a Postgresql database adapter.

func ToPostgres

func ToPostgres(adapter Adapter) *Postgres

func (*Postgres) Close

func (d *Postgres) Close() error

Close closes the connection of the Postgresql adapter. It returns any error encountered during the connection closure process.

func (*Postgres) GetDb

func (d *Postgres) GetDb() *sql.DB

GetDb returns the underlying database connection of the Postgresql adapter.

func (*Postgres) GetDriver

func (d *Postgres) GetDriver() *pgxpool.Pool

GetDriver returns the underlying database native connection of the Postgresql adapter.

func (*Postgres) GetMigration

func (d *Postgres) GetMigration() (*migrate.Migrate, error)

GetMigration returns a new migration instance for the Postgresql database. The migration instance is configured based on the adapter's dialect options. The function returns the migration instance and any error encountered during its creation.

func (*Postgres) GetOptions

func (d *Postgres) GetOptions() *options.Dialect

GetOptions returns the options of the Postgresql adapter.

Jump to

Keyboard shortcuts

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