// this example does not need any redis implementation, it's a mock.
// load the store and the collections we will use
const {Store, RedisSet, RedisIdToSet, RedisIdToMap} = require("redis-collections").Mock
// create a store to run the queries
const store = new Store()
// create a shared data container for the mock collections
const data = {}
// define database structure with a shared (mock) data container
const users = {
list: new RedisSet({key: 'users', data}),
settings: new RedisIdToMap({key: 'user:${userId}:settings', data}),
friends: new RedisIdToSet({key: 'user:${userId}:friends', data})
}
// plan to fill the db
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")
]
// execute plan
await store.promise(createUsers)
// query the user ids
const userIds = await store.promise(users.list.getList())
// plan to load structured data (id is predefined, settings and friends needs filling out)
const loadList = userIds.map(userId => ({
id: userId,
settings: users.settings.getMap(userId),
friends: users.friends.getList(userId)
}))
// execute the plan
const userList = await store.promise(loadList)
// print every step into one string
const print = []
print.push("userIds = " + JSON.stringify(userIds))
print.push("userList = " + JSON.stringify(userList))
print.push("data = " + JSON.stringify(data))
console.log(print.join("\n\n"))
Created from: https://runkit.com/npm/redis-collections