Skip to content

Commit 9de7760

Browse files
committed
update couchbase
1 parent bf5a6ee commit 9de7760

15 files changed

+243
-360
lines changed

package-lock.json

Lines changed: 174 additions & 306 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"body-parser": "^1.18.2",
3131
"compression": "^1.7.3",
3232
"cors": "^2.8.4",
33-
"couchbase": "^2.6.5",
33+
"couchbase": "^3.0.6",
3434
"create-hash": "^1.2.0",
3535
"eosjs": "^20.0.0",
3636
"eosjs-ecc": "^4.0.4",

src/database/couchbase.js

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
11
import config from '../util/config';
22

3-
import { promisifyAll } from 'bluebird';
4-
53
const couchbase = require('couchbase');
64

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,
4045
};
4146

47+
48+
49+
50+
51+
52+
53+
54+
55+
56+

src/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Blacklist from "./util/blacklist";
2727
import WebHookService from "./services/WebHookService";
2828
import FeatureFlags from "./services/FeatureFlags";
2929

30-
const bucket = couchbase('scatter');
30+
const bucket = couchbase.get('scatter');
3131

3232

3333
/********************************/

src/services/AppService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class AppService {
2424
}
2525

2626
static async getApps(){
27-
if(!inRam) inRam = (await bucket.get(bucketKey)).value;
27+
if(!inRam) inRam = (await bucket.get(bucketKey)).content;
2828
return inRam;
2929
}
3030

src/services/ExchangeService.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const COINSWITCH_KEY = config('COINSWITCH_KEY');
66
import couchbase from '../database/couchbase'
77
import PriceService from "./PriceService";
88
import {BANCOR_EOS_PAIRS, BANCOR_RELAYS} from "../data/bancor_relays";
9-
const bucket = couchbase('exchange');
9+
const bucket = couchbase.get('exchange');
1010

1111
const pairCaches = {};
1212

@@ -346,7 +346,7 @@ export default class ExchangeService {
346346
}
347347

348348
async getOrder(orderId){
349-
const original = await bucket.get(`order:${orderId}`).then(x => x.value).catch(() => null);
349+
const original = await bucket.get(`order:${orderId}`).then(x => x.content).catch(() => null);
350350
if(!original) return;
351351

352352
if(original.order.service === SERVICES.BANCOR_EOS){
@@ -364,12 +364,12 @@ export default class ExchangeService {
364364
}
365365

366366
static async cancelled(orderId){
367-
await bucket.remove(`order:${orderId}`).then(x => x.value).catch(() => null);
367+
await bucket.remove(`order:${orderId}`).then(x => x.content).catch(() => null);
368368
return true;
369369
}
370370

371371
static async accepted(orderId){
372-
const original = await bucket.get(`order:${orderId}`).then(x => x.value).catch(() => null);
372+
const original = await bucket.get(`order:${orderId}`).then(x => x.content).catch(() => null);
373373
original.accepted = true;
374374
await bucket.upsert(`order:${orderId}`, original).catch(() => null);
375375
return true;

src/services/ExplorerService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class ExplorerService {
2020
}
2121

2222
static async getApps(){
23-
if(!inRam) inRam = (await bucket.get(bucketKey)).value;
23+
if(!inRam) inRam = (await bucket.get(bucketKey)).content;
2424
return inRam;
2525
}
2626

src/services/FiatService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class FiatService {
2222
}
2323

2424
static async getConversions(){
25-
if(!inRam) inRam = (await bucket.get(bucketKey)).value;
25+
if(!inRam) inRam = (await bucket.get(bucketKey)).content;
2626
return inRam;
2727
}
2828

src/services/LanguageService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class LanguageService {
2424
}
2525

2626
static async getLanguages(namesOnly = false, name = null){
27-
if(!inRam) inRam = (await bucket.get(bucketKey)).value;
27+
if(!inRam) inRam = (await bucket.get(bucketKey)).content;
2828

2929
if(namesOnly) return inRam.map(x => x.name);
3030

src/services/NetworkService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class NetworkService {
2121
}
2222

2323
static async getNetworks(){
24-
if(!inRam) inRam = (await bucket.get(bucketKey)).value;
24+
if(!inRam) inRam = (await bucket.get(bucketKey)).content;
2525
return inRam;
2626
}
2727

src/services/PriceService.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class PriceService {
7777
static async getPrices(){
7878
await Promise.all(networks.map(async net => {
7979
if(!pricesInRam.hasOwnProperty(net)){
80-
pricesInRam[net] = (await bucket.get(net)).value;
80+
pricesInRam[net] = (await bucket.get(net)).content;
8181
return true;
8282
} else {
8383
return true;
@@ -131,7 +131,7 @@ export default class PriceService {
131131

132132
eosMainnetPrices.map(x => {
133133
const clone = JSON.parse(JSON.stringify(x))
134-
clone.price = parseFloat(parseFloat(EOS.price * x.price).toFixed(8));
134+
if(EOS) clone.price = parseFloat(parseFloat(EOS.price * x.price).toFixed(8));
135135
result[`eos:${x.contract}:${x.symbol}:${x.chainId}`.toLowerCase()] = convertToMultiCurrency(clone);
136136
})
137137

@@ -219,7 +219,7 @@ export default class PriceService {
219219

220220
static async getPriceTimeline(id){
221221
return bucket.get(`prices:timeline:${id}`).then(x => {
222-
return x.value
222+
return x.content
223223
}).catch(err => {
224224
console.error(err);
225225
return {};
@@ -232,7 +232,7 @@ export default class PriceService {
232232

233233
let pricesRaw = await this.getV2Prices(true, false);
234234
pricesRaw = Object.keys(pricesRaw).reduce((acc,x) => {
235-
acc[x] = pricesRaw[x].price;
235+
if(pricesRaw[x]) acc[x] = pricesRaw[x].price;
236236
return acc;
237237
}, {});
238238
let prices = await this.getPriceTimeline(id);

src/services/ProxyService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class ProxyService {
2020
}
2121

2222
static async getProxies(){
23-
if(!inRam) inRam = (await bucket.get(bucketKey)).value;
23+
if(!inRam) inRam = (await bucket.get(bucketKey)).content;
2424
return inRam;
2525
}
2626

src/services/TokenService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class TokenService {
2525
}
2626

2727
static async getTokens(){
28-
if(!inRam) inRam = (await bucket.get(bucketKey).catch(() => ({value:[]}))).value;
28+
if(!inRam) inRam = (await bucket.get(bucketKey).catch(() => ({content:[]}))).content;
2929
return inRam;
3030
}
3131

src/services/VersionService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class VersionService {
2020
}
2121

2222
static async getVersion(){
23-
if(!inRam) inRam = (await bucket.get(bucketKey)).value;
23+
if(!inRam) inRam = (await bucket.get(bucketKey)).content;
2424
return inRam;
2525
}
2626

src/services/WebHookService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class WebHookService {
1010

1111
static setBucket(_b){
1212
bucket = _b;
13-
bucket.get(bucketKey).then(x => inRam = x.value || []).catch(err => {
13+
bucket.get(bucketKey).then(x => inRam = x.content || []).catch(err => {
1414
if(err.code === 13) bucket.upsert(bucketKey, inRam);
1515
console.log('ERRRRR', err);
1616
});

0 commit comments

Comments
 (0)