Socket
Socket
Sign inDemoInstall

pd-redis-model

Package Overview
Dependencies
32
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pd-redis-model

To facilitate database operation in Redis


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

pd-redis-model

To facilitate database operation in Redis

Installation

npm install -save pd-redis-model

Tutorial

Create a model

var User = require('pd-redis-model')('user'); 

'user' is the saved name of the model, all capital letters are converted into lower-case letters

C.R.U.D

To create:
var profile = {
   email : 'myletter@email.com', 
   password : 'abc123'
};
var creatingPromise = User.create(profile); 

The returning value of User.create is a q.Promise The newly created record will have a sequence id which is unique of the type. It can be fetched by using 'then' of the promise as follows

creatingPromise.then(function(sid){
   //do something to the returned sid...
});
To read:
To find one record
var readingPromise = User.findBySid('1')

Again the returning value of User.findBySid is a q.Promise. The record information can be read by using 'then' as follows

readingPromise.then(function(rec){
   // => rec's content is: {  'pd-sid' : '1', email: 'myletter@email.com' ....}
});
To find a list of records
var option = {
  latest: (new Date()).getTime(), //the ending time point of list
  earliest : 0                    //the starting time point of list
}
var listPromise = User.range(option);

It will return all available records in a list in descending order of time. They can be reached as follows

listPromise.then(function(list){
   // list's content ==>  
   // [
   //    {'pd-sid' : 1 ,  email : 'myletter1@email.com' ... }, 
   //    {'pd-sid' : 2,  email: 'myletter2@email.com' ...}
   //    .....
   // ]
});
To get total amount of a model
var amountPromise = User.amount();
amountPromise.then(function(amount){
  // amount ==> 1
});
To update:
var profile = {
  'pd-sid' : 1
  password : '123abc', 
  status : 'online'
};
var updatePromise = User.modify(profile);

The 'pd-sid' which is the auto-increase id field can never be updated but it should be assigned value to specify which record is to be updated.

To remove:
var removePromise = User.remove('1') //'1' is the user record's sid

More about CRUD

For more details, check Base Record

Set model fields

Set unique fields
User.setUniqueDef('account-name', ['email']);
var readPromise = User.withUnique('account-name').findBy('myhost@email.com');

check Set unique fields for more details

Set non-empty fields
User.needInputOf(['email', 'password'])
User.eachInputOf('email').mustMatch(function(val){
   return require('validator').isEmail(val);
})
User.eachInputOf('password').mustMatch(/^\w{6,30}$/);

check Set non-empty fields for more details

Set relationship

var Posts = require('pd-redis-model')('post');
User.mother(Posts);
var userSid = '1';
var postProfile = {
   content : 'Hello'
};
User.PostOwner(userSid).bear(postProfile);
var postSid = '12';
User.PostOwner(userSid).hasKid(postSid);
User.PostOwner(userSid).findKids({
  latest: (new Date()).getTime(),
  earliest: 0
});
Posts.UserKid(postSid).getParent();

check Set parenthood for more details

Keywords

FAQs

Last updated on 22 Jul 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc