repository

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package repository provides generic type-safe database operations using Go generics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanupExpired

func CleanupExpired[T any](
	database *db.DB,
	bucket []byte,
	threshold time.Time,
	getTimestamp func(*T) time.Time,
) error

CleanupExpired removes records from a bucket that are older than the given threshold. The getTimestamp function extracts the timestamp to compare from each record.

This is a two-phase operation:

  1. View transaction: collect keys of expired records
  2. Update transaction: delete collected keys

Example:

err := repository.CleanupExpired(db, db.BucketAuth, threshold, func(e *Entry) time.Time {
    return e.CreatedAt
})

Types

type Repository

type Repository[T any] struct {
	// contains filtered or unexported fields
}

Repository provides type-safe CRUD operations for a bbolt bucket. T is the type of records stored in this repository.

func New

func New[T any](database *db.DB, bucket []byte, name string) *Repository[T]

New creates a new typed repository for the given bucket.

Parameters:

  • database: the bbolt database wrapper
  • bucket: the bucket name (e.g., db.BucketProviders)
  • name: human-readable name for error messages (e.g., "provider")

func (*Repository[T]) Count

func (r *Repository[T]) Count() (int, error)

Count returns the total number of records in the bucket.

func (*Repository[T]) Delete

func (r *Repository[T]) Delete(id string) error

Delete removes a record by ID. Returns an error if the record does not exist.

func (*Repository[T]) DeleteIfExists

func (r *Repository[T]) DeleteIfExists(id string) error

DeleteIfExists removes a record by ID if it exists. Does not return an error if the record doesn't exist.

func (*Repository[T]) Exists

func (r *Repository[T]) Exists(id string) (bool, error)

Exists checks if a record with the given ID exists.

func (*Repository[T]) FindFirst

func (r *Repository[T]) FindFirst(predicate func(*T) bool) (*T, error)

FindFirst returns the first record matching the given predicate. Returns an error if no matching record is found.

Example:

item, err := repo.FindFirst(func(obj *MyType) bool {
    return obj.Name == "target"
})

func (*Repository[T]) Get

func (r *Repository[T]) Get(id string) (*T, error)

Get retrieves a single record by ID. Returns an error if the record does not exist.

func (*Repository[T]) List

func (r *Repository[T]) List() ([]*T, error)

List returns all records in the bucket.

func (*Repository[T]) ListFiltered

func (r *Repository[T]) ListFiltered(predicate func(*T) bool) ([]*T, error)

ListFiltered returns all records matching the given predicate.

Example:

items, err := repo.ListFiltered(func(obj *MyType) bool {
    return obj.Status == "active"
})

func (*Repository[T]) Put

func (r *Repository[T]) Put(id string, obj *T) error

Put stores a record with the given ID. Overwrites any existing record with the same ID.

func (*Repository[T]) Update

func (r *Repository[T]) Update(id string, fn func(*T) error) error

Update performs an atomic get-modify-put operation. The provided function receives a pointer to the record and can modify it. The modified record is then saved back to the database.

Example:

err := repo.Update(id, func(obj *MyType) error {
    obj.Field = "new value"
    return nil
})

Jump to

Keyboard shortcuts

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