@webiny/api-elasticsearch 0.0.0-mt-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/client.d.ts +5 -0
- package/client.js +47 -0
- package/compression.d.ts +6 -0
- package/compression.js +68 -0
- package/cursors.d.ts +9 -0
- package/cursors.js +49 -0
- package/index.d.ts +9 -0
- package/index.js +38 -0
- package/limit.d.ts +1 -0
- package/limit.js +45 -0
- package/normalize.d.ts +5 -0
- package/normalize.js +27 -0
- package/operators.d.ts +18 -0
- package/operators.js +42 -0
- package/package.json +49 -0
- package/plugins/GzipCompression.d.ts +19 -0
- package/plugins/GzipCompression.js +75 -0
- package/plugins/definition/CompressionPlugin.d.ts +20 -0
- package/plugins/definition/CompressionPlugin.js +17 -0
- package/plugins/definition/ElasticsearchBodyModifierPlugin.d.ts +13 -0
- package/plugins/definition/ElasticsearchBodyModifierPlugin.js +35 -0
- package/plugins/definition/ElasticsearchFieldPlugin.d.ts +87 -0
- package/plugins/definition/ElasticsearchFieldPlugin.js +128 -0
- package/plugins/definition/ElasticsearchQueryBuilderOperatorPlugin.d.ts +10 -0
- package/plugins/definition/ElasticsearchQueryBuilderOperatorPlugin.js +20 -0
- package/plugins/definition/ElasticsearchQueryModifierPlugin.d.ts +14 -0
- package/plugins/definition/ElasticsearchQueryModifierPlugin.js +35 -0
- package/plugins/definition/ElasticsearchSortModifierPlugin.d.ts +13 -0
- package/plugins/definition/ElasticsearchSortModifierPlugin.js +35 -0
- package/plugins/operator/andIn.d.ts +7 -0
- package/plugins/operator/andIn.js +56 -0
- package/plugins/operator/between.d.ts +7 -0
- package/plugins/operator/between.js +51 -0
- package/plugins/operator/contains.d.ts +7 -0
- package/plugins/operator/contains.js +43 -0
- package/plugins/operator/equal.d.ts +7 -0
- package/plugins/operator/equal.js +45 -0
- package/plugins/operator/gt.d.ts +7 -0
- package/plugins/operator/gt.js +40 -0
- package/plugins/operator/gte.d.ts +7 -0
- package/plugins/operator/gte.js +40 -0
- package/plugins/operator/in.d.ts +7 -0
- package/plugins/operator/in.js +54 -0
- package/plugins/operator/index.d.ts +13 -0
- package/plugins/operator/index.js +174 -0
- package/plugins/operator/lt.d.ts +7 -0
- package/plugins/operator/lt.js +40 -0
- package/plugins/operator/lte.d.ts +7 -0
- package/plugins/operator/lte.js +40 -0
- package/plugins/operator/not.d.ts +7 -0
- package/plugins/operator/not.js +40 -0
- package/plugins/operator/notBetween.d.ts +7 -0
- package/plugins/operator/notBetween.js +51 -0
- package/plugins/operator/notContains.d.ts +7 -0
- package/plugins/operator/notContains.js +43 -0
- package/plugins/operator/notIn.d.ts +7 -0
- package/plugins/operator/notIn.js +46 -0
- package/sort.d.ts +12 -0
- package/sort.js +67 -0
- package/types.d.ts +49 -0
- package/types.js +18 -0
- package/where.d.ts +17 -0
- package/where.js +106 -0
package/sort.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createSort = void 0;
|
|
9
|
+
|
|
10
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
11
|
+
|
|
12
|
+
const sortRegExp = new RegExp(/^([a-zA-Z-0-9_]+)_(ASC|DESC)$/);
|
|
13
|
+
|
|
14
|
+
const createSort = params => {
|
|
15
|
+
const {
|
|
16
|
+
sort,
|
|
17
|
+
defaults,
|
|
18
|
+
fieldPlugins
|
|
19
|
+
} = params;
|
|
20
|
+
|
|
21
|
+
if (!sort || sort.length === 0) {
|
|
22
|
+
const {
|
|
23
|
+
field,
|
|
24
|
+
order,
|
|
25
|
+
unmappedType
|
|
26
|
+
} = defaults || {};
|
|
27
|
+
/**
|
|
28
|
+
* We say that our system defaults is always id since all records we create have some kind of primary ID.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
[field || "id.keyword"]: {
|
|
33
|
+
order: order || "desc",
|
|
34
|
+
unmapped_type: unmappedType || undefined
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return sort.reduce((acc, value) => {
|
|
40
|
+
const match = value.match(sortRegExp);
|
|
41
|
+
|
|
42
|
+
if (!match) {
|
|
43
|
+
throw new _error.default(`Cannot sort by "${value}".`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const [, field, initialOrder] = match;
|
|
47
|
+
const order = initialOrder.toLowerCase() === "asc" ? "asc" : "desc";
|
|
48
|
+
const plugin = fieldPlugins[field] || fieldPlugins["*"];
|
|
49
|
+
|
|
50
|
+
if (!plugin) {
|
|
51
|
+
throw new _error.default(`Missing plugin for the field "${field}"`, "PLUGIN_SORT_ERROR", {
|
|
52
|
+
field
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* In case field plugin is the global one, change the * with actual field name.
|
|
57
|
+
* Custom path methods will return their own values anyway so replacing * will not matter.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
const path = plugin.getPath(field);
|
|
62
|
+
acc[path] = plugin.getSortOptions(order);
|
|
63
|
+
return acc;
|
|
64
|
+
}, {});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
exports.createSort = createSort;
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Client } from "@elastic/elasticsearch";
|
|
2
|
+
import { BoolQueryConfig as esBoolQueryConfig, Query as esQuery } from "elastic-ts";
|
|
3
|
+
import { ContextInterface } from "@webiny/handler/types";
|
|
4
|
+
export * from "elastic-ts";
|
|
5
|
+
export interface ElasticsearchContext extends ContextInterface {
|
|
6
|
+
elasticsearch: Client;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* To simplify our plugins, we say that query contains arrays of objects, not single objects.
|
|
10
|
+
* And that they all are defined as empty arrays at the start.
|
|
11
|
+
*/
|
|
12
|
+
export interface ElasticsearchBoolQueryConfig extends esBoolQueryConfig {
|
|
13
|
+
must: esQuery[];
|
|
14
|
+
filter: esQuery[];
|
|
15
|
+
should: esQuery[];
|
|
16
|
+
must_not: esQuery[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Definitions of possible Elasticsearch operators.
|
|
20
|
+
*
|
|
21
|
+
* @category Elasticsearch
|
|
22
|
+
*/
|
|
23
|
+
export declare type ElasticsearchQueryOperator = "eq" | "not" | "in" | "not_in" | "contains" | "not_contains" | "between" | "not_between" | "gt" | "gte" | "lt" | "lte";
|
|
24
|
+
/**
|
|
25
|
+
* Definition for arguments of the ElasticsearchQueryBuilderOperatorPlugin.apply method.
|
|
26
|
+
*
|
|
27
|
+
* @see ElasticsearchQueryBuilderOperatorPlugin.apply
|
|
28
|
+
*
|
|
29
|
+
* @category Plugin
|
|
30
|
+
* @category Elasticsearch
|
|
31
|
+
*/
|
|
32
|
+
export interface ElasticsearchQueryBuilderArgsPlugin {
|
|
33
|
+
/**
|
|
34
|
+
* A full path to the field. Including the ".keyword" if it is added.
|
|
35
|
+
*/
|
|
36
|
+
path: string;
|
|
37
|
+
/**
|
|
38
|
+
* A path to the field, plain.
|
|
39
|
+
*/
|
|
40
|
+
basePath: string;
|
|
41
|
+
/**
|
|
42
|
+
* Value to apply.
|
|
43
|
+
*/
|
|
44
|
+
value: any;
|
|
45
|
+
/**
|
|
46
|
+
* Is path containing the ".keyword"
|
|
47
|
+
*/
|
|
48
|
+
keyword: boolean;
|
|
49
|
+
}
|
package/types.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _elasticTs = require("elastic-ts");
|
|
8
|
+
|
|
9
|
+
Object.keys(_elasticTs).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _elasticTs[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _elasticTs[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
package/where.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ElasticsearchBoolQueryConfig } from "./types";
|
|
2
|
+
import { ElasticsearchFieldPlugin } from "./plugins/definition/ElasticsearchFieldPlugin";
|
|
3
|
+
import { ElasticsearchQueryBuilderOperatorPlugin } from "./plugins/definition/ElasticsearchQueryBuilderOperatorPlugin";
|
|
4
|
+
declare type Records<T> = Record<string, T>;
|
|
5
|
+
export interface Params {
|
|
6
|
+
query: ElasticsearchBoolQueryConfig;
|
|
7
|
+
where: Records<any>;
|
|
8
|
+
fields: Records<ElasticsearchFieldPlugin>;
|
|
9
|
+
operators: Records<ElasticsearchQueryBuilderOperatorPlugin>;
|
|
10
|
+
}
|
|
11
|
+
export interface ParseWhereKeyResult {
|
|
12
|
+
field: string;
|
|
13
|
+
operator: string;
|
|
14
|
+
}
|
|
15
|
+
export declare const parseWhereKey: (key: string) => ParseWhereKeyResult;
|
|
16
|
+
export declare const applyWhere: (params: Params) => void;
|
|
17
|
+
export {};
|
package/where.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.parseWhereKey = exports.applyWhere = void 0;
|
|
9
|
+
|
|
10
|
+
var _ElasticsearchFieldPlugin = require("./plugins/definition/ElasticsearchFieldPlugin");
|
|
11
|
+
|
|
12
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
14
|
+
const parseWhereKeyRegExp = new RegExp(/^([a-zA-Z0-9]+)(_[a-zA-Z0-9_]+)?$/);
|
|
15
|
+
|
|
16
|
+
const parseWhereKey = key => {
|
|
17
|
+
const match = key.match(parseWhereKeyRegExp);
|
|
18
|
+
|
|
19
|
+
if (!match) {
|
|
20
|
+
throw new Error(`It is not possible to search by key "${key}"`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const [, field, operation = "eq"] = match;
|
|
24
|
+
|
|
25
|
+
if (!field.match(/^([a-zA-Z]+)$/)) {
|
|
26
|
+
throw new Error(`Cannot filter by "${field}".`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const operator = operation.match(/^_/) ? operation.substr(1) : operation;
|
|
30
|
+
return {
|
|
31
|
+
field,
|
|
32
|
+
operator
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.parseWhereKey = parseWhereKey;
|
|
37
|
+
const ALL = _ElasticsearchFieldPlugin.ElasticsearchFieldPlugin.ALL;
|
|
38
|
+
|
|
39
|
+
const applyWhere = params => {
|
|
40
|
+
const {
|
|
41
|
+
query,
|
|
42
|
+
where,
|
|
43
|
+
fields,
|
|
44
|
+
operators
|
|
45
|
+
} = params;
|
|
46
|
+
|
|
47
|
+
for (const key in where) {
|
|
48
|
+
if (where.hasOwnProperty(key) === false) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const initialValue = where[key];
|
|
53
|
+
/**
|
|
54
|
+
* There is a possibility that undefined is sent as a value, so just skip it.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
if (initialValue === undefined) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const {
|
|
62
|
+
field,
|
|
63
|
+
operator
|
|
64
|
+
} = parseWhereKey(key);
|
|
65
|
+
const fieldPlugin = fields[field] || fields[ALL];
|
|
66
|
+
|
|
67
|
+
if (!fieldPlugin) {
|
|
68
|
+
throw new _error.default(`Missing plugin for the field "${field}".`, "PLUGIN_WHERE_ERROR", {
|
|
69
|
+
field
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const operatorPlugin = operators[operator];
|
|
74
|
+
|
|
75
|
+
if (!operatorPlugin) {
|
|
76
|
+
throw new _error.default(`Missing plugin for the operator "${operator}"`, "PLUGIN_WHERE_ERROR", {
|
|
77
|
+
operator
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Get the path but in the case of * (all fields, replace * with the field.
|
|
82
|
+
* Custom path would return its own value anyways.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
const path = fieldPlugin.getPath(field);
|
|
87
|
+
const basePath = fieldPlugin.getBasePath(field);
|
|
88
|
+
/**
|
|
89
|
+
* Transform the value for the search.
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
const value = fieldPlugin.toSearchValue({
|
|
93
|
+
value: initialValue,
|
|
94
|
+
path,
|
|
95
|
+
basePath
|
|
96
|
+
});
|
|
97
|
+
operatorPlugin.apply(query, {
|
|
98
|
+
value,
|
|
99
|
+
path,
|
|
100
|
+
basePath,
|
|
101
|
+
keyword: fieldPlugin.keyword
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
exports.applyWhere = applyWhere;
|