forked from FULLALT/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarantool.js
60 lines (54 loc) · 1.7 KB
/
tarantool.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
48
49
50
51
52
53
54
55
56
57
58
59
60
import TarantoolDriver from 'tarantool-driver';
let instance = null;
class Tarantool {
constructor() {
const config = require('../config').default;
const {host, port, username, password} = config.tarantool || {host: 'localhost', port: 3301, username: 'guest', password: ''};
const connection = this.connection = new TarantoolDriver({host, port});
this.ready_promise = new Promise((resolve, reject) => {
connection.connect()
.then(() => connection.auth(username, password))
.then(() => resolve())
.catch(error => reject(error));
});
}
makeCall(call_name, args) {
return this.ready_promise
.then(() => this.connection[call_name].apply(this.connection, args))
.catch(error => {
console.error('Tarantool error', error.message);
if (error.message.indexOf('connect') >= 0)
instance = null;
return Promise.reject(error);
});
}
select() {
return this.makeCall('select', arguments);
}
delete() {
return this.makeCall('delete', arguments);
}
insert() {
return this.makeCall('insert', arguments);
}
replace() {
return this.makeCall('replace', arguments);
}
update() {
return this.makeCall('update', arguments);
}
eval() {
return this.makeCall('eval', arguments);
}
call() {
return this.makeCall('call', arguments);
}
upsert() {
return this.makeCall('upsert', arguments);
}
}
Tarantool.instance = function () {
if (!instance) instance = new Tarantool();
return instance;
};
export default Tarantool;