@opra/elastic 1.21.0 → 1.22.0

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.
Files changed (31) hide show
  1. package/{esm/elastic-collection-service.js → elastic-collection-service.js} +12 -0
  2. package/{esm/elastic-entity-service.js → elastic-entity-service.js} +23 -2
  3. package/{esm/elastic-service.js → elastic-service.js} +11 -0
  4. package/package.json +10 -26
  5. package/cjs/adapter-utils/prepare-filter.js +0 -130
  6. package/cjs/adapter-utils/prepare-patch.js +0 -35
  7. package/cjs/adapter-utils/prepare-projection.js +0 -69
  8. package/cjs/adapter-utils/prepare-sort.js +0 -16
  9. package/cjs/elastic-adapter.js +0 -126
  10. package/cjs/elastic-collection-service.js +0 -409
  11. package/cjs/elastic-entity-service.js +0 -536
  12. package/cjs/elastic-service.js +0 -64
  13. package/cjs/index.js +0 -7
  14. package/cjs/package.json +0 -3
  15. package/esm/package.json +0 -3
  16. package/types/index.d.cts +0 -4
  17. /package/{types/adapter-utils → adapter-utils}/prepare-filter.d.ts +0 -0
  18. /package/{esm/adapter-utils → adapter-utils}/prepare-filter.js +0 -0
  19. /package/{types/adapter-utils → adapter-utils}/prepare-patch.d.ts +0 -0
  20. /package/{esm/adapter-utils → adapter-utils}/prepare-patch.js +0 -0
  21. /package/{types/adapter-utils → adapter-utils}/prepare-projection.d.ts +0 -0
  22. /package/{esm/adapter-utils → adapter-utils}/prepare-projection.js +0 -0
  23. /package/{types/adapter-utils → adapter-utils}/prepare-sort.d.ts +0 -0
  24. /package/{esm/adapter-utils → adapter-utils}/prepare-sort.js +0 -0
  25. /package/{types/elastic-adapter.d.ts → elastic-adapter.d.ts} +0 -0
  26. /package/{esm/elastic-adapter.js → elastic-adapter.js} +0 -0
  27. /package/{types/elastic-collection-service.d.ts → elastic-collection-service.d.ts} +0 -0
  28. /package/{types/elastic-entity-service.d.ts → elastic-entity-service.d.ts} +0 -0
  29. /package/{types/elastic-service.d.ts → elastic-service.d.ts} +0 -0
  30. /package/{types/index.d.ts → index.d.ts} +0 -0
  31. /package/{esm/index.js → index.js} +0 -0
@@ -6,6 +6,18 @@ import { ElasticEntityService } from './elastic-entity-service.js';
6
6
  * @template T - The type of the documents in the collection.
7
7
  */
8
8
  export class ElasticCollectionService extends ElasticEntityService {
9
+ /**
10
+ * Represents a common filter function for a ElasticService.
11
+ *
12
+ * @type {FilterInput | Function}
13
+ */
14
+ documentFilter;
15
+ /**
16
+ * Represents the default limit value for a certain operation.
17
+ *
18
+ * @type {number}
19
+ */
20
+ defaultLimit;
9
21
  /**
10
22
  * Constructs a new instance
11
23
  *
@@ -7,6 +7,29 @@ import { ElasticService } from './elastic-service.js';
7
7
  * @template T - The type of the documents in the collection.
8
8
  */
9
9
  export class ElasticEntityService extends ElasticService {
10
+ _dataTypeScope;
11
+ _dataType_;
12
+ _dataType;
13
+ _inputCodecs = {};
14
+ _outputCodecs = {};
15
+ /**
16
+ * Defines comma delimited scopes for api document
17
+ */
18
+ scope;
19
+ /**
20
+ * Represents the name of a index in ElasticDB
21
+ */
22
+ indexName;
23
+ /**
24
+ * Represents the name of a resource.
25
+ * @type {string}
26
+ */
27
+ resourceName;
28
+ /**
29
+ * Generates a new id for new inserting Document.
30
+ *
31
+ */
32
+ idGenerator;
10
33
  /**
11
34
  * Constructs a new instance
12
35
  *
@@ -16,8 +39,6 @@ export class ElasticEntityService extends ElasticService {
16
39
  */
17
40
  constructor(dataType, options) {
18
41
  super(options);
19
- this._inputCodecs = {};
20
- this._outputCodecs = {};
21
42
  this._dataType_ = dataType;
22
43
  if (options?.indexName)
23
44
  this.indexName = options?.indexName;
@@ -5,6 +5,17 @@ import { ServiceBase } from '@opra/core';
5
5
  * @template T - The type of the documents in the collection.
6
6
  */
7
7
  export class ElasticService extends ServiceBase {
8
+ /**
9
+ * Represents a ElasticDB database object.
10
+ */
11
+ client;
12
+ /**
13
+ * Callback function for handling errors.
14
+ *
15
+ * @param {unknown} error - The error object.
16
+ * @param _this - The context object.
17
+ */
18
+ onError;
8
19
  /**
9
20
  * Constructs a new instance
10
21
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/elastic",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
4
4
  "description": "Opra Elastic Search adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -11,43 +11,27 @@
11
11
  },
12
12
  "peerDependencies": {
13
13
  "@elastic/elasticsearch": ">=8.7.0 <10",
14
- "@opra/common": "^1.21.0",
15
- "@opra/core": "^1.21.0"
14
+ "@opra/common": "^1.22.0",
15
+ "@opra/core": "^1.22.0"
16
16
  },
17
17
  "type": "module",
18
+ "module": "./index.js",
19
+ "types": "./index.d.ts",
18
20
  "exports": {
19
21
  ".": {
20
- "import": {
21
- "types": "./types/index.d.ts",
22
- "default": "./esm/index.js"
23
- },
24
- "require": {
25
- "types": "./types/index.d.cts",
26
- "default": "./cjs/index.js"
27
- },
28
- "default": "./esm/index.js"
22
+ "types": "./index.d.ts",
23
+ "default": "./index.js"
29
24
  },
30
25
  "./package.json": "./package.json"
31
26
  },
32
- "main": "./cjs/index.js",
33
- "module": "./esm/index.js",
34
- "types": "./types/index.d.ts",
27
+ "engines": {
28
+ "node": ">=20.0"
29
+ },
35
30
  "repository": {
36
31
  "type": "git",
37
32
  "url": "git+https://github.com/panates/opra.git",
38
33
  "directory": "packages/elastic"
39
34
  },
40
- "engines": {
41
- "node": ">=16.0",
42
- "npm": ">=7.0.0"
43
- },
44
- "files": [
45
- "cjs/",
46
- "esm/",
47
- "types/",
48
- "LICENSE",
49
- "README.md"
50
- ],
51
35
  "keywords": [
52
36
  "opra",
53
37
  "elastic",
@@ -1,130 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = prepareFilter;
4
- require("@opra/core");
5
- const common_1 = require("@opra/common");
6
- function prepareFilter(filters) {
7
- const filtersArray = Array.isArray(filters) ? filters : [filters];
8
- if (!filtersArray.length)
9
- return;
10
- const out = [];
11
- for (const filter of filtersArray) {
12
- if (!filter)
13
- continue;
14
- let x = filter;
15
- if (typeof filter === 'string')
16
- x = prepareFilterAst(common_1.OpraFilter.parse(filter));
17
- else if (filter instanceof common_1.OpraFilter.Expression)
18
- x = prepareFilterAst(filter);
19
- out.push(x);
20
- }
21
- if (out.length > 1) {
22
- return { bool: { must: [...out] } };
23
- }
24
- return out[0] ? out[0] : undefined;
25
- }
26
- function prepareFilterAst(ast, negative) {
27
- if (!ast)
28
- return;
29
- if (ast instanceof common_1.OpraFilter.QualifiedIdentifier ||
30
- ast instanceof common_1.OpraFilter.Literal) {
31
- return ast.value;
32
- }
33
- if (ast instanceof common_1.OpraFilter.ArrayExpression) {
34
- return ast.items.map(x => prepareFilterAst(x, negative));
35
- }
36
- if (ast instanceof common_1.OpraFilter.NegativeExpression) {
37
- return prepareFilterAst(ast.expression, !negative);
38
- }
39
- if (ast instanceof common_1.OpraFilter.LogicalExpression) {
40
- const items = ast.items
41
- .map(x => prepareFilterAst(x))
42
- /** Filter nullish items */
43
- .filter(x => x != null);
44
- const k = (ast.op === 'or' ? 'should' : 'must') + (negative ? '_not' : '');
45
- return { bool: { [k]: items } };
46
- }
47
- if (ast instanceof common_1.OpraFilter.ParenthesizedExpression) {
48
- return prepareFilterAst(ast.expression, negative);
49
- }
50
- if (ast instanceof common_1.OpraFilter.ComparisonExpression) {
51
- if (!(ast.left instanceof common_1.OpraFilter.QualifiedIdentifier ||
52
- ast.left instanceof common_1.OpraFilter.StringLiteral)) {
53
- throw new Error('Left side of ComparisonExpression must be a QualifiedIdentifier');
54
- }
55
- const left = prepareFilterAst(ast.left);
56
- const right = prepareFilterAst(ast.right);
57
- let out;
58
- if (right == null) {
59
- negative = !negative;
60
- out = { exists: { field: left } };
61
- }
62
- else {
63
- if (ast.prepare) {
64
- out = ast.prepare({
65
- left,
66
- right,
67
- op: ast.op,
68
- adapter: 'elastic',
69
- });
70
- }
71
- if (!out) {
72
- switch (ast.op) {
73
- case '!=':
74
- case '=':
75
- case 'in':
76
- case '!in': {
77
- out = Array.isArray(right)
78
- ? { terms: { [left]: right } }
79
- : { match: { [left]: right } };
80
- break;
81
- }
82
- case '>': {
83
- out = { range: { [left]: { gt: right } } };
84
- break;
85
- }
86
- case '>=': {
87
- out = { range: { [left]: { gte: right } } };
88
- break;
89
- }
90
- case '<': {
91
- out = { range: { [left]: { lt: right } } };
92
- break;
93
- }
94
- case '<=': {
95
- out = { range: { [left]: { lte: right } } };
96
- break;
97
- }
98
- case '!like':
99
- case 'like': {
100
- out = {
101
- wildcard: { [left]: { value: String(right).replace(/%/g, '*') } },
102
- };
103
- break;
104
- }
105
- case '!ilike':
106
- case 'ilike': {
107
- out = {
108
- wildcard: {
109
- [left]: {
110
- value: String(right).replace(/%/g, '*'),
111
- case_insensitive: true,
112
- },
113
- },
114
- };
115
- break;
116
- }
117
- default:
118
- /* istanbul ignore next */
119
- throw new TypeError(`Unknown ComparisonExpression operation (${ast.op})`);
120
- }
121
- }
122
- }
123
- if ((ast.op.startsWith('!') && !negative) ||
124
- (!ast.op.startsWith('!') && negative)) {
125
- return { bool: { must_not: { ...out } } };
126
- }
127
- return out;
128
- }
129
- throw new Error(`${ast.kind} is not implemented yet`);
130
- }
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = preparePatch;
4
- function preparePatch(doc) {
5
- const script = [];
6
- const params = {};
7
- _preparePatch(doc, script, params, '');
8
- return {
9
- source: script.join('\n'),
10
- params,
11
- lang: 'painless',
12
- };
13
- }
14
- function _preparePatch(src, script, params, path) {
15
- let f;
16
- let field;
17
- for (const [k, v] of Object.entries(src)) {
18
- f = k.startsWith('*') ? k.substring(1) : k;
19
- field = path ? path + '.' + f : f;
20
- if (v == null) {
21
- script.push(`ctx._source.remove('${field}');`);
22
- continue;
23
- }
24
- if (v &&
25
- typeof v === 'object' &&
26
- !Array.isArray(v) &&
27
- /** If field name starts with "*", do "replace" operation except "merge" */
28
- !k.startsWith('*')) {
29
- _preparePatch(v, script, params, field);
30
- continue;
31
- }
32
- script.push(`ctx._source['${field}'] = params['${field}'];`);
33
- params[field] = v;
34
- }
35
- }
@@ -1,69 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = prepareProjection;
4
- exports.prepare = prepare;
5
- const common_1 = require("@opra/common");
6
- function prepareProjection(dataType, projection, scope) {
7
- const out = {};
8
- const includes = [];
9
- const excludes = [];
10
- const projection_ = typeof projection === 'string' || Array.isArray(projection)
11
- ? (0, common_1.parseFieldsProjection)(projection)
12
- : projection;
13
- prepare(dataType, includes, excludes, '', projection_, scope);
14
- if (includes.length)
15
- out.includes = includes;
16
- if (excludes.length)
17
- out.excludes = excludes;
18
- return includes.length || excludes.length ? out : undefined;
19
- }
20
- function getNeedIncludes(projection) {
21
- return !!(projection && Object.values(projection).find(p => !p.sign));
22
- }
23
- function prepare(dataType, includes, excludes, curPath, projection, scope) {
24
- const needIncludes = getNeedIncludes(projection);
25
- const projectionKeys = projection && Object.keys(projection);
26
- const projectionKeysSet = new Set(projectionKeys);
27
- let fieldName;
28
- let fieldPath;
29
- let field;
30
- let k;
31
- /** Add fields from data type */
32
- for (field of dataType.fields(scope)) {
33
- fieldName = field.name;
34
- fieldPath = curPath + (curPath ? '.' : '') + fieldName;
35
- k = fieldName.toLowerCase();
36
- projectionKeysSet.delete(k);
37
- const p = projection?.[k];
38
- if (
39
- /** if field is omitted */
40
- p?.sign === '-' ||
41
- /** if no projection defined for this field and includeDefaultFields is true and the field is exclusive */
42
- (!p && field.exclusive)) {
43
- if (!needIncludes)
44
- excludes.push(fieldPath);
45
- continue;
46
- }
47
- if (needIncludes && p && !includes.includes(fieldPath)) {
48
- if (!getNeedIncludes(p?.projection)) {
49
- includes.push(fieldPath);
50
- }
51
- }
52
- if (field.type instanceof common_1.ComplexType &&
53
- typeof p?.projection === 'object') {
54
- prepare(field.type, includes, excludes, fieldPath, p.projection);
55
- }
56
- }
57
- if (dataType.additionalFields) {
58
- for (k of projectionKeysSet.values()) {
59
- const n = projection?.[k];
60
- fieldPath = curPath + (curPath ? '.' : '') + k;
61
- if (n?.sign === '-') {
62
- if (!needIncludes)
63
- excludes.push(fieldPath);
64
- }
65
- else
66
- includes.push(fieldPath);
67
- }
68
- }
69
- }
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = prepareSort;
4
- const SIGN_PATTERN = /^([+-])?(.+)$/;
5
- function prepareSort(sort) {
6
- if (!(sort && sort.length))
7
- return;
8
- const out = [];
9
- sort.forEach(k => {
10
- const m = SIGN_PATTERN.exec(k);
11
- if (m) {
12
- out.push({ [m[2]]: { order: m[1] === '-' ? 'desc' : 'asc' } });
13
- }
14
- });
15
- return out;
16
- }
@@ -1,126 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ElasticAdapter = void 0;
4
- const tslib_1 = require("tslib");
5
- const prepare_filter_js_1 = tslib_1.__importDefault(require("./adapter-utils/prepare-filter.js"));
6
- const prepare_patch_js_1 = tslib_1.__importDefault(require("./adapter-utils/prepare-patch.js"));
7
- const prepare_projection_js_1 = tslib_1.__importDefault(require("./adapter-utils/prepare-projection.js"));
8
- const prepare_sort_js_1 = tslib_1.__importDefault(require("./adapter-utils/prepare-sort.js"));
9
- var ElasticAdapter;
10
- (function (ElasticAdapter) {
11
- ElasticAdapter.prepareFilter = prepare_filter_js_1.default;
12
- ElasticAdapter.preparePatch = prepare_patch_js_1.default;
13
- ElasticAdapter.prepareProjection = prepare_projection_js_1.default;
14
- ElasticAdapter.prepareSort = prepare_sort_js_1.default;
15
- async function parseRequest(context) {
16
- if (context.transport !== 'http') {
17
- throw new TypeError('ElasticAdapter can parse only HttpContext');
18
- }
19
- const ctx = context;
20
- const { __oprDef } = ctx;
21
- if (__oprDef?.composition?.startsWith('Entity.') &&
22
- __oprDef.compositionOptions?.type) {
23
- const controller = __oprDef.owner;
24
- switch (__oprDef.composition) {
25
- case 'Entity.Create': {
26
- const data = await ctx.getBody();
27
- const options = {
28
- projection: ctx.queryParams.projection,
29
- };
30
- return {
31
- method: 'create',
32
- data,
33
- options,
34
- };
35
- }
36
- case 'Entity.Delete': {
37
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
38
- controller.parameters.find(p => p.keyParam);
39
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
40
- const options = {
41
- filter: ctx.queryParams.filter,
42
- };
43
- return {
44
- method: 'delete',
45
- key,
46
- options,
47
- };
48
- }
49
- case 'Entity.DeleteMany': {
50
- const options = {
51
- filter: ctx.queryParams.filter,
52
- };
53
- return { method: 'deleteMany', options };
54
- }
55
- case 'Entity.FindMany': {
56
- const options = {
57
- filter: ctx.queryParams.filter,
58
- projection: ctx.queryParams.projection,
59
- count: ctx.queryParams.count,
60
- limit: ctx.queryParams.limit,
61
- skip: ctx.queryParams.skip,
62
- sort: ctx.queryParams.sort,
63
- };
64
- return { method: 'findMany', options };
65
- }
66
- case 'Entity.Get': {
67
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
68
- controller.parameters.find(p => p.keyParam);
69
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
70
- const options = {
71
- projection: ctx.queryParams.projection,
72
- filter: ctx.queryParams.filter,
73
- };
74
- return { method: 'get', key, options };
75
- }
76
- case 'Entity.Replace': {
77
- const data = await ctx.getBody();
78
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
79
- controller.parameters.find(p => p.keyParam);
80
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
81
- const options = {
82
- projection: ctx.queryParams.projection,
83
- filter: ctx.queryParams.filter,
84
- };
85
- return {
86
- method: 'replace',
87
- key,
88
- data,
89
- options,
90
- };
91
- }
92
- case 'Entity.Update': {
93
- const data = await ctx.getBody();
94
- const keyParam = __oprDef.parameters.find(p => p.keyParam) ||
95
- controller.parameters.find(p => p.keyParam);
96
- const key = keyParam && ctx.pathParams[String(keyParam.name)];
97
- const options = {
98
- projection: ctx.queryParams.projection,
99
- filter: ctx.queryParams.filter,
100
- };
101
- return {
102
- method: 'update',
103
- key,
104
- data,
105
- options,
106
- };
107
- }
108
- case 'Entity.UpdateMany': {
109
- const data = await ctx.getBody();
110
- const options = {
111
- filter: ctx.queryParams.filter,
112
- };
113
- return {
114
- method: 'updateMany',
115
- data,
116
- options,
117
- };
118
- }
119
- default:
120
- break;
121
- }
122
- }
123
- throw new Error(`This operation is not compatible to Elastic adapter`);
124
- }
125
- ElasticAdapter.parseRequest = parseRequest;
126
- })(ElasticAdapter || (exports.ElasticAdapter = ElasticAdapter = {}));