redis-odmd
v0.2.7
Published
ODM like for redis cache server.
Downloads
16
Maintainers
Readme
For some use cases we need to have a ODM
like structure in order to work with our
our stored hash data in redis
. this module give us all the main methods for that but there
is't already any kind of schemas
to define or validations
.
npm install redis-odmd
Example
const Dictionary = require('redis-odmd');
const options = {
prefix: 'online', // Prefix for your dictionary
redis: {
port: 6379
}
}
// create your dictionary object
const online_users = Dictionary(options);
let data = {
name: 'John',
family: 'Doe',
address: {
country: 'Iran',
city: 'Shiraz'
}
};
// Add data to your dictionary (store hash string)
online_users.set('123', data).then(result => {
console.log(result) // OK
}).catch(err) {
throw(err);
}
// get a field of the user hash
online_users.get('123', 'address.city').then(user => {
console.log(user.data.address.city)
})
options
Is an object which:
prefix
prefix of dictionarykeySeperator
for seperatingprefix
andid
asprefix<keySeperator>id
.redis
all redis options
methods
set(id, data<Object|String>)
returnsOK
like redis.get(id)
returns an dictionary object.get(id, property)
returns the property from stored hash.Example
// stored hash const user = { name: 'John', family: 'Doe' } online_users.get(id, 'name').then(result => { console.log(result.data.name) // John })
getAll()
returns an object contains all hashes withkey:hash
.
online_users.getAll().then(users => {
// all users
/*
{
id_1: {
name: 'John',
family: 'Doe'
},
...
}
*/
})
delete(id)
redis return style.exists(id)
redis return style.count()
number of stored hashes in dictionary.create(id, data<Object|String>)
returns an object of dictionaryupdate()
should be called afterget
and willset
the hash with last changes. returns a promise.Example
online_users.get('123').then(user => { user.data.new_field = 'new_field'; user.update() })