@ossdeveloper/couchpromise
v1.6.1
Published
Couchbase SDK for node.js - with promises
Downloads
8
Maintainers
Readme
@ossdeveloper/couchpromise - As Promised : Under active development
couchbase SDK for Node.js - with promises
Prerequisites
- Ensure node-gyp is installed globally(npm install -g node-gyp)
- node.js version >= 10.13.0
Installation
- npm install --save @ossdeveloper/couchpromise
Above command installs couchbase and 'q' promise library as dependencies.
Assumptions
- Your couchbase cluster username is 'username' and password is 'password'
- You have a bucket named 'default' in your couchbase cluster
List of operations - as promised
** You can use this library like regular couchbase node.js sdk and, use below methods for promises **
- Settingup buckets(DB connection) : setupBuckets
- insert : insertSingle
- upsert : upsertSingle
- replace : replaceSingle
- remove : removeSingle
- get : getSingle
- Execute N1QL : getN1QLResults
- getAndTouch : getAndTouchSingle
Usage
1. To setup Buckets
Parameters
username : String
password : String
aryBuckets : array -> an array of bucket names in your couchbase cluster. each item(bucket name) is a string
aryIps : array -> an array of IP addresses of your couchbase cluster. each item(ip) is a string
Example :
let db = require('@ossdeveloper/couchpromise').buckets
let buckets = {};
let username = 'username';
let password = 'password';
let aryBuckets = ['default','bucket2'];
let aryIPs = ['xxx.xxx.xxx.xxx','xxx.xxx.xxx.xxx','xxx.xxx.xxx.xxx'];
db.setupBuckets(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
buckets = db.getBuckets()
console.log(buckets)
}else{
console.error(result)
}
})
.catch((e) => {
console.error(e)
})
2. To insert a single record
let db = require('@ossdeveloper/couchpromise').buckets
let insertSingle = require('@ossdeveloper/couchpromise').insertSingle
cluster.openConnections(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
buckets = db.getBuckets()
let strKey = 'uniqueKey'
let objDocument = {"test": "test"}
let objOptions = {}
let objBucket = db.getBuckets()['default']
insertSingle(strKey, objDocument, objBucket, objOptions)
.then((result) => console.log(result))
.catch((e) => console.log(e))
}else{
console.error(result)
}
})
.catch((e) => console.error(e));
3. To upsert a single record
let db = require('@ossdeveloper/couchpromise').buckets
let upsertSingle = require('@ossdeveloper/couchpromise').upsertSingle
cluster.openConnections(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
buckets = db.getBuckets()
let strKey = 'uniqueKey'
let objDocument = {"test": "test"}
let objOptions = {}
let objBucket = db.getBuckets()['default']
upsertSingle(strKey, objDocument, objBucket, objOptions)
.then((result) => console.log(result))
.catch((e) => console.log(e))
}else{
console.error(result)
}
.catch((e) => {
console.error(e);
});
4. To replace a single record
let db = require('@ossdeveloper/couchpromise').buckets
let replaceSingle = require('@ossdeveloper/couchpromise').replaceSingle
cluster.openConnections(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
buckets = db.getBuckets()
let strKey = 'uniqueKey'
let objDocument = {"test": "test"}
let objOptions = {}
let objBucket = db.getBuckets()['default']
replaceSingle(strKey, objDocument, objBucket, objOptions)
.then((result) => console.log(result))
.catch((e) => console.error(e))
}else{
console.error(result)
}
})
.catch((e) => console.error(e))
5. To remove a single record
let db = require('@ossdeveloper/couchpromise').buckets
let removeSingle = require('@ossdeveloper/couchpromise').removeSingle
cluster.openConnections(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
buckets = db.getBuckets()
let strKey = 'uniqueKey'
let objDocument = {"test": "test"}
let objOptions = {}
let objBucket = cluster.getBuckets()['default']
removeSingle(strKey, objBucket)
.then((result) => console.log(result))
.catch((e) => console.error(e))
}else{
console.error(result)
}
})
.catch((e) => console.error(e))
6. To get a single record
let db = require('@ossdeveloper/couchpromise').buckets
let getSingle = require('@ossdeveloper/couchpromise').getSingle
cluster.openConnections(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
buckets = db.getBuckets()
let strKey = 'uniqueKey'
let objDocument = {"test": "test"}
let objOptions = {}
let objBucket = db.getBuckets()['default']
getSingle(dbKey, objBucket)
.then((result) => console.log(result))
.catch((e) => console.error(e))
}else{
console.error(result)
}
})
.catch((e) => {
console.error(e);
});
7. To execute N1QL Query and receive results
let db = require('@ossdeveloper/couchpromise').buckets
let getN1QLResults = require('@ossdeveloper/couchpromise').getN1QLResults
db.openConnections(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
let strQuery = 'select * from default limit 10' // To get 10 records from default bucket
let objBucket = db.getBuckets()['default']
getN1QLResults(strQuery, objBucket)
.then((result) => console.log(result))
.catch((e) => console.error(e))
}else{
console.error(result)
}
})
.catch((e) => {
console.error(e);
});
Multiple operations
8. To get Multiple records
let db = require('./index').buckets
let getSingle = require('@ossdeveloper/couchpromise').getSingle
cluster.openConnections(username, password,aryBuckets,aryIPs)
.then((result) =>{
if(result === true){
let strKey = ['key1','key2']
let objBucket = cluster.getBuckets()['default']
getMulti(strKey,objBucket)
.then((result) => {
console.log(result)
})
.catch((e) => {
console.error(e)
})
}else{
console.error(result)
}
.catch((e) => {
console.error(e);
});