-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict';
/**
* Integrate Couchbase with Koa JS
* to access all your buckets in your middleware
* @param {object} options
*
* Example Options:
*
* const couchBaseOptions = {
* connections: [
* {
* bucket: 'sample-bucket1'
* }
* ],
* host: "localhost",
* username: "Administrator",
* password: "mypassword"
* }
*
*/
module.exports = createCouchbaseConnections;
var couchbase = require('couchbase');
function createCouchbaseConnections(options) {
let connectionUrl = `couchbase://${options.host}`;
var cluster = new couchbase.Cluster();
cluster.authenticate(options.username, options.password);
// this will contain all the bucket references
let connections = {}
// Will be creating the Bucket Instances once here
options.connections.map((settings) => {
connections[settings.bucket] = cluster.openBucket(settings.bucket);
})
// middleware function to attach all the opened buckets
// to the Request Context every time
return async (ctx, next) => {
ctx.couchbase = connections;
await next();
};
}