redis-collections
Collection based views for Redis. Inspired by clean code principles.
Goals
- Structure the database.
- Use collections instead of commands.
- Work with IDs instead of keys.
- Composite queries.
- Promises.
- Clean code.
Structured database
Split your database into smaller sections, and describe them with collections. For example group the user related collections together like this:
const users = list: 'users' settings: 'user:${userId}:settings' friends: 'user:${userId}:friends'
Composite queries
Instead of loading data separately and then mapping them together, you can just tie your queries together into a single self-documenting structure.
const loadAllUserInfo = userIdsconst userInfoList = await store
Collections (for now)
- RedisSet manages redis sets with a fixed key (like "users")
- RedisMap manages redis hash maps with a fixed key (like "settings")
- RedisSortedSet manages redis sorted sets with a fixed key (like "users-by-name")
- RedisIdToValue manages redis string values with an ID in the key (like "user:${userId}:name")
- RedisIdToSet manages redis sets with an ID in the key (like "user:${userId}:friends")
- RedisIdToMap manages redis hash maps with an ID in the key (like "user:${userId}:settings")
- RedisIdToSortedSet manages redis sorted sets with an ID in the key (like "user:${userId}:friends-by-distance")
- RedisIdPairToMap manages redis hash maps with two IDs in the key (like "user:${userId}:friend:${userId}:relationship-details")
Example (single)
simple-example.js: (Online version)
// const redis = require('redis')const redis = const Store RedisSet = const store = redis const numbers = 'numbers' store
Should print:
list = [ 'one', 'two' ]
Example (multiple)
better-example.js: (Online version)
const store = redisconst users = list: 'users' settings: 'user:${userId}:settings' friends: 'user:${userId}:friends' const createUsers = userslist userssettings userssettings usersfriends usersfriendsawait store const userIds = await storeconst loadList = userIdsconst userList = await store
Output:
createUsers = 'redis' 'sadd' 'users' 'U1' 'U2' 'redis' 'hmset' 'user:U1:settings' name: 'USER1' 'redis' 'hmset' 'user:U2:settings' name: 'USER2' 'redis' 'sadd' 'user:U1:friends' 'U2' 'redis' 'sadd' 'user:U2:friends' 'U1' userIds = 'U1' 'U2' loadList= id: 'U1' settings: 'redis' 'hgetall' 'user:U1:settings' friends: 'redis' 'smembers' 'user:U1:friends' id: 'U2' settings: 'redis' 'hgetall' 'user:U2:settings' friends: 'redis' 'smembers' 'user:U2:friends' userList = id: 'U1' settings: name: 'USER1' friends: 'U2' id: 'U2' settings: name: 'USER2' friends: 'U1'
There is a mock implementation for the collections so you can test the contents of the database any time. The example above created this database: (Online version)
"users": "U1" "U2" "user:U1:settings": "name": "USER1" "user:U2:settings": "name": "USER2" "user:U1:friends": "U2" "user:U2:friends": "U1"
Install
npm install redis-collections
Testing
Testing needs node 7.2.1
npm test
Status
Needs tests, more collections, use cases, etc.