|
1 | 1 | import config from '../util/config';
|
2 | 2 |
|
3 |
| -import { promisifyAll } from 'bluebird'; |
4 |
| - |
5 | 3 | const couchbase = require('couchbase');
|
6 | 4 |
|
7 |
| -export const n1ql = couchbase.N1qlQuery; |
8 |
| - |
9 |
| -const cluster = new couchbase.Cluster(config('COUCHBASE_HOST')); |
10 |
| -cluster.authenticate(config('COUCHBASE_USERNAME'),config('COUCHBASE_PASSWORD')); |
11 |
| - |
12 |
| -export default bucketName => { |
13 |
| - const bucket = cluster.openBucket(bucketName); |
14 |
| - promisifyAll(bucket); |
15 |
| - |
16 |
| - const bucketAdditions = { |
17 |
| - existsAsync:(key) => { |
18 |
| - return new Promise((resolve, reject) => { |
19 |
| - bucket.getAsync(key) |
20 |
| - .then(res => resolve(true)) |
21 |
| - .catch(error => { |
22 |
| - if(error.code === 13) resolve(false); |
23 |
| - else throw new Error(error); |
24 |
| - }) |
25 |
| - }) |
26 |
| - } |
27 |
| - }; |
28 |
| - |
29 |
| - const bucketWithAdditions = Object.assign(bucket, bucketAdditions); |
30 |
| - |
31 |
| - // Sets up a proxy to turn all methods into async using bluebird |
32 |
| - return new Proxy(bucketWithAdditions, { |
33 |
| - get(b, method){ return (...args) => { |
34 |
| - // Mutations return the MutationBuilder and should not become async. |
35 |
| - if(method.indexOf('mutate') > -1) return bucket[method](...args); |
36 |
| - // Everything else will become async |
37 |
| - else return bucket[`${method}Async`](...args); |
38 |
| - } } |
39 |
| - }) |
| 5 | + |
| 6 | +const cluster = new couchbase.Cluster(config('COUCHBASE_HOST'), { |
| 7 | + username: config('COUCHBASE_USERNAME'), |
| 8 | + password: config('COUCHBASE_PASSWORD'), |
| 9 | +}); |
| 10 | + |
| 11 | +let instances = {}; |
| 12 | + |
| 13 | +const query = (queryString, model = null) => { |
| 14 | + // Replaces placeholder param. |
| 15 | + queryString = queryString.replace(/BUCKET_NAME/g, '`'+BUCKET_NAME+'`'); |
| 16 | + |
| 17 | + return cluster.query(queryString, { |
| 18 | + scanConsistency: couchbase.QueryScanConsistency.RequestPlus |
| 19 | + }).then(({meta, rows}) => { |
| 20 | + if(!rows) return []; |
| 21 | + // Fix for couchbase stupidness |
| 22 | + rows = rows.map(x => x[BUCKET_NAME] ? x[BUCKET_NAME] : x); |
| 23 | + rows = rows.map(x => x.data); |
| 24 | + |
| 25 | + return rows.map(x => { |
| 26 | + if(model) return model.fromJson(x); |
| 27 | + return x; |
| 28 | + }) |
| 29 | + }).catch(err => { |
| 30 | + console.error(err); |
| 31 | + return null |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +const get = (bucket_name) => { |
| 36 | + if(instances[bucket_name]) return instances[bucket_name]; |
| 37 | + |
| 38 | + instances[bucket_name] = cluster.bucket(bucket_name).defaultCollection(); |
| 39 | + return instances[bucket_name]; |
| 40 | +}; |
| 41 | + |
| 42 | +export default { |
| 43 | + query, |
| 44 | + get, |
40 | 45 | };
|
41 | 46 |
|
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
0 commit comments