database

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package database provides low-level database connectivity and helpers used by the DAO layer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFunctionName added in v1.5.0

func GetFunctionName(temp interface{}) string

GetFunctionName returns the name of the function passed as an argument It uses reflection to obtain the function's program counter and retrieves its name.

func GetStructType added in v1.5.1

func GetStructType(data any) string

func IsValidFieldInStruct added in v1.5.1

func IsValidFieldInStruct(fromField Field, data any) error

func IsValidTypeForField added in v1.5.1

func IsValidTypeForField(field Field, data, forStruct any) error

Types

type DB

type DB struct {
	Name string
	// contains filtered or unexported fields
}

DB represents a database connection and its configuration

func Connect

func Connect(table any, options ...Option) *DB

Connect establishes a database connection with the provided options It is the primary function to initiate a connection using various configuration options.

func ConnectToNamedDB

func ConnectToNamedDB(name string, options ...Option) *DB

ConnectToNamedDB establishes a database connection to a named database DEPRECATED: ConnectToNamedDB - Use Connect with WithNameSpace option instead

func (*DB) Backup

func (db *DB) Backup(loc string)

Backup creates a backup of the database to the specified location It disconnects the database, performs the backup, and then reconnects. The backup process is timed and logged.

func (*DB) CacheSpew added in v1.6.0

func (db *DB) CacheSpew()

func (*DB) Count

func (db *DB) Count(data any) (int, error)

Count returns the total number of records of the specified type in the database.

Parameters:

  • data: A pointer to the struct representing the type whose records are to be counted.

Returns:

  • int: The total number of records.
  • error: An error object if any issues occur during the counting process; otherwise, nil.

func (*DB) CountWhere

func (db *DB) CountWhere(fieldName Field, value any, to any) (int, error)

CountWhere returns the number of records that match the specified field and value.

Parameters:

  • fieldName: The field to be used for filtering records.
  • value: The value of the specified field to filter records.
  • to: A pointer to the struct representing the type whose records are to be counted.

Returns:

  • int: The number of records that match the specified criteria.
  • error: An error object if any issues occur during the counting process; otherwise, nil.

func (*DB) Create

func (db *DB) Create(data any) error

Create adds a new record to the database.

Parameters:

  • data: A pointer to the struct representing the record to be created.

Returns:

  • error: An error object if any issues occur during the creation process; otherwise, nil.

func (*DB) Delete

func (db *DB) Delete(data any) error

Delete removes the specified record from the database.

Parameters:

  • data: A pointer to the struct representing the record to be deleted.

Returns:

  • error: An error object if any issues occur during the deletion process; otherwise, nil.

func (*DB) Disconnect

func (db *DB) Disconnect()

Disconnect closes the database connection and removes it from the connection pool It uses a timing mechanism to log the duration of the disconnection process. If disconnection fails, it logs the error and panics with a wrapped disconnect error.

func (*DB) Drop

func (db *DB) Drop(data any) error

Drop removes the entire bucket or collection associated with the specified struct from the database.

Parameters:

  • data: A pointer to the struct representing the type whose bucket or collection is to be dropped.

Returns:

  • error: An error object if any issues occur during the drop process; otherwise, nil.

func (*DB) Flush added in v1.6.0

func (db *DB) Flush() error

func (*DB) Get added in v1.6.0

func (db *DB) Get(field Field, value, to any) (any, error)

Get retrieves a single record from the database based on the specified field and value.

Parameters:

  • field: The field to be used for filtering the record.
  • value: The value of the specified field to filter the record.
  • to: A pointer to the struct where the retrieved record will be stored.

Returns:

  • any: The retrieved record.
  • error: An error object if any issues occur during the retrieval process; otherwise, nil.

func (*DB) GetAll

func (db *DB) GetAll(to any, options ...func(*index.Options)) ([]any, error)

GetAll retrieves all records of the specified type from the database.

Parameters:

  • to: A pointer to a slice where the retrieved records will be stored.

Returns:

  • []any: A slice of all retrieved records.
  • error: An error object if any issues occur during the retrieval process; otherwise, nil.

func (*DB) GetAllWhere added in v1.6.0

func (db *DB) GetAllWhere(field Field, value, to any) ([]any, error)

GetAllWhere retrieves all TemplateStore records that match the specified field and value.

Parameters:

  • field: The field to be used for filtering records.
  • value: The value of the specified field to filter records.

Returns:

  • []TemplateStore: A slice of TemplateStore records that match the specified criteria.
  • error: An error object if any issues occur during the retrieval process; otherwise, nil.

func (*DB) PreLoadCache added in v1.5.0

func (db *DB) PreLoadCache(to any, options ...func(*index.Options)) error

func (*DB) Reconnect added in v1.2.30

func (db *DB) Reconnect()

func (*DB) Retrieve

func (db *DB) Retrieve(field Field, value, to any) (any, error)

Retrieve retrieves a single record from the database based on the specified field and value.

Parameters:

  • field: The field to be used for filtering the record.
  • value: The value of the specified field to filter the record.
  • to: A pointer to the struct where the retrieved record will be stored.

Returns:

  • any: The retrieved record.
  • error: An error object if any issues occur during the retrieval process; otherwise, nil.

DEPRECATED: Use Get instead.

func (*DB) Update

func (db *DB) Update(data any) error

Update modifies an existing record in the database.

Parameters:

  • data: A pointer to the struct representing the record to be updated.

Returns:

  • error: An error object if any issues occur during the update process; otherwise, nil.

type Field added in v1.4.1

type Field field

func (Field) String added in v1.5.1

func (f Field) String() string

type Option added in v1.5.0

type Option func(*connectionConfig)

Option is a function that configures the database connection

func WithCacheKey added in v1.5.0

func WithCacheKey(field Field) Option

WithCacheKey sets the cache key field for the database connection

func WithCaching added in v1.5.0

func WithCaching(enabled bool) Option

WithCaching enables or disables caching for the database connection

func WithEncryption added in v1.5.0

func WithEncryption(enabled bool) Option

WithEncryption enables or disables encryption for the database connection By default, encryption is disabled. Reserved for Future use.

func WithIndex added in v1.5.0

func WithIndex(field Field) Option

WithIndex adds a single index field for the database connection Used to create an index on a field for faster querying. Reserved for Future use.

func WithIndices added in v1.5.0

func WithIndices(indices []Field) Option

WithIndices sets the indices for the database connection Used to create indices on fields for faster querying. Reserved for Future use.

func WithNameSpace added in v1.5.0

func WithNameSpace(name string) Option

WithNameSpace sets the namespace (database name) for the connection

func WithPoolSize added in v1.5.0

func WithPoolSize(size int) Option

WithPoolSize sets the maximum connection pool size

func WithTimeout added in v1.5.0

func WithTimeout(seconds int) Option

WithTimeout sets the connection timeout in seconds

func WithVerbose added in v1.5.0

func WithVerbose(enabled bool) Option

WithVerbose enables or disables verbose logging for the database connection

Jump to

Keyboard shortcuts

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