-
Notifications
You must be signed in to change notification settings - Fork 4
/
es.js
82 lines (73 loc) · 2.26 KB
/
es.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const EventEmitter = require('events').EventEmitter;
const requestBuilder = require('./util/request.js');
const configManager = require('./util/globalConfig.js');
const Cluster = require('./src/cluster/cluster.js');
const Entity = require('./src/entity.js');
const Condition = require('./src/esCondition.js');
const Aggs = require('./src/esAggs.js');
const Range = require('./src/esRange.js');
const buildVersion = (number) => {
let list = number.split(',').map((item) => {
return +item;
});
return list;
};
function Connection(opts) {
EventEmitter.call(this);
const self = this;
const entities = new Map();
const BASE_URL = `http://${opts.domain}:${opts.port}/`;
const config = configManager();
Cluster.call(this, BASE_URL, config);
const request = requestBuilder(config);
let testConnection = async () => {
let body = await request({
'url': BASE_URL,
'timeout': 1500
});
config.set('version', buildVersion(body.version.number));
};
let connect = async () => {
await testConnection();
};
this.register = (name, opts, mappings, settings) => {
if (!config.has('BASE_URL')) {
throw new Error("make sure instance has already connected");
}
if (!opts.index) {
throw new Error("index could not be blank");
}
if (!opts.type) {
throw new Error('type could not be blank');
}
entities.set(name, new Entity(name, opts, mappings, settings, config));
return entities.get(name);
};
this.set = (key, value) => {
config.set(key, value);
};
this.get = (key) => {
return config.get(key);
};
connect().then((ret) => {
config.set('domain', opts.domain);
config.set('port', opts.port);
config.set('BASE_URL', BASE_URL);
self.emit('connected');
}).catch((e) => {
self.emit('error', e);
});
};
Connection.prototype = new EventEmitter();
const ES = (opts) => {
if (!opts.domain || !opts.port) {
throw new Error("opts params is invalide");
}
let conn = new Connection(opts);
return conn;
};
ES.Aggs = Aggs;
ES.Range = Range;
ES.Entity = Entity;
ES.Condition = Condition;
module.exports = ES;