Documentation ¶
Overview ¶
Package model implements convenience methods for managing indexes on top of the Store. See this doc for the general idea https://github.com/m3o/dev/blob/feature/storeindex/design/auto-indexes.md Prior art/Inspirations from github.com/gocassa/gocassa, which is a similar package on top an other KV store (Cassandra/gocql)
Index ¶
Constants ¶
const ( OrderTypeUnordered = OrderType("unordered") OrderTypeAsc = OrderType("ascending") OrderTypeDesc = OrderType("descending") )
Variables ¶
var ( ErrorNilInterface = errors.New("interface is nil") ErrorNotFound = errors.New("not found") ErrorMultipleRecordsFound = errors.New("multiple records found") )
var ( // DefaultKey is the default field for indexing DefaultKey = "ID" // DefaultIndex is the ID index DefaultIndex = newIndex("ID") // DefaultModel is the default model DefaultModel = NewModel() )
Functions ¶
This section is empty.
Types ¶
type Index ¶
type Index struct { FieldName string // Type of index, eg. equality Type string Order Order // Do not allow duplicate values of this field in the index. // Useful for emails, usernames, post slugs etc. Unique bool // Strings for ordering will be padded to a fix length // Not a useful property for Querying, please ignore this at query time. // Number is in bytes, not string characters. Choose a sufficiently big one. // Consider that each character might take 4 bytes given the // internals of reverse ordering. So a good rule of thumbs is expected // characters in a string X 4 StringOrderPadLength int // True = base32 encode ordered strings for easier management // or false = keep 4 bytes long runes that might dispaly weirdly Base32Encode bool FloatFormat string Float64Max float64 Float32Max float32 }
Index represents a data model index for fast access
func ByEquality ¶
ByEquality constructs an equiality index on `fieldName`
type Model ¶
type Model interface { // Context sets the context for the model returning a new copy Context(ctx context.Context) Model // Register a new model eg. User struct, Order struct Register(v interface{}) error // Create a new object. (Maintains indexes set up) Create(v interface{}) error // Update will take an existing object and update it. // TODO: Make use of "sync" interface to lock, read, write, unlock Update(v interface{}) error // Read accepts a pointer to a value and expects to fine one or more // elements. Read throws an error if a value is not found or we can't // find a matching index for a slice based query. Read(query Query, resultPointer interface{}) error // Deletes a record. Delete only support Equals("id", value) for now. // @todo Delete only supports string keys for now. Delete(query Query) error }
Model represents a place where data can be saved to and queried from.
type Option ¶
type Option func(*Options)
func WithContext ¶
WithContext sets the context for all queries
func WithDatabase ¶
WithDatabase sets the default database for queries
func WithIndexes ¶
WithIndexes creates an option with the given indexes
func WithNamespace ¶
WithNamespace sets the namespace to scope to
type Options ¶
type Options struct { // Database sets the default database Database string // Table sets the default table Table string // Enable debug logging Debug bool // The indexes to use for queries Indexes []Index // Namespace to scope to Namespace string // Store is the storage engine Store store.Store // Context is the context for all model queries Context context.Context // Key is the fiel name of the primary key Key string }
type Order ¶
type Order struct { FieldName string // Ordered or unordered keys. Ordered keys are padded. // Default is true. This option only exists for strings, where ordering // comes at the cost of having rather long padded keys. Type OrderType }
Order is the order of the index