Documentation
¶
Overview ¶
Package zoom is a fast and lightweight ORM powered by Redis. It supports models of any arbitrary struct type, supports relationships between models, and provides basic querying functionality. It also supports running Redis commands directly.
Index ¶
- func Close()
- func Delete(model Model) error
- func DeleteById(modelName string, id string) error
- func GetConn() redis.Conn
- func Init(passedConfig *Configuration)
- func Interfaces(in interface{}) []interface{}
- func KeyExists(key string, conn redis.Conn) (bool, error)
- func MDelete(models []Model) error
- func MDeleteById(modelNames []string, ids []string) error
- func MSave(models []Model) error
- func MScanById(ids []string, models interface{}) error
- func Register(model Model) error
- func RegisterName(name string, model Model) error
- func Save(model Model) error
- func ScanById(id string, model Model) error
- func SetContains(key, member string, conn redis.Conn) (bool, error)
- func Unregister(model Model) error
- func UnregisterName(name string) error
- type ById
- type Configuration
- type DefaultData
- type KeyNotFoundError
- type MarshalerUnmarshaler
- type Model
- type ModelNameNotRegisteredError
- type ModelNotFoundError
- type ModelTypeNotRegisteredError
- type NameAlreadyRegisteredError
- type Query
- func (q *Query) Count() (int, error)
- func (q *Query) Exclude(fields ...string) *Query
- func (q *Query) Filter(filterString string, value interface{}) *Query
- func (q *Query) IdsOnly() ([]string, error)
- func (q *Query) Include(fields ...string) *Query
- func (q *Query) Limit(amount uint) *Query
- func (q *Query) Offset(amount uint) *Query
- func (q *Query) Order(fieldName string) *Query
- func (q *Query) Run() (interface{}, error)
- func (q *Query) RunOne() (interface{}, error)
- func (q *Query) Scan(in interface{}) error
- func (q *Query) ScanOne(in interface{}) error
- func (q *Query) String() string
- type TypeAlreadyRegisteredError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Close ¶
func Close()
Close closes the connection pool and shuts down the Zoom library. It should be run when application exits, e.g. using defer.
func Delete ¶
Delete removes a model from the database. It will throw an error if the type of the model has not yet been registered, if the Id field of the model is empty, or if there is a problem connecting to the database. If the model does not exist in the database, Delete will not return an error; it will simply have no effect.
func DeleteById ¶
DeleteById removes a model from the database by its registered name and id. By default modelName should be the string version of the type of model (without the asterisk, ampersand, or package prefix). If you used RegisterName instead of Register, modelName should be the custom name you used. DeleteById will throw an error if modelName is invalid or if there is a problem connecting to the database. If the model does not exist, DeleteById will not return an error; it will simply have no effect.
func GetConn ¶
GetConn gets a connection from the connection pool and returns it. It can be used for directly interacting with the database. Check out http://godoc.org/github.com/garyburd/redigo/redis for full documentation on the redis.Conn type.
func Init ¶
func Init(passedConfig *Configuration)
Init starts the Zoom library and creates a connection pool. It accepts a Configuration struct as an argument. Any zero values in the configuration will fallback to their default values. Init should be called once during application startup.
func Interfaces ¶ added in v0.4.0
func Interfaces(in interface{}) []interface{}
Interfaces convert interface{} to []interface{} Will panic if the type is invalid.
func KeyExists ¶
KeyExists returns true iff a given key exists in redis. If conn is nil, a new connection will be created and closed before the end of the function.
func MDelete ¶ added in v0.4.0
MDelete is like Delete but accepts a slice of models and deletes them all in a single transaction. See http://redis.io/topics/transactions. If an error is encountered in the middle of the transaction, the function will halt and return the error. In that case, any models which were deleted before the error was encountered will still be deleted. Usually this is fine because calling Delete on a model a second time will have no adverse effects.
func MDeleteById ¶ added in v0.4.0
MDeleteById is like DeleteById but accepts a slice of modelNames and ids and deletes them all in a single transaction. See http://redis.io/topics/transactions. The slice of modelNames and ids should be properly aligned so that, e.g., modelNames[0] corresponds to ids[0]. If there is an error in the middle of the transaction, the function will halt and return the error. In that case, any models which were deleted before the error was encountered will still be deleted. Usually this is fine because calling Delete on a model a second time will have no adverse effects.
func MSave ¶ added in v0.4.0
MSave is like Save but accepts a slice of models and saves them all in a single transaction. See http://redis.io/topics/transactions. If there is an error in the middle of the transaction, any models that were saved before the error was encountered will still be saved. Usually this is fine because saving a model a second time will have no adverse effects.
func MScanById ¶ added in v0.4.0
MScanById is like ScanById but accepts a slice of ids and a pointer to a slice of models. It executes the commands needed to retrieve the models in a single transaction. See http://redis.io/topics/transactions. The slice of ids and models should be properly aligned so that, e.g., ids[0] corresponds to models[0]. If there is an error in the middle of the transaction, the function will halt and return the error. Any models that were scanned before the error will still be valid. If any of the models in the models slice are nil, MScanById will use reflection to allocate memory for them.
func Register ¶
Register adds a model type to the list of registered types. Any struct you wish to save must be registered first. The type of model must be unique, i.e. not already registered. Each registered model gets a name, a unique string identifier, which by default is just the string version of the type (the asterisk and any package prefixes are stripped). See RegisterName if you would prefer to specify a custom name.
func RegisterName ¶ added in v0.4.0
RegisterName is like Register but allows you to specify a custom name to use for the model type. The custom name will be used as a prefix for all models of this type stored in redis. The custom name will also be used in functions which require a model name, such as queries.
func Save ¶
Save writes a model (a struct which satisfies the Model interface) to the redis database. Save throws an error if the type of the struct has not yet been registered or if there is a problem connecting to the database. If the Id field of the struct is empty, Save will mutate the struct by setting the Id. To make a struct satisfy the Model interface, you can embed zoom.DefaultData.
func ScanById ¶ added in v0.1.1
ScanById retrieves a model from redis and scans it into model. model should be a pointer to a struct of a registered type. ScanById will mutate the struct, filling in its fields. It returns an error if a model with that id does not exist or if there was a problem connecting to the database.
func SetContains ¶
SetContains returns true iff the redis set identified by key contains member. If conn is nil, a new connection will be created and closed before the end of the function.
func Unregister ¶ added in v0.4.0
Unregister removes a model type from the list of registered types. You only need to call UnregisterName or UnregisterType, not both.
func UnregisterName ¶
UnregisterName removes a model type (identified by modelName) from the list of registered model types. You only need to call UnregisterName or UnregisterType, not both.
Types ¶
type ById ¶ added in v0.5.0
type ById []*indexedPrimativesModel
utility type for quickly sorting by id
type Configuration ¶
type Configuration struct { Address string // Address to connect to. Default: "localhost:6379" Network string // Network to use. Default: "tcp" Database int // Database id to use (using SELECT). Default: 0 }
Configuration contains various options. It should be created once and passed in to the Init function during application startup.
type DefaultData ¶ added in v0.3.0
type DefaultData struct {
Id string `redis:"-"`
}
DefaultData should be embedded in any struct you wish to save. It includes important fields and required methods to implement Model.
func (DefaultData) GetId ¶ added in v0.3.0
func (d DefaultData) GetId() string
func (*DefaultData) SetId ¶ added in v0.3.0
func (d *DefaultData) SetId(id string)
type KeyNotFoundError ¶
type KeyNotFoundError struct {
// contains filtered or unexported fields
}
KeyNotFoundError is returned from Find, Scan, and Query functions if the model you are trying to find does not exist in the database.
func NewKeyNotFoundError ¶
func NewKeyNotFoundError(key string, modelType reflect.Type) *KeyNotFoundError
func (*KeyNotFoundError) Error ¶
func (e *KeyNotFoundError) Error() string
type MarshalerUnmarshaler ¶ added in v0.4.0
type MarshalerUnmarshaler interface { Marshal(v interface{}) ([]byte, error) // Return a byte-encoded representation of v Unmarshal(data []byte, v interface{}) error // Parse byte-encoded data and store the result in the value pointed to by v. }
Interface MarshalerUnmarshaler defines a handler for marshaling arbitrary data structures into a byte format and unmarshaling bytes into arbitrary data structures. Any struct which correctly implements the interface should have the property that what you put in using Marshal is identical to what you get out using Unmarshal.
type Model ¶
Model is an interface encapsulating anything that can be saved. Any struct which includes an embedded DefaultData field satisfies the Model interface.
func FindById ¶
FindById gets a model from the database. It returns an error if a model with that id does not exist or if there was a problem connecting to the database. By default modelName should be the string version of the type of model (without the asterisk, ampersand, or package prefix). If you used RegisterName instead of Register, modelName should be the custom name you used.
func MFindById ¶ added in v0.4.0
MFindById is like FindById but accepts a slice of model names and ids and returns a slice of models. It executes the commands needed to retrieve the models in a single transaction. See http://redis.io/topics/transactions. The slice of modelNames and ids should be properly aligned so that, e.g., modelNames[0] corresponds to ids[0]. If there is an error in the middle of the transaction, the function will halt and return the models retrieved so far (as well as the error).
type ModelNameNotRegisteredError ¶
type ModelNameNotRegisteredError struct {
// contains filtered or unexported fields
}
ModelNameNotRegisteredError is returned if you attempt to perform certain operations for unregistered names.
func NewModelNameNotRegisteredError ¶
func NewModelNameNotRegisteredError(name string) *ModelNameNotRegisteredError
func (*ModelNameNotRegisteredError) Error ¶
func (e *ModelNameNotRegisteredError) Error() string
type ModelNotFoundError ¶ added in v0.6.0
type ModelNotFoundError struct{}
ModelNotFoundError is returned from ScanOne and RunOne if a model that matches the query criteria was not found.
func NewModelNotFoundError ¶ added in v0.6.0
func NewModelNotFoundError() *ModelNotFoundError
func (*ModelNotFoundError) Error ¶ added in v0.6.0
func (e *ModelNotFoundError) Error() string
type ModelTypeNotRegisteredError ¶
type ModelTypeNotRegisteredError struct {
// contains filtered or unexported fields
}
ModelTypeNotRegisteredError is returned if you attempt to perform certain operations for unregistered types.
func NewModelTypeNotRegisteredError ¶
func NewModelTypeNotRegisteredError(typ reflect.Type) *ModelTypeNotRegisteredError
func (*ModelTypeNotRegisteredError) Error ¶
func (e *ModelTypeNotRegisteredError) Error() string
type NameAlreadyRegisteredError ¶
type NameAlreadyRegisteredError struct {
// contains filtered or unexported fields
}
NameAlreadyRegisteredError is returned if you try to register a name which has already been registered.
func NewNameAlreadyRegisteredError ¶
func NewNameAlreadyRegisteredError(name string) *NameAlreadyRegisteredError
func (*NameAlreadyRegisteredError) Error ¶
func (e *NameAlreadyRegisteredError) Error() string
type Query ¶ added in v0.3.0
type Query struct {
// contains filtered or unexported fields
}
Query represents a query which will retrieve some models from the database. A Query may consist of one or more query modifiers and can be run in several different ways with different query finishers.
func NewQuery ¶ added in v0.4.0
NewQuery is used to construct a query. modelName should be the name of a registered model. The query returned can be chained together with one or more query modifiers, and then executed using the Run, Scan, Count, or IdsOnly methods. If no query modifiers are used, running the query will return all models that match the type corresponding to modelName in uspecified order. NewQuery will set an error on the query if modelName is not the name of some registered model type. The error, same as any other error that occurs during the lifetime of the query, is not returned until the Query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Count ¶ added in v0.4.0
Count counts the number of models that would be returned by the query without actually retreiving the models themselves. Count will also return the first error that occured during the lifetime of the query object (if any). Otherwise, the second return value will be nil.
func (*Query) Exclude ¶ added in v0.4.0
Exclude specifies one or more field names which will *not* be read from the database and scanned. Any other fields *will* be read and scanned into the resulting models when the query is run. You can only use one of Include or Exclude, not both on the same query. Exclude will set an error if you try to use it with Include on the same query. The error, same as any other error that occurs during the lifetime of the query, is not returned until the Query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Filter ¶ added in v0.4.0
Filter applies a filter to the query, which will cause the query to only return models with attributes matching the expression. filterString should be an expression which includes a fieldName, a space, and an operator in that order. Operators must be one of "=", "!=", ">", "<", ">=", or "<=". You can only use Filter on fields which are indexed, i.e. those which have the `zoom:"index"` struct tag. If multiple filters are applied to the same query, the query will only return models which have matches for ALL of the filters. I.e. applying multiple filters is logially equivalent to combining them with a AND or INTERSECT operator. Filter will set an error on the query if the arguments are improperly formated, if the field you are attempting to filter is not indexed, or if the type of value does not match the type of the field. The error, same as any other error that occurs during the lifetime of the query, is not returned until the Query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) IdsOnly ¶ added in v0.4.0
IdsOnly returns only the ids of the models without actually retreiving the models themselves. IdsOnly will also return the first error that occured during the lifetime of the query object (if any). Otherwise, the second return value will be nil.
func (*Query) Include ¶ added in v0.4.0
Include specifies one or more field names which will be read from the database and scanned into the resulting models when the query is run. Field names which are not specified in Include will not be read or scanned. You can only use one of Include or Exclude, not both on the same query. Include will set an error if you try to use it with Exclude on the same query. The error, same as any other error that occurs during the lifetime of the query, is not returned until the Query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Limit ¶ added in v0.4.0
Limit specifies an upper limit on the number of records to return. If amount is 0, no limit will be applied. The default value is 0.
func (*Query) Offset ¶ added in v0.4.0
Offset specifies a starting index (inclusive) from which to start counting records that will be returned. The default value is 0.
func (*Query) Order ¶ added in v0.4.0
Order specifies a field by which to sort the records and the order in which records should be sorted. fieldName should be a field in the struct type specified by the modelName argument in the query constructor. By default, the records are sorted by ascending order. To sort by descending order, put a negative sign before the field name. Zoom can only sort by fields which have been indexed, i.e. those which have the `zoom:"index"` struct tag. However, in the future this may change. Only one order may be specified per query. However in the future, secondary orders may be allowed, and will take effect when two or more models have the same value for the primary order field. Order will set an error on the query if the fieldName is invalid, if another order has already been applied to the query, or if the fieldName specified does not correspond to an indexed field. The error, same as any other error that occurs during the lifetime of the query, is not returned until the Query is executed. When the query is executed the first error that occured during the lifetime of the query object (if any) will be returned.
func (*Query) Run ¶ added in v0.3.0
Run executes the query and returns the results in the form of an interface. The true type of the return value will be a slice of pointers to some regestired model type. If you need a type-safe way to run queries, look at the Scan method. Run will also return the first error that occured during the lifetime of the query object (if any). Otherwise, the second return value will be nil.
func (*Query) RunOne ¶ added in v0.6.0
RunOne is exactly like Run, returns only the first model that fits the query criteria, or if no models fit the critera, returns an error. If you need to do this in a type-safe way, look at the ScanOne method.
func (*Query) Scan ¶ added in v0.4.0
Scan (like Run) executes the query but instead of returning the results it attempts to scan the results into in. The type of in should be a pointer to a slice of pointers to a registered model type. Scan will return the first error that occured during the lifetime of the query object (if any), or will return an error if you provided an interface with an invalid type. Otherwise, the return value will be nil.
type TypeAlreadyRegisteredError ¶
type TypeAlreadyRegisteredError struct {
// contains filtered or unexported fields
}
TypeAlreadyRegisteredError is returned if you try to register a type which has already been registered.
func NewTypeAlreadyRegisteredError ¶
func NewTypeAlreadyRegisteredError(typ reflect.Type) *TypeAlreadyRegisteredError
func (*TypeAlreadyRegisteredError) Error ¶
func (e *TypeAlreadyRegisteredError) Error() string