forked from cubyn/gumgum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (88 loc) · 2.67 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
102
103
104
105
106
107
// dependecies
const camelcase = require('camelcase');
// available keywords for search
const keyWords = ['body', 'query', 'filter', 'term', 'aggs', 'global', 'bool',
'multi_match', 'must', 'filtered', 'should', 'must_not', 'range', 'match'];
class EsWord {
// called when we use g[something](...args)
// something is the word arguments
// and args is the list of arguments given to the something function
constructor(word, args, isArrayKeyWord = false) {
if (isArrayKeyWord) {
this.parseArray(word, args[0]);
} else {
this.parseObject(word, args);
}
}
// g.must([{ key: 'a' }, { key: 'b' }])
// out
// { "must": [{ key: 'a' }, { key: 'b' }] }
parseArray(word, args) {
const body = args.map(arg => {
if (arg instanceof EsWord) {
return arg.compile();
} else {
return arg;
}
});
this.word = word;
this.body = body;
}
// g.must({ key: 'a' }, { key2: 'b' })
// out
// { "must": { key: 'a', key2: 'b' } }
parseObject(word, args) {
const body = args.reduce((acc, arg) => {
if (arg instanceof EsWord) {
return Object.assign(acc, arg.compile());
} else {
return Object.assign(acc, arg);
}
}, {});
this.word = word;
this.body = body;
}
// return the elastic query as a js object
compile() {
return { [this.word]: this.body };
}
}
// gumgum start a chain if called directly
function gumgum() {
// ctx is used to save and to end the chain
const ctx = function (...arg) {
return {
compile() {
let firstChain = ctx.chain.shift();
let dest = new EsWord(firstChain, arg, Array.isArray(arg[0])).compile();
// bubble up the chain
ctx.chain.forEach(elem => {
dest = { [elem]: dest };
});
ctx.chain.unshift(firstChain);
return dest;
}
}
};
// the chain
ctx.chain = [];
// for each available elastic keywords create a getter
keyWords.forEach(kw => {
Reflect.defineProperty(ctx, camelcase(kw), {
get() {
ctx.chain.unshift(kw);
return ctx;
}
});
});
return ctx;
}
keyWords.forEach(kw => {
gumgum[camelcase(kw)] = function (...arg) {
return new EsWord(kw, arg, Array.isArray(arg[0]));
};
});
gumgum.key = function (k, ...arg) {
return new EsWord(k, arg, Array.isArray(arg[0]));
}
module.exports = gumgum;