Documentation ¶
Overview ¶
Package database provides basic interfaces for modelling data from the database.
Index ¶
- Variables
- func Bind(a, b string, mm ...Model) func(int, Model)
- func CompareKeys(a, b interface{}) bool
- func Connect(dsn string) (*sqlx.DB, error)
- func List(vals ...interface{}) query.Expr
- func LoadRelations(relations map[string]RelationFunc, loaders *Loaders, mm ...Model) error
- func MapKey(key string, mm []Model) []interface{}
- func OrWhere(m Model, args ...string) query.Option
- func Scan(val interface{}) ([]byte, error)
- func Search(col, pattern string) query.Option
- func Where(m Model, args ...string) query.Option
- type Binder
- type Loader
- type LoaderFunc
- type Loaders
- type Model
- type Paginator
- type RelationFunc
- type Store
- func (s Store) All(i interface{}, table string, opts ...query.Option) error
- func (s Store) Chown(table string, from, to int64) error
- func (s Store) Create(table string, mm ...Model) error
- func (s Store) Delete(table string, mm ...Model) error
- func (s Store) Get(i interface{}, table string, opts ...query.Option) error
- func (s Store) Paginate(table string, page, limit int64, opts ...query.Option) (Paginator, error)
- func (s Store) Update(table string, mm ...Model) error
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
Functions ¶
func Bind ¶
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 ¶
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 ¶
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 OrWhere ¶
OrWhere returns an OR WHERE clause on the given Model. This operates the same way as Where, only the returned clause is different.
func Search ¶
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 ¶
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 ¶
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 (*Loaders) Delete ¶
Delete removes the loader of the given name from the Loaders store. If the loader cannot be found then nothing happens.
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 ¶
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 ¶
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 ¶
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 ¶
Store is a simple struct for performing SELECT, INSERT, UPDATE, and DELETE queries on tables.
func (Store) All ¶
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) Create ¶
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 ¶
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 ¶
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 ¶
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.