Radredis
Radredis is a node data adapter for redis. It is not a full ORM but a simple opinionated interface for storing application data in redis.
Goals
- Use json-schema for data validation
- No classes. No
new
keyword. Factory functions. - Before/After transforms
- Promise based
- Pipelining. All redis commands are executed via transaction if possible.
- iodredis under the hood.
Setup
const Radredis =const redisOpts = db: 15 keyPrefix: 'your-app:'const transforms = modeltitle = modeltitleconst schema =title: 'Post'type: "object"properties :title:type: 'string'author_id:type: 'number'const Post =
Redis configuration
The final three arguments to Radredis
are passed through to ioredis
. See their documentation for more info: https://github.com/luin/ioredis/blob/master/API.md#new_Redis_new
Examples:
// Radredis(schema, transforms, [port], [host], [options])const Post =const Post =const Post =
All
Postall// => [ post, post, post, ...]// All with limit and offsetPostall limit: 2 offset: 10// => [ post, post ]// All by index// Note order is currently always descendingPostall index: 'author_id'// Return only certain propertiesPostall properties: 'author_id'
Range
Allows for retrieving records by range of values for an attribute.
- Results ordered in descending value
- Results are inclusive of
min
andmax
min
,max
, andindex
are required
Post// => [ post, post, post, ...]// Also accepts limit and offsetPost// => [ post, post ]// Return only certain propertiesPost
Scan
const stream = Poststreamstream
// Return only specific fieldsconst stream = Poststream// => { id: 1, author_id: 1 },// => { id: 2, author_id: 2 }// ...
Count
Post// => 42// Count by indexPost// => 35
Find
// Find onePost// => { id: 1, title: 'A title', author_id: 1 }// Find multiplePost// => [ { id: 1, title: 'A title', author_id: 1 }, { id: 2, title: 'Another title', author_id: 2 } ]
Create
Post// => { id: 1, title: 'Redis rocks' }
Update
Does a partial update of the model. Basically Object.assign(old, new)
PostPost// => {id: 1, title: 'A new title', author: 5 }
Replace
Completely replaces the model in the database with the provided attributes
PostPost// => {id: 1, title: 'A new title'}
Delete
Post// => {id: 1, title: 'A new title'}
Versions
See versioning below. Will return all versions of an object including the most recent one.
PostPost// => [ {id: 1, _v: 1, title: 'foo'}, {id: 1, _v: 2, title: 'bar'} ]
Validation
NOT IMPLEMENTED
Validation is handled by json-schema (http://json-schema.org/examples.html) Functions return promise rejections if bad data
Indexing
Specify indexed attributes inside json schema:
const schema =title: 'Post'type: "object"properties :author_id:type: 'number'index: true
Versioning
Specify attributes that should create a new version when changed inside json schema. The version number is in the _v
key of the record
const schema =title: 'Post'type: "object"properties :author_id:type: 'number'index: truetitle:type: 'string'version: true
Transforms
// Note oldAttributes will be undefined on create{ /*do stuff to attributes*/}{ /*do stuff with attributes*/}
Errors
radredis will throw a RecordNotFound
error on any operation where the record does not exist.
// Can also do://import { default as radredis, RecordNotFound } from 'radredis'const Post =Post