README
camfou-modinha-redis
The RedisDocument mixin defines a collection of persistence methods that map cleanly between HTTP semantics and Redis data structures.
Usage
Suppose we've defined an Account model with Modinha like so:
var Modinha = require('camfou-modinha')
, RedisDocument = require('camfou-modinha-redis').RedisDocument
var Account = Modinha.define('accounts', {
email: { type: 'string', required: true, unique: true },
role: { type: 'string', secondary: true, enum: ['admin', 'editor', 'author'] },
hash: { type: 'string', private: true }
});
Account.extend(RedisDocument);
RedisDocument will add the following persistence methods to the Account document.
HTTP MODEL METHOD
GET /accounts Account.list(options, callback)
GET /accounts/id Account.get(ids, options, callback)
POST /accounts Account.insert(data, options, callback)
PUT /accounts/id Account.replace(id, data, options, callback)
PATCH /accounts/id Account.patch(id, data, options, callback)
DELETE /accounts/id Account.delete(id, callback)
Extending Account with RedisDocument will also define the following properties on Account.schema:
_id: { type: 'string', required: true, default: Model.defaults.uuid },
created: { type: 'number', order: true, default: Model.defaults.timestamp },
modified: { type: 'number', order: true, default: Model.defaults.timestamp }
Since we defined unique
and secondary
properties on email and role, respectively, the mixin will also generate property specific methods for those indexes.
Account.getByEmail(email, callback)
Account.listByRole(role, callback)
More about indexing
We can index in a variety of ways with Redis hashes and sorted sets. For example, we could explicitly define our unique email index like so:
Account.defineIndex({
type: 'hash',
key: 'accounts:email',
field: 'email',
value: '_id'
});
This tells the model to store an account's _id
property in a hash named accounts:email
with email as the field name. Because this is a very common use of the hash type index, the mixin also provides a helper method for defining unique indices:
Account.indexUnique('email');
This is equivalent to adding unique: true
to the property definition in our schema.
Sorted set indices get a little more interesting. We have a great deal of flexibility in how we can index our models. For example, suppose we have a Video
model that has a category
property and a likes
property. We want to retrieve a list of videos for a specific category, sorted by the number of likes.
Video.defineIndex({
type: 'sorted',
key: ['videos:#: