pd-redis-base-record v0.1.8
pd-redis-base-record
Simple ORM record model with auto-increment sid
Installation
$ npm install -save pd-redis-base-record
To modify: Model.modify(JSON)
var profileToModify = {
'pd-sid' : '12' ,
name : 'Jane Doe',
gender : 'female'
};
User.modify(profileToModify).then(function(){
//do something after modification
});
'pd-sid' is not a changeable field, but it has to be assigned to specify which record to be modified.
To remove: Model.remove(sid)
var sid = '12';
User.remove(sid).then(function(){
//do something after removal
});
To read
To get total amount of records of a model: Model.amount()
User.amount().then(function(amount){
//do something to amount
});
To get a record by specifying sid: Model.findBySid(sid)
User.findBySid(12).then(function(record){
//record => { email: 'myletter@email.com', name: 'Jane Doe', updatedAt : '12345678901' }
//Because the data is modified just now, so it automatically got 'updatedAt' field
});
To get a list of records: Model.range(option)
User.range({
latest: (new Date()).getTime(), //* the ending time point of list
earliest: 0 , //* the starting time point of list
limit : [0, 50], //(optional)[start, offset]
}).then(function(records){
//records:
// [
// {'pd-sid' : 1 , email : 'myletter1@email.com' ... },
// {'pd-sid' : 2, email: 'myletter2@email.com' ...}
// .....
// ]
});
To decide if a record is brand new: Model.checkAbsence(sid)
User.checkAbsence(sid).then(function(){
//record not found
}).fail(function(err){
var announcer = require('pd-api-announcer');
if(announcer.isClientErrorFor(err, 'user', 'taken')) {
//record already existed
}
});
###To decide if a record already existed: Model.checkPresence(sid)
User.checkPresence(sid).then(function(){
//record found
}).fail(function(err){
var announcer = require('pd-api-announcer');
if(announcer.isClientErrorFor(err, 'user', 'gone')){
//record not found
}
});
To lock
To lock a type of records: Model.lock.sidSet(onLockCallback, expiredAfterMilliseconds)
Where the lock is used, every time the same routine visits the locked sid-set, if there is still a locked routine, it will wait until the previous routine is done
var onLockCallback = function(){
return User.create({
email: 'myletter@email.com',
password: 'abc123'
});
};
User.lock.sidSet(onLockCallback); //by default it will expire after 15000 milliseconds
If asynchronous operation is to be added in onLockCallback, the return value of onLockCallback should be a q.Promise object, so that the lock will wait until all operations are over then release the lock.
The returning value of User.lock.sidSet() is also a q.Promise, all code in User.lock.sidSet().then() will be executed after the lock is released
User.lock.sidSet(function(){...}).then(function(){
//do something after the lock is released
});
To lock one record: Model.lock.dataForSid(sid, onLockCallback, expiredAfterMilliseconds)
Similar to lock.sidSet(), but it only locks operations on a single record, or to be specific, dependent on one sid of a type of records
var sid = '12';
var onLockCallback = function(){
return User.modify({
'pd-sid' : sid,
email: 'myletter@email.com',
password: 'abc123'
});
};
User.lock.dataForSid(sid, onLockCallback);