database

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailToOpenDatabase = apperrors.System(nil, "fail to open database: %s", "DB:FAIL_TO_OPEN_DATABASE")
	ErrFailToRunMigration = apperrors.System(nil, "fail to run migration", "DB:FAIL_TO_RUN_MIGRATION")
	ErrDatabaseMustBeFile = apperrors.Business("database must be a file: %s", "DB:DATABASE_MUST_BE_FILE")
	ErrDatabaseMustExist  = apperrors.Business("database file must exist: %s", "DB:DATABASE_MUST_EXIST")
)
View Source
var ErrFailedToCreateEntry = apperrors.System(nil, "failed to create entry", "DB:FailedToCreateEntry")

Functions

func GroupByHash

func GroupByHash[T model.IEntry](entries []T) (map[string]T, []string, error)

func NewStorage

func NewStorage[T model.IEntry](db *sql.DB, opt Options) (storage.Storage[T], error)

func Open

func Open(ctx context.Context, conf Options) (*sql.DB, error)

func Where

func Where[T model.IEntry](where storage.WhereOptions, has bool, entry DBEntry[T]) bool

Types

type DBEntry

type DBEntry[T model.IEntry] struct {
	Hash       string         `db:"hash,primarykey"`
	SourceName string         `db:"source_name"`
	ImageURL   string         `db:"image_url"`
	Text       string         `db:"text"`
	Categories []byte         `db:"categories"`
	URL        string         `db:"url"`
	Status     storage.Status `db:"status"`
	CreatedAt  time.Time      `db:"created_at"`
	HasStory   bool           `db:"has_story"`
	TTL        time.Time      `db:"ttl"`
}

func NewEntry

func NewEntry[T model.IEntry](ttl time.Duration, entry storage.Entry[T]) (DBEntry[T], error)

func (DBEntry[T]) ToEntry

func (e DBEntry[T]) ToEntry(target T) T

type DBEntryToUpdate

type DBEntryToUpdate[T model.IEntry] struct {
	Hash     string         `db:"hash,primarykey"`
	Status   storage.Status `db:"status"`
	HasStory bool           `db:"has_story"`
}

func EntryToUpdate

func EntryToUpdate[T model.IEntry](entry storage.Entry[T]) (DBEntryToUpdate[T], error)

type Options

type Options struct {
	storage.Options `fig:",squash"    yaml:",inline"`
	Path            string `fig:"path"       yaml:"path"`
	MustExist       bool   `fig:"must_exist" yaml:"must_exist"`
}

type Storage

type Storage[T model.IEntry] struct {
	// contains filtered or unexported fields
}

func (Storage[T]) Cleanup

func (s Storage[T]) Cleanup() (int64, error)

func (Storage[T]) FindByHasStory

func (s Storage[T]) FindByHasStory(opt storage.FindByHasStoryOptions) ([]T, error)

func (Storage[T]) FindByHash

func (s Storage[T]) FindByHash(hash string) (T, error)

func (Storage[T]) Has

func (s Storage[T]) Has(hash string) (bool, error)

func (Storage[T]) Store

func (s Storage[T]) Store(entry storage.Entry[T]) error

func (Storage[T]) Update

func (s Storage[T]) Update(entry storage.Entry[T]) error

func (Storage[T]) Where

func (s Storage[T]) Where(where storage.WhereOptions, list []T) ([]T, error)

Jump to

Keyboard shortcuts

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