Documentation
¶
Overview ¶
Package database provides low-level database connectivity and helpers used by the DAO layer.
Index ¶
- func GetAllTyped[T any](db *DB, options ...func(*index.Options)) ([]T, error)
- func GetAllWhereTyped[T any](db *DB, field entities.Field, value any) ([]T, error)
- func GetTyped[T any](db *DB, field entities.Field, value any) (T, error)
- type DB
- func (db *DB) Backup(loc string)
- func (db *DB) Count(data any) (int, error)
- func (db *DB) CountWhere(field entities.Field, value any, to any) (int, error)
- func (db *DB) Create(data any) error
- func (db *DB) Delete(data any) error
- func (db *DB) Disconnect()
- func (db *DB) Drop(data any) error
- func (db *DB) Get(field entities.Field, value, to any) (any, error)
- func (db *DB) GetAll(to any, options ...func(*index.Options)) ([]any, error)
- func (db *DB) GetAllWhere(field entities.Field, value, to any) ([]any, error)
- func (db *DB) Reconnect()
- func (db *DB) Retrieve(field entities.Field, value, to any) (any, error)
- func (db *DB) Update(data any) error
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetAllTyped ¶ added in v1.7.0
GetAllTyped retrieves all records for type T and returns a typed slice.
NOTE: T is expected to be a struct type (not a pointer).
func GetAllWhereTyped ¶ added in v1.7.0
GetAllWhereTyped retrieves all matching records for type T filtered by field/value.
NOTE: T is expected to be a struct type (not a pointer).
func GetTyped ¶ added in v1.7.0
GetTyped retrieves a single record from the database and returns it as a concrete type.
NOTE: Go does not currently allow methods with type parameters, so these helpers are package-level functions.
T is expected to be a struct type (not a pointer). Storm expects a pointer to a struct destination; if you call this with a pointer type, it will return an invalid-type error.
Types ¶
type DB ¶
type DB struct {
Name string
// contains filtered or unexported fields
}
DB represents a database connection and its configuration
func Connect ¶
Connect establishes a database connection with the provided options It is the primary function to initiate a connection using various configuration options.
func ConnectToNamedDB ¶
ConnectToNamedDB establishes a database connection to a named database DEPRECATED: ConnectToNamedDB - Use Connect with WithNameSpace option instead
func (*DB) Backup ¶
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) Count ¶
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 ¶
CountWhere returns the number of records that match the specified fields.Field and value.
Parameters:
- fields.FieldName: The fields.Field to be used for filtering records.
- value: The value of the specified fields.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 ¶
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 ¶
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 ¶
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) Get ¶ added in v1.6.0
Get retrieves a single record from the database based on the specified fields.Field and value.
Parameters:
- fields.Field: The fields.Field to be used for filtering the record.
- value: The value of the specified fields.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 ¶
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
GetAllWhere retrieves all TemplateStore records that match the specified fields.Field and value.
Parameters:
- fields.Field: The fields.Field to be used for filtering records.
- value: The value of the specified fields.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) Retrieve ¶
Retrieve retrieves a single record from the database based on the specified fields.Field and value.
Parameters:
- fields.Field: The fields.Field to be used for filtering the record.
- value: The value of the specified fields.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.
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
WithCacheKey sets the cache key fields.Fields.Field for the database connection
func WithCaching ¶ added in v1.5.0
WithCaching enables or disables caching for the database connection
func WithEncryption ¶ added in v1.5.0
WithEncryption enables or disables encryption for the database connection By default, encryption is disabled. Reserved for Future use.
func WithNameSpace ¶ added in v1.5.0
WithNameSpace sets the namespace (database name) for the connection
func WithPoolSize ¶ added in v1.5.0
WithPoolSize sets the maximum connection pool size
func WithTimeout ¶ added in v1.5.0
WithTimeout sets the connection timeout in seconds
func WithVerbose ¶ added in v1.5.0
WithVerbose enables or disables verbose logging for the database connection