|
const Couchbase = require('couchbase'); |
|
const Promise = require('bluebird'); |
|
|
|
class Bucket { |
|
constructor(options) { |
|
this.options = options; |
|
} |
|
|
|
async connect() { |
|
const { |
|
clusterUrl, username, password, bucketName, |
|
} = this.options; |
|
|
|
const cluster = new Couchbase.Cluster(clusterUrl); |
|
cluster.authenticate(username, password); |
|
|
|
const bucket = await new Promise((resolve, reject) => { |
|
const bkt = cluster.openBucket(bucketName, (err) => { |
|
if (err) { |
|
reject(err); |
|
} else { |
|
resolve(bkt); |
|
} |
|
}); |
|
}); |
|
const manager = bucket.manager(); |
|
|
|
this.bucket = bucket; |
|
|
|
// METHODS come from here: |
|
// https://docs.couchbase.com/sdk-api/couchbase-node-client/Bucket.html |
|
this.get = Promise.promisify(bucket.get, { context: bucket }); |
|
this.upsert = Promise.promisify(bucket.upsert, { context: bucket }); |
|
this.insert = Promise.promisify(bucket.insert, { context: bucket }); |
|
this.replace = Promise.promisify(bucket.replace, { context: bucket }); |
|
this.remove = Promise.promisify(bucket.remove, { context: bucket }); |
|
this.getMulti = Promise.promisify(bucket.getMulti, { context: bucket }); |
|
this.query = Promise.promisify(bucket.query, { context: bucket }); |
|
this.listAppend = Promise.promisify(bucket.listAppend, { context: bucket }); |
|
this.listGet = Promise.promisify(bucket.listGet, { context: bucket }); |
|
this.listPrepend = Promise.promisify(bucket.listPrepend, { context: bucket }); |
|
this.listRemove = Promise.promisify(bucket.listRemove, { context: bucket }); |
|
this.listSet = Promise.promisify(bucket.listSet, { context: bucket }); |
|
this.listSize = Promise.promisify(bucket.listSize, { context: bucket }); |
|
this.mapAdd = Promise.promisify(bucket.mapAdd, { context: bucket }); |
|
this.mapGet = Promise.promisify(bucket.mapGet, { context: bucket }); |
|
this.mapRemove = Promise.promisify(bucket.mapRemove, { context: bucket }); |
|
this.mapSize = Promise.promisify(bucket.mapSize, { context: bucket }); |
|
this.queuePop = Promise.promisify(bucket.queuePop, { context: bucket }); |
|
this.queuePush = Promise.promisify(bucket.queuePush, { context: bucket }); |
|
this.queueSize = Promise.promisify(bucket.queueSize, { context: bucket }); |
|
this.setAdd = Promise.promisify(bucket.setAdd, { context: bucket }); |
|
this.setExists = Promise.promisify(bucket.setExists, { context: bucket }); |
|
this.setRemove = Promise.promisify(bucket.setRemove, { context: bucket }); |
|
this.setSize = Promise.promisify(bucket.setSize, { context: bucket }); |
|
this.touch = Promise.promisify(bucket.touch, { context: bucket }); |
|
this.unlock = Promise.promisify(bucket.unlock, { context: bucket }); |
|
this.ping = Promise.promisify(bucket.ping, { context: bucket }); |
|
this.getReplica = Promise.promisify(bucket.getReplica, { context: bucket }); |
|
this.getAndTouch = Promise.promisify(bucket.getAndTouch, { context: bucket }); |
|
this.getAndLock = Promise.promisify(bucket.getAndLock, { context: bucket }); |
|
this.counter = Promise.promisify(bucket.counter, { context: bucket }); |
|
this.append = Promise.promisify(bucket.append, { context: bucket }); |
|
this.flush = Promise.promisify(manager.flush, { context: manager }); |
|
this.mutateIn = (...args) => { |
|
const mutateBuilder = bucket.mutateIn(...args); |
|
mutateBuilder.execute = Promise.promisify(mutateBuilder.execute, { context: mutateBuilder }); |
|
return mutateBuilder; |
|
}; |
|
this.lookupIn = (...args) => { |
|
const lookupBuilder = bucket.lookupIn(...args); |
|
lookupBuilder.execute = Promise.promisify(lookupBuilder.execute, { context: lookupBuilder }); |
|
return lookupBuilder; |
|
}; |
|
|
|
|
|
this.diagnostics = Promise.promisify(bucket.diagnostics, { |
|
context: bucket, |
|
}); |
|
} |
|
|
|
async disconnect() { |
|
this.bucket.disconnect(); |
|
// wait a little. unfortunately bucket.disconnect doesnt take |
|
// a callback and does not return a promise even though it's |
|
// an async behavior. instead we just give it a little time |
|
// to do it's thing. |
|
await new Promise(resolve => setTimeout(resolve, 500)); |
|
} |
|
} |
|
|
|
module.exports = Bucket; |