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 ¶
- Variables
- func RegisterAdapter(dialectName string, factory AdapterFactory)
- type Adapter
- func GetAdapter(ctx context.Context, dialectName string) (Adapter, error)
- func NewClickhouseDB(ctx context.Context, opts *options.Dialect) (Adapter, error)
- func NewDialectDB(ctx context.Context, dialectName string) (Adapter, error)
- func NewPostgresDB(ctx context.Context, opts *options.Dialect) (Adapter, error)
- func NewSqliteDB(ctx context.Context, opts *options.Options) (Adapter, error)
- type AdapterFactory
- type BadgerDB
- type Clickhouse
- type Db
- type Dialect
- type Postgres
Constants ¶
This section is empty.
Variables ¶
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 ¶
GetAdapter creates a new database adapter based on the provided dialect name.
func NewClickhouseDB ¶
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 ¶
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 ¶
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.
type AdapterFactory ¶
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 ¶
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) Exists ¶
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 ¶
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()
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) GetMigration ¶
func (*Db) GetOptions ¶
type Postgres ¶
type Postgres struct {
// contains filtered or unexported fields
}
Postgres represents a Postgresql database adapter.
func ToPostgres ¶
func (*Postgres) Close ¶
Close closes the connection of the Postgresql adapter. It returns any error encountered during the connection closure process.
func (*Postgres) GetDb ¶
GetDb returns the underlying database connection of the Postgresql adapter.
func (*Postgres) GetDriver ¶
GetDriver returns the underlying database native connection of the Postgresql adapter.
func (*Postgres) GetMigration ¶
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 ¶
GetOptions returns the options of the Postgresql adapter.