-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCouchbaseNode.js
More file actions
56 lines (46 loc) · 1.7 KB
/
CouchbaseNode.js
File metadata and controls
56 lines (46 loc) · 1.7 KB
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
48
49
50
51
52
53
54
55
56
const SEP_CHAR = '.';
const couchbase = require('couchbase');
const functions = {
find: require("./functions/find"),
count: require("./functions/count"),
insert: require("./functions/insert"),
update: require("./functions/update")
};
global._couchbase_clients = {};
function getClient(DBConfigs) {
return new Promise((resolve, reject) => {
if (global._couchbase_clients.hasOwnProperty(DBConfigs.bucket)) {
resolve(global._couchbase_clients[bucket]);
} else {
global._couchbase_clients = (new couchbase.Cluster(DBConfigs.url)).openBucket(DBConfigs.bucket);
resolve(global._couchbase_clients);
}
});
}
class CouchbaseNode {
constructor(name, children, args) {
this.name = name;
this.args = args;
this.children = children;
}
getName() {
return this.name;
}
//noinspect JSAnnotator
async eval(context, opts) {
const tokenizedName = this.name.split(SEP_CHAR);
const [ dbtype, DBName, bucket, operation ] = tokenizedName;
if (!functions.hasOwnProperty(operation)) {
throw `Operation Error: operation ${operation} does not exist / is not supported`;
}
if (!opts.hasOwnProperty('Couchbase')) {
throw `Missing configs: Couchbase settings are missing`;
} else if (!opts['Couchbase'].hasOwnProperty(DBName)) {
throw `Missing configs: Couchbase settings for DB ${DBName} are missing`;
}
const DBConfigs = opts['Couchbase'][DBName];
const db = await getClient(bucket, DBConfigs);
return await functions[operation](bucket, db, this.args);
}
}
module.exports = CouchbaseNode;