database

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package database provides basic interfaces for modelling data from the database.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

func Bind

func Bind(a, b string, mm ...Model) func(int, Model)

Bind returns a LoaderFunc that checks to see if the key on the target model, specified via a, at index i matches the key on the model being loaded, specified via b. If so, then that model is bound to the target model. This typically assumes that both the keys being retrieved have an underlying type of int64.

func CompareKeys

func CompareKeys(a, b interface{}) bool

CompareKeys compares the two given interface values, assuming they are either of type int64, or sql.NullInt64.

func Connect

func Connect(dsn string) (*sqlx.DB, error)

Connect returns an sqlx database connection to a PostgreSQL database using the given dsn. Once the connection is open a subsequent Ping is made to the database to check the connectivity.

func List

func List(vals ...interface{}) query.Expr

List returns a query.Expr for a list of values. If the given list of values is empty, then a query.List expression is simply returned, only containing a single -1 in the list. This will allow for situations where queries that use WHERE IN (...) to still build in a valid way.

func LoadRelations

func LoadRelations(relations map[string]RelationFunc, loaders *Loaders, mm ...Model) error

LoadRelation loads all of the given relations from the given map, for all of the given models, using the respective Loader from the given Loaders type.

func MapKey

func MapKey(key string, mm []Model) []interface{}

MapKey returns a slice of values for the given key from the given slice of models.

func OrWhere

func OrWhere(m Model, args ...string) query.Option

OrWhere returns an OR WHERE clause on the given Model. This operates the same way as Where, only the returned clause is different.

func Scan

func Scan(val interface{}) ([]byte, error)

Scan the given interface value into a slice of bytes.

func Search(col, pattern string) query.Option

Search returns a WHERE LIKE clause for the given column and pattern. If the pattern is empty then no WHERE LIKE clause is returned.

func Where

func Where(m Model, args ...string) query.Option

Where returns a WHERE clause on the given Model if the given Model is non-zero. The args variadic argument is used to specify the column, and value to use for the WHERE clause. The first item in the argument is the column on which the WHERE clause is performed. The second item is the value to use in thw WHERE clause. If no second item is given then the primary key of the given model is used instead.

Types

type Binder

type Binder interface {
	// Bind the given models to the implementation. This would typically be
	// used for binding related models to a model, or to a Store if you want
	// to constrain the queries performed via a Store. It is expected for the
	// given Model to be type asserted to the concrete type.
	Bind(...Model)
}

type Loader

type Loader interface {
	// Load will load models under the given key for the given slice of values.
	// This will be the equivalent of a WHERE key IN (vals,...). The LoaderFunc
	// will be invoked for each model that has been retrieved from the database.
	Load(string, []interface{}, LoaderFunc) error
}

type LoaderFunc

type LoaderFunc func(int, Model)

LoaderFunc is the callback that is called when a model is to be loaded into its relating model. This takes an integer, that represents an index in a slice of models, and the model being loaded, for example,

fn := func(i int, m Model) {
    p := posts[i]
    _, id := m.Primary()
    if p.UserID == id {
        p.User = m
    }
}

type Loaders

type Loaders struct {
	// contains filtered or unexported fields
}

Loaders stores a Loader by their respective name. This is typically passed to the LoadRelations function when loading a model's relationships. Each Loader is called in the order by which they were put added.

func NewLoaders

func NewLoaders() *Loaders

NewLoaders creates a new empty Loaders store.

func (*Loaders) Copy

func (ls *Loaders) Copy() *Loaders

Copy returns a copy of the given Loader.

func (*Loaders) Delete

func (ls *Loaders) Delete(name string)

Delete removes the loader of the given name from the Loaders store. If the loader cannot be found then nothing happens.

func (*Loaders) Get

func (ls *Loaders) Get(name string) (Loader, bool)

Get returns a Loader of the given name.

func (*Loaders) Put

func (ls *Loaders) Put(name string, l Loader)

Put adds a Loader of the given name to the underlying map.

type Model

type Model interface {
	Binder

	// SetPrimary will set the value of the primary key.
	SetPrimary(int64)

	// Primary will return the name of the column for the primary key, and the
	// column's value.
	Primary() (string, int64)

	// IsZero will return whether all of the model's underlying values are a
	// zero value of their type. This should return true on underlying nil
	// values for the implementation.
	IsZero() bool

	// JSON will return a map of the fields from the Model that should be used
	// for JSON representation. The given string will be used as the address of
	// the server from which the Model can be accessed. This will be used to
	// set any URL fields that may be set in the returned map.
	JSON(string) map[string]interface{}

	// Endpoint will return the endpoint used to access the model. This will
	// append the given variadic list of strings to the returned endpoint.
	Endpoint(...string) string

	// Values will return a map of the model's values. This will be called
	// during calls to Store.Create, and Store.Update. Each key in the returned
	// map should be snake-case, and have the respective models value for that
	// key.
	Values() map[string]interface{}
}

Model interface wraps the basic methods that a model will have. This assumes that models implementing this interface use 64 bit integers for their primary keys.

func ModelSlice

func ModelSlice(l int, get func(int) Model) []Model

ModelSlice converts a slice of models of length l, into a slice of Model. The given callback takes the current index of the new Model slice as its only argument. It is expected for this index to be used to return the original type that implements the Model interface from a source slice.

type Paginator

type Paginator struct {
	Next   int64
	Prev   int64
	Offset int64
	Page   int64
	Limit  int64
	Pages  []int64
}

Paginator stores information about a paginated table. This is typically used for performing subsequent queries to retrieve the offset records from the table.

type RelationFunc

type RelationFunc func(Loader, ...Model) error

RelationFunc is a callback that is returned from the Relation function. This will perform the actual loading of the model's relationships.

func Relation

func Relation(fk, pk string) RelationFunc

Relation is used for defining relations between models. This returns a RelationFunc, which when called will invoke the given Loader against the given models. The returned callback will use the defined foreign key, and primary key for actually performing the relationship loading, and binding.

type Store

type Store struct {
	*sqlx.DB
}

Store is a simple struct for performing SELECT, INSERT, UPDATE, and DELETE queries on tables.

func (Store) All

func (s Store) All(i interface{}, table string, opts ...query.Option) error

All performs a SELECT query on the given table using the given query options. The given interface is expected to be a slice, which is then populated via sqlx.

func (Store) Chown

func (s Store) Chown(table string, from, to int64) error

func (Store) Create

func (s Store) Create(table string, mm ...Model) error

Create performs an INSERT on the given table for each given model. The ID of the new record is set on the created model.

func (Store) Delete

func (s Store) Delete(table string, mm ...Model) error

Delete all the given models from the given table. This expects the models given to share the same column for the primary key.

func (Store) Get

func (s Store) Get(i interface{}, table string, opts ...query.Option) error

Get performs a SELECT query on the given table using the given query options. This will return a single record from the given table.

func (Store) Paginate

func (s Store) Paginate(table string, page, limit int64, opts ...query.Option) (Paginator, error)

Paginate the records in the table and return the paginator for the given page. The returned struct contains information about the paginated data, but not the data itself. It is expected for a subsequent All call to be made using the paginator information to get the desired data.

func (Store) Update

func (s Store) Update(table string, mm ...Model) error

Update the given models in the given table. This expects the models given to share the same column for the primary key.

Jump to

Keyboard shortcuts

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