-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
101 lines (88 loc) · 2.34 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const {
Client
} = require('es7');
const {
addDefaults,
buildOrder,
buildFilter
} = require('./lib/filter');
const {
buildWhere,
buildNestedQueries,
buildDeepNestedQueries
} = require('./lib/where');
const {
setup
} = require('./lib/setup');
const {
find,
findOne,
findById,
count
} = require('./lib/search');
const {
create,
updateById,
updateByQuery,
deleteById,
deleteByQuery
} = require('./lib/write');
class ESConnector {
constructor (modelDefinition, dataSource) {
this.modelDefinition = modelDefinition;
this.dataSource = dataSource;
this.db = null;
this.refreshOn = [
'create',
'updateById',
'deleteById',
'updateByQuery',
'deleteByQuery'
];
this.restAsInt = [
'find',
'findOne',
'findById'
];
if (this.modelDefinition.id === undefined ||
this.modelDefinition.id.property === undefined ||
this.modelDefinition.id.property === null) {
throw new Error('id field not set for model!');
}
this.modelName = this.modelDefinition.name;
this.idName = this.modelDefinition.id.property;
}
}
ESConnector.prototype.setup = setup;
ESConnector.prototype.init = async function () {
this.db = new Client(this.dataSource.settings.configuration);
await this.db.info();
return await this.setup();
};
ESConnector.prototype.addDefaults = addDefaults;
ESConnector.prototype.buildOrder = buildOrder;
ESConnector.prototype.buildFilter = buildFilter;
ESConnector.prototype.buildWhere = buildWhere;
ESConnector.prototype.buildNestedQueries = buildNestedQueries;
ESConnector.prototype.buildDeepNestedQueries = buildDeepNestedQueries;
ESConnector.prototype.find = find;
ESConnector.prototype.findById = findById;
ESConnector.prototype.findOne = findOne;
ESConnector.prototype.count = count;
ESConnector.prototype.create = create;
ESConnector.prototype.updateById = updateById;
ESConnector.prototype.updateByQuery = updateByQuery;
ESConnector.prototype.deleteById = deleteById;
ESConnector.prototype.deleteByQuery = deleteByQuery;
ESConnector.prototype.docToModel = function (doc, total = null, sort = null) {
const modelDoc = doc._source;
if (total) {
modelDoc._total = total;
}
if (sort) {
modelDoc._search_after = sort;
}
modelDoc[this.modelDefinition.id.property] = doc._id;
return modelDoc;
};
module.exports = ESConnector;