redis-collections

0.0.39 • Public • Published

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: new RedisSet('users'),
    settings: new RedisIdToMap('user:${userId}:settings'),
    friends: new RedisIdToSet('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 = userIds.map(userId => ({
    id: userId,
    settings: users.settings.getMap(userId),
    friends: users.friends.getList(userId)
}))
const userInfoList = await store.promise(loadAllUserInfo)

Collections (for now)

Example (single)

simple-example.js: (Online version)

// const redis = require('redis')
const redis = require('fakeredis')
const {Store, RedisSet} = require("redis-collections")
 
const store = new Store(redis.createClient())
 
const numbers = new RedisSet('numbers')
 
store.promise(numbers.add('two'))
    .then(() => store.promise(numbers.add('one')))
    .then(() => store.promise(numbers.getList()))
    .then(list => {
        console.log("list = ", list)
    })

Should print:

list = [ 'one''two' ]

Example (multiple)

better-example.js: (Online version)

const store = new Store(redis.createClient())
const users = {
    list: new RedisSet('users'),
    settings: new RedisIdToMap('user:${userId}:settings'),
    friends: new RedisIdToSet('user:${userId}:friends')
}
 
const createUsers = [
    users.list.addAll(["U1", "U2"]),
    users.settings.setAll("U1", {name: "USER1"}),
    users.settings.setAll("U2", {name: "USER2"}),
    users.friends.add("U1", "U2"),
    users.friends.add("U2", "U1")
]
await store.promise(createUsers)
 
const userIds = await store.promise(users.list.getList())
const loadList = userIds.map(userId => ({
    id: userId,
    settings: users.settings.getMap(userId),
    friends: users.friends.getList(userId)
}))
const userList = await store.promise(loadList)

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.

License

MIT

Package Sidebar

Install

npm i redis-collections

Weekly Downloads

3

Version

0.0.39

License

MIT

Unpacked Size

150 kB

Total Files

41

Last publish

Collaborators

  • tamasmajer