db

package
v0.0.0-...-9d9692a Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoRows          = errors.New("db: no rows in result")
	ErrConflict        = errors.New("db: database state violation")
	ErrSyntaxPrivilege = errors.New("db: syntax error or insufficient privilege")
	ErrInvalidData     = errors.New("db: invalid data for database operation")
	ErrNil             = errors.New("db: nil pointer")
	ErrEncoding        = errors.New("db: invalid data encoding")
	ErrInvalidColumn   = errors.New("db: invalid or non-permissible column")
)

some common errors.

View Source
var MemberAllowedCols = []Column{"sid", "uid"}

MemberAllowedCols is a list of columns allowed for sorting.

View Source
var UserAllowedCols = []Column{"uid", "email", "username"}

UserAllowedCols is a list of columns allowed for sorting.

Functions

This section is empty.

Types

type Collection

type Collection[Item, Filter, Updater, Id any] interface {
	// Insert adds multiple items to the database. If no items, then ErrNoRows.
	Insert(ctx context.Context, items ...Item) error
	// DeleteOne removes exactly one record from the database.
	// If not found, then ErrNoRows. If nil id passed, then ErrNil.
	DeleteOne(ctx context.Context, id *Id) error
	// UpdateOne modifies exactly one record in the database.
	// If no records match then ErrNoRows.
	UpdateOne(context.Context, *Id, *Updater) error
	// Find fetches all the records that match the given filter and projects
	// them as a list. If no records match, then empty list.
	Find(context.Context, *Filter, *Projector) (*Iterable[Item], error)
	// FindOne fetches exactly one matching record. If no such record exist
	// in the database, then ErrNoRows.
	FindOne(ctx context.Context, id *Id) (Item, error)
}

Collection is a generic implementation of a collection with support for basic CRUD operations. It is assumed to be thread-safe.

type Column

type Column string

Column defines the column names over which sorting and filtering can be performed for a collection.

func (Column) String

func (c Column) String() string

String implements fmt.Stringer on Column.

type Iterable

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

Iterable is a list of generic items being iterable. Iteration is provided via a closure function.

Note: If the `GOEXPERIMENT=range` in go version 1.22 becomes supported, then such (push) iterator pattern will have compiler support for the `for range` loops.

func NewIterable

func NewIterable[T any](iterator func(yield func(T) bool) (int, error)) *Iterable[T]

NewIterable is the construct for list from a closure that yields successive values of T and reports the total items or error.

func (*Iterable[T]) Err

func (list *Iterable[T]) Err() error

Err reports any errors that occurred during the iteration over the list. It is supposed to be called after iteration, otherwise the call panics.

func (*Iterable[T]) Iterator

func (list *Iterable[T]) Iterator(yield func(T) bool)

Iterator is the iterator that yields successive values of T in list. It is a single-use iterator and the entire list is consumed after looping over it. Cannot call Iterator after consuming the list, otherwise call panics.

func (*Iterable[T]) Total

func (list *Iterable[T]) Total() int

Total returns the total number of elements in the list. It is supposed to be called after consuming the list. If it is called before iterating over the list, then call panics.

type Member

type Member struct {
	Sid string
	Uid string
}

Member depicts the member object for interactions with the members datastore.

type MemberFilter

type MemberFilter Member

MemberFilter provides fields for filtering the members.

type MemberId

type MemberId Member

MemberId is the 'id' type for member collection. Both sid and uid determine a member uniquely.

type MemberUpdater

type MemberUpdater struct{}

MemberUpdater provides fields necessary for update operation for member record.

type Paging

type Paging struct {
	Limit  int
	Offset int
}

Paging provides pagination options for querying the database.

type PasswordUpdater

type PasswordUpdater struct {
	Old []byte
	New []byte
	Uid string
}

PasswordUpdater provides fields necessary for update password operation for a user record.

type Projector

type Projector struct {
	Order  []Sorter // column order matters
	Paging *Paging  // pagination options
}

Projector provides projection options for fetching the data from a collection.

type Scount

type Scount struct {
	Sid         string
	Owner       string
	Title       string
	Description string
}

Scount depicts the scount object for interaction with scounts datastore.

type ScountFilter

type ScountFilter struct {
	Sid   string
	Uid   string
	Owner string
	Title string
}

ScountFilter provides fields for filtering the users.

type ScountId

type ScountId struct {
	Sid string
}

ScountId is the 'id' type for scount collection. Sid is the primary key or object identifier in the database.

type ScountUpdater

type ScountUpdater struct {
	Owner string
	Title string
}

ScountUpdater provides fields for updating scounts.

type Sorter

type Sorter struct {
	Column Column // on which column
	Desc   bool   // descending order
}

Sorter defines the sorting order for a particular column.

type Store

type Store struct {
	Users interface {
		Collection[User, UserFilter, UserUpdater, UserId]
		FindByEmail(ctx context.Context, email string) (User, error)
		UpdatePassword(context.Context, *PasswordUpdater) error
	}
	Scounts Collection[Scount, ScountFilter, ScountUpdater, ScountId]
	Members Collection[Member, MemberFilter, MemberUpdater, MemberId]
}

Store provides an interface for all datastore operations in one place.

type User

type User struct {
	Uid      string // id
	Email    string // unique
	Username string
	Password []byte
}

User depicts the user object for interactions with users datastore.

type UserFilter

type UserFilter struct {
	Uid      string
	Email    string
	Username string
}

UserFilter provides fields for filtering the users.

type UserId

type UserId struct {
	Uid string
}

UserId is the 'id' type for user collection. Uid is primary key or object identifier in the database.

type UserUpdater

type UserUpdater struct {
	Username string
}

UserUpdater provides fields for updating users.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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