Nodejs Couchbase Module
npm install "git+https://github.com/Tlantic/couchbaselib.git#master"
var DB = require('couchbaselib');
Example:
DB.initConnection(config.db.host);
Example:
DB.getBucket('mrs', function(err, msg){
if(err)
{
callback(err);
}
else{
callback(null, msg);
}
});
);
var couchbaselib = require('couchbaselib');
var Schema = couchbaselib.Schema;
var userSchema = new Schema('user',{properties: {
firstname: {
type: 'string',
minLength: 2,
maxLength: 15
},
lastname: {
type: 'string',
minLength: 2,
maxLength: 25,
},
state: {
type: 'string',
minLength: 2,
maxLength: 15,
default: "A"
}
},
required: ['firstname']});
var methods = {
getRefreshToken: function(key, cb) {
couchbaselib.db.query('default', 'dev_auth', 'by_all', cb, {
key: key
});
}
};
exports = module.exports = couchbaselib.model('User', userSchema, 'default', methods);
couchbaselib.model(Name, Schema, bucketName, Methods);
- _uId: uuid.v4()
- _type: document type (ex: user)
- _createDate: unix date format
- _updateDate: unix date format
- findById: function(bucket, key, cb);
- save: function(callback);
- update: function(id, data, callback);
- remove: function(id, callback)
On start app
require('../application/user.model');
###Example
var User = couchbaselib.get('User');
var user = new User({firstname:'John', lastname:"Doe"});
user.save(function(err, data){
if(err)
res.send(err);
User.getRefreshToken();
User.findById('default', data._type ,data._uId ,function(err, data){
httpResponse.success(data, res);
});
});
Example:
DB.insert(bucketName, key, data, function(err, result, retriveDocument) {
if (err)
cb(err);
cb(null, result);
}, true);
Example:
DB.update(bucketName, key, data, function(err, result) {
if (err)
cb(err);
cb(null, result);
}, true);
Example:
DB.remove(bucketName, key, function(err, result) {
if (err)
cb(err);
cb(null, result);
}, true);
Example:
DB.get(bucketName, key, function(err, result) {
if (err)
cb(err);
cb(null, result);
});
Example:
DB.query('default', 'dev_user', 'by_type', function (err, result) {
if (err){
cb(err);
}
cb(null, result);
});
viewQueryOptions:{
stale: couchbase.ViewQuery.Update.BEFORE,
skip : null,
limit : null,
order : null,
reduce : null,
group : null,
group_level : null,
key : null,
keys : null,
range : null, //{start:0, end:10, inclusive_end:true} Specifies a range of keys to retrieve from the index.
id_range : null, //{start:0, end:10} Specifies a range of document id's to retrieve from the index.
include_docs : null, //Flag to request a view request include the full document value.
full_set : null //Flag to request a view request accross all nodes in the case of
}
Example:
DB.getN1qlQuery('default', "SELECT * FROM bee where type='user'", function (err, result) {
if (err) {
cb(err);
}
cb(null, result);
});