Redis-based User Database Structure
We use a simple set of key-value pairs to store any individual user.
Any user has
- an id
- an email address
- a role (either
'user'or'admin') - a password (saved as a hash)
ID, email and role are accessible under _USERS/[id]/* and the id can be
"reverse looked up" based on the email via _USER_EMAILS/[email]:
This way, it's possible to perform all CRUD user operations with four Redis commands:
SET [key] [value]- set or add some valuesGET [key]- get some valuesDEL [key]- delete valuesKEYS [pattern]- lookup all keys that match the[pattern]
List of users
The only thing that may seem difficult with this key-value based storage, at
first, is listing all users. But we can easily achieve this using the KEYS
command:
const raw = await references.redisConn.client.KEYS('_USERS/*/email');
const userIds = raw.map(s => s.split('/')[1]);