Secondary indexing for Redis
$ npm install sein-redis
const Redis = require('ioredis');
const seinRedis = require('sein-redis');
const redis = new Redis();
await seinRedis.setClient(redis);
seinRedis.setPrefix('example');
class UserModel extends seinRedis.Model {
constructor() {
super();
this.modelName = 'User';
this.setDefinitions([
{
name: 'id',
primaryKey: true,
},
{
name: 'email',
uniqueIndex: true,
},
{
name: 'typeId',
index: true,
},
]);
}
getByEmail(email) {
return this.getByUniqueIndex({ email }, true);
}
getByType(typeId) {
return this.getByIndex({ typeId }, true);
}
}
const isSet = await userModel.set({
id: 'd982398j2398u',
name: 'Test User',
email: '[email protected]',
typeId: 1,
});
let user = await userModel.get('d982398j2398u');
user = await userModel.getByEmail('[email protected]');
const users = await userModel.getByType(1);
MIT