@webiny/api-dynamodb-to-elasticsearch 0.0.0-mt-3 → 0.0.0-unstable.2af142b57e

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/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const createEventHandler: () => import("@webiny/handler-aws").DynamoDBEventHandler<null>;
package/index.js ADDED
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.createEventHandler = void 0;
8
+ var _error = _interopRequireDefault(require("@webiny/error"));
9
+ var _dynamodb = require("aws-sdk/clients/dynamodb");
10
+ var _apiElasticsearch = require("@webiny/api-elasticsearch");
11
+ var _handlerAws = require("@webiny/handler-aws");
12
+ var _pRetry = _interopRequireDefault(require("p-retry"));
13
+ var Operations;
14
+ (function (Operations) {
15
+ Operations["INSERT"] = "INSERT";
16
+ Operations["MODIFY"] = "MODIFY";
17
+ Operations["REMOVE"] = "REMOVE";
18
+ })(Operations || (Operations = {}));
19
+ const getError = item => {
20
+ if (!item.index || !item.index.error || !item.index.error.reason) {
21
+ return null;
22
+ }
23
+ const reason = item.index.error.reason;
24
+ if (reason.match(/no such index \[([a-zA-Z0-9_-]+)\]/) !== null) {
25
+ return "index";
26
+ }
27
+ return reason;
28
+ };
29
+ const getNumberEnvVariable = (name, def) => {
30
+ const input = process.env[name];
31
+ const value = Number(input);
32
+ if (value > 0) {
33
+ return value;
34
+ }
35
+ return def;
36
+ };
37
+ const checkErrors = result => {
38
+ if (!result || !result.body || !result.body.items) {
39
+ return;
40
+ }
41
+ for (const item of result.body.items) {
42
+ const err = getError(item);
43
+ if (!err) {
44
+ continue;
45
+ } else if (err === "index") {
46
+ if (process.env.DEBUG === "true") {
47
+ console.log("Bulk response", JSON.stringify(result, null, 2));
48
+ }
49
+ continue;
50
+ }
51
+ console.log(item.error);
52
+ throw new _error.default(err, "DYNAMODB_TO_ELASTICSEARCH_ERROR", item);
53
+ }
54
+ };
55
+ const createEventHandler = () => {
56
+ return (0, _handlerAws.createDynamoDBEventHandler)(async ({
57
+ event,
58
+ context: ctx
59
+ }) => {
60
+ const context = ctx;
61
+ if (!context.elasticsearch) {
62
+ console.log("Missing elasticsearch definition on context.");
63
+ return null;
64
+ }
65
+
66
+ /**
67
+ * Wrap the code we need to run into the function, so it can be called within itself.
68
+ */
69
+ const execute = async () => {
70
+ const operations = [];
71
+ for (const record of event.Records) {
72
+ const dynamodb = record.dynamodb;
73
+ if (!dynamodb) {
74
+ continue;
75
+ }
76
+ const newImage = _dynamodb.Converter.unmarshall(dynamodb.NewImage);
77
+ if (newImage.ignore === true) {
78
+ continue;
79
+ }
80
+ const oldImage = _dynamodb.Converter.unmarshall(dynamodb.OldImage);
81
+ const keys = _dynamodb.Converter.unmarshall(dynamodb.Keys);
82
+ const _id = `${keys.PK}:${keys.SK}`;
83
+ const operation = record.eventName;
84
+
85
+ /**
86
+ * On operations other than REMOVE we decompress the data and store it into the Elasticsearch.
87
+ * No need to try to decompress if operation is REMOVE since there is no data sent into that operation.
88
+ */
89
+ let data = undefined;
90
+ if (operation !== Operations.REMOVE) {
91
+ /**
92
+ * We must decompress the data that is going into the Elasticsearch.
93
+ */
94
+ data = await (0, _apiElasticsearch.decompress)(context.plugins, newImage.data);
95
+ /**
96
+ * No point in writing null or undefined data into the Elasticsearch.
97
+ * This might happen on some error while decompressing. We will log it.
98
+ *
99
+ * Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.
100
+ * If it is - it is a bug.
101
+ */
102
+ if (data === undefined || data === null) {
103
+ console.log(`Could not get decompressed data, skipping ES operation "${operation}", ID ${_id}`);
104
+ continue;
105
+ }
106
+ }
107
+ switch (record.eventName) {
108
+ case Operations.INSERT:
109
+ case Operations.MODIFY:
110
+ operations.push({
111
+ index: {
112
+ _id,
113
+ _index: newImage.index
114
+ }
115
+ }, data);
116
+ break;
117
+ case Operations.REMOVE:
118
+ operations.push({
119
+ delete: {
120
+ _id,
121
+ _index: oldImage.index
122
+ }
123
+ });
124
+ break;
125
+ default:
126
+ break;
127
+ }
128
+ }
129
+ if (!operations.length) {
130
+ return;
131
+ }
132
+ try {
133
+ const res = await context.elasticsearch.bulk({
134
+ body: operations
135
+ });
136
+ checkErrors(res);
137
+ } catch (error) {
138
+ if (process.env.DEBUG === "true") {
139
+ const meta = (error === null || error === void 0 ? void 0 : error.meta) || {};
140
+ delete meta["meta"];
141
+ console.log("Bulk error", JSON.stringify(meta, null, 2));
142
+ }
143
+ throw error;
144
+ }
145
+ };
146
+ const maxRetryTime = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_RETRY_TIME", 300000);
147
+ const retries = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_RETRIES", 20);
148
+ const minTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MIN_TIMEOUT", 1500);
149
+ const maxTimeout = getNumberEnvVariable("WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_TIMEOUT", 30000);
150
+ await (0, _pRetry.default)(execute, {
151
+ maxRetryTime,
152
+ retries,
153
+ minTimeout,
154
+ maxTimeout,
155
+ onFailedAttempt: error => {
156
+ /**
157
+ * We will only log attempts which are after 3/4 of total attempts.
158
+ */
159
+ if (error.attemptNumber < retries * 0.75) {
160
+ return;
161
+ }
162
+ console.log(`Attempt #${error.attemptNumber} failed.`);
163
+ console.log(error.message);
164
+ }
165
+ });
166
+ return null;
167
+ });
168
+ };
169
+ exports.createEventHandler = createEventHandler;
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Operations","getError","item","index","error","reason","match","getNumberEnvVariable","name","def","input","process","env","value","Number","checkErrors","result","body","items","err","DEBUG","console","log","JSON","stringify","WebinyError","createEventHandler","createDynamoDBEventHandler","event","context","ctx","elasticsearch","execute","operations","record","Records","dynamodb","newImage","Converter","unmarshall","NewImage","ignore","oldImage","OldImage","keys","Keys","_id","PK","SK","operation","eventName","data","undefined","REMOVE","decompress","plugins","INSERT","MODIFY","push","_index","delete","length","res","bulk","meta","maxRetryTime","retries","minTimeout","maxTimeout","pRetry","onFailedAttempt","attemptNumber","message"],"sources":["index.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { Converter } from \"aws-sdk/clients/dynamodb\";\nimport { decompress } from \"@webiny/api-elasticsearch\";\nimport { ApiResponse, ElasticsearchContext } from \"@webiny/api-elasticsearch/types\";\nimport { createDynamoDBEventHandler } from \"@webiny/handler-aws\";\nimport { StreamRecord } from \"aws-lambda/trigger/dynamodb-stream\";\nimport pRetry from \"p-retry\";\n\nenum Operations {\n INSERT = \"INSERT\",\n MODIFY = \"MODIFY\",\n REMOVE = \"REMOVE\"\n}\n\ninterface BulkOperationsResponseBodyItemIndexError {\n reason?: string;\n}\n\ninterface BulkOperationsResponseBodyItemIndex {\n error?: BulkOperationsResponseBodyItemIndexError;\n}\n\ninterface BulkOperationsResponseBodyItem {\n index?: BulkOperationsResponseBodyItemIndex;\n error?: string;\n}\n\ninterface BulkOperationsResponseBody {\n items: BulkOperationsResponseBodyItem[];\n}\n\nconst getError = (item: BulkOperationsResponseBodyItem): string | null => {\n if (!item.index || !item.index.error || !item.index.error.reason) {\n return null;\n }\n const reason = item.index.error.reason;\n if (reason.match(/no such index \\[([a-zA-Z0-9_-]+)\\]/) !== null) {\n return \"index\";\n }\n return reason;\n};\n\nconst getNumberEnvVariable = (name: string, def: number): number => {\n const input = process.env[name];\n const value = Number(input);\n if (value > 0) {\n return value;\n }\n return def;\n};\n\nconst checkErrors = (result?: ApiResponse<BulkOperationsResponseBody>): void => {\n if (!result || !result.body || !result.body.items) {\n return;\n }\n for (const item of result.body.items) {\n const err = getError(item);\n if (!err) {\n continue;\n } else if (err === \"index\") {\n if (process.env.DEBUG === \"true\") {\n console.log(\"Bulk response\", JSON.stringify(result, null, 2));\n }\n continue;\n }\n console.log(item.error);\n throw new WebinyError(err, \"DYNAMODB_TO_ELASTICSEARCH_ERROR\", item);\n }\n};\n\ninterface RecordDynamoDbImage {\n data: Record<string, any>;\n ignore?: boolean;\n index: string;\n}\n\ninterface RecordDynamoDbKeys {\n PK: string;\n SK: string;\n}\n\nexport const createEventHandler = () => {\n return createDynamoDBEventHandler(async ({ event, context: ctx }) => {\n const context = ctx as unknown as ElasticsearchContext;\n if (!context.elasticsearch) {\n console.log(\"Missing elasticsearch definition on context.\");\n return null;\n }\n\n /**\n * Wrap the code we need to run into the function, so it can be called within itself.\n */\n const execute = async (): Promise<void> => {\n const operations = [];\n\n for (const record of event.Records) {\n const dynamodb = record.dynamodb as Required<StreamRecord>;\n if (!dynamodb) {\n continue;\n }\n const newImage = Converter.unmarshall(dynamodb.NewImage) as RecordDynamoDbImage;\n\n if (newImage.ignore === true) {\n continue;\n }\n\n const oldImage = Converter.unmarshall(dynamodb.OldImage) as RecordDynamoDbImage;\n const keys = Converter.unmarshall(dynamodb.Keys) as RecordDynamoDbKeys;\n const _id = `${keys.PK}:${keys.SK}`;\n const operation = record.eventName;\n\n /**\n * On operations other than REMOVE we decompress the data and store it into the Elasticsearch.\n * No need to try to decompress if operation is REMOVE since there is no data sent into that operation.\n */\n let data: any = undefined;\n if (operation !== Operations.REMOVE) {\n /**\n * We must decompress the data that is going into the Elasticsearch.\n */\n data = await decompress(context.plugins, newImage.data);\n /**\n * No point in writing null or undefined data into the Elasticsearch.\n * This might happen on some error while decompressing. We will log it.\n *\n * Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.\n * If it is - it is a bug.\n */\n if (data === undefined || data === null) {\n console.log(\n `Could not get decompressed data, skipping ES operation \"${operation}\", ID ${_id}`\n );\n continue;\n }\n }\n\n switch (record.eventName) {\n case Operations.INSERT:\n case Operations.MODIFY:\n operations.push({ index: { _id, _index: newImage.index } }, data);\n break;\n case Operations.REMOVE:\n operations.push({ delete: { _id, _index: oldImage.index } });\n break;\n default:\n break;\n }\n }\n\n if (!operations.length) {\n return;\n }\n\n try {\n const res = await context.elasticsearch.bulk<BulkOperationsResponseBody>({\n body: operations\n });\n checkErrors(res);\n } catch (error) {\n if (process.env.DEBUG === \"true\") {\n const meta = error?.meta || {};\n delete meta[\"meta\"];\n console.log(\"Bulk error\", JSON.stringify(meta, null, 2));\n }\n throw error;\n }\n };\n\n const maxRetryTime = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_RETRY_TIME\",\n 300000\n );\n const retries = getNumberEnvVariable(\"WEBINY_DYNAMODB_TO_ELASTICSEARCH_RETRIES\", 20);\n const minTimeout = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_ELASTICSEARCH_MIN_TIMEOUT\",\n 1500\n );\n const maxTimeout = getNumberEnvVariable(\n \"WEBINY_DYNAMODB_TO_ELASTICSEARCH_MAX_TIMEOUT\",\n 30000\n );\n\n await pRetry(execute, {\n maxRetryTime,\n retries,\n minTimeout,\n maxTimeout,\n onFailedAttempt: error => {\n /**\n * We will only log attempts which are after 3/4 of total attempts.\n */\n if (error.attemptNumber < retries * 0.75) {\n return;\n }\n console.log(`Attempt #${error.attemptNumber} failed.`);\n console.log(error.message);\n }\n });\n\n return null;\n });\n};\n"],"mappings":";;;;;;;AAAA;AACA;AACA;AAEA;AAEA;AAA6B,IAExBA,UAAU;AAAA,WAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;AAAA,GAAVA,UAAU,KAAVA,UAAU;AAuBf,MAAMC,QAAQ,GAAIC,IAAoC,IAAoB;EACtE,IAAI,CAACA,IAAI,CAACC,KAAK,IAAI,CAACD,IAAI,CAACC,KAAK,CAACC,KAAK,IAAI,CAACF,IAAI,CAACC,KAAK,CAACC,KAAK,CAACC,MAAM,EAAE;IAC9D,OAAO,IAAI;EACf;EACA,MAAMA,MAAM,GAAGH,IAAI,CAACC,KAAK,CAACC,KAA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,EAC9C,IAAI,CACP;IACD,MAAM6D,UAAU,GAAG7D,oBAAoB,CACnC,8CAA8C,EAC9C,KAAK,CACR;IAED,MAAM,IAAA8D,eAAM,EAACrC,OAAO,EAAE;MAClBiC,YAAY;MACZC,OAAO;MACPC,UAAU;MACVC,UAAU;MACVE,eAAe,EAAElE,KAAK,IAAI;QACtB;AAChB;AACA;QACgB,IAAIA,KAAK,CAACmE,aAAa,GAAGL,OAAO,GAAG,IAAI,EAAE;UACtC;QACJ;QACA7C,OAAO,CAACC,GAAG,CAAE,YAAWlB,KAAK,CAACmE,aAAc,UAAS,CAAC;QACtDlD,OAAO,CAACC,GAAG,CAAClB,KAAK,CAACoE,OAAO,CAAC;MAC9B;IACJ,CAAC,CAAC;IAEF,OAAO,IAAI;EACf,CAAC,CAAC;AACN,CAAC;AAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api-dynamodb-to-elasticsearch",
3
- "version": "0.0.0-mt-3",
3
+ "version": "0.0.0-unstable.2af142b57e",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,22 +11,25 @@
11
11
  "license": "MIT",
12
12
  "author": "Webiny Ltd.",
13
13
  "dependencies": {
14
- "@babel/runtime": "7.15.4",
15
- "@webiny/api-elasticsearch": "0.0.0-mt-3",
16
- "@webiny/error": "0.0.0-mt-3",
17
- "@webiny/handler": "0.0.0-mt-3",
18
- "aws-sdk": "2.1026.0"
14
+ "@babel/runtime": "7.20.13",
15
+ "@webiny/api-elasticsearch": "0.0.0-unstable.2af142b57e",
16
+ "@webiny/error": "0.0.0-unstable.2af142b57e",
17
+ "@webiny/handler-aws": "0.0.0-unstable.2af142b57e",
18
+ "aws-lambda": "1.0.7",
19
+ "aws-sdk": "2.1310.0",
20
+ "p-retry": "4.6.2"
19
21
  },
20
22
  "devDependencies": {
21
- "@babel/cli": "^7.5.5",
22
- "@babel/core": "^7.5.5",
23
- "@babel/plugin-proposal-object-rest-spread": "^7.5.5",
24
- "@babel/plugin-transform-runtime": "^7.5.5",
25
- "@babel/preset-env": "^7.5.5",
26
- "@babel/preset-typescript": "^7.0.0",
27
- "@webiny/cli": "^0.0.0-mt-3",
28
- "@webiny/project-utils": "^0.0.0-mt-3",
29
- "typescript": "^4.1.3"
23
+ "@babel/cli": "^7.19.3",
24
+ "@babel/core": "^7.19.3",
25
+ "@babel/plugin-proposal-object-rest-spread": "^7.16.0",
26
+ "@babel/plugin-transform-runtime": "^7.16.4",
27
+ "@babel/preset-env": "^7.19.4",
28
+ "@babel/preset-typescript": "^7.18.6",
29
+ "@webiny/cli": "^0.0.0-unstable.2af142b57e",
30
+ "@webiny/plugins": "^0.0.0-unstable.2af142b57e",
31
+ "@webiny/project-utils": "^0.0.0-unstable.2af142b57e",
32
+ "typescript": "4.7.4"
30
33
  },
31
34
  "publishConfig": {
32
35
  "access": "public",
@@ -46,5 +49,5 @@
46
49
  ]
47
50
  }
48
51
  },
49
- "gitHead": "ebea815be2be99404591cba465cc1fe88355bd48"
52
+ "gitHead": "2af142b57e7cdc433f5098b3b6d43ae6caa5d54e"
50
53
  }
@@ -1,4 +0,0 @@
1
- import { HandlerPlugin } from "@webiny/handler/types";
2
- import { ElasticsearchContext } from "@webiny/api-elasticsearch/types";
3
- declare const _default: () => HandlerPlugin<ElasticsearchContext>;
4
- export default _default;
package/handler/index.js DELETED
@@ -1,158 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = void 0;
9
-
10
- var _dynamodb = require("aws-sdk/clients/dynamodb");
11
-
12
- var _error = _interopRequireDefault(require("@webiny/error"));
13
-
14
- var _compression = require("@webiny/api-elasticsearch/compression");
15
-
16
- var Operations;
17
-
18
- (function (Operations) {
19
- Operations["INSERT"] = "INSERT";
20
- Operations["MODIFY"] = "MODIFY";
21
- Operations["REMOVE"] = "REMOVE";
22
- })(Operations || (Operations = {}));
23
-
24
- const getError = item => {
25
- if (!item.index || !item.index.error || !item.index.error.reason) {
26
- return null;
27
- }
28
-
29
- const reason = item.index.error.reason;
30
-
31
- if (reason.match(/no such index \[([a-zA-Z0-9_-]+)\]/) !== null) {
32
- return "index";
33
- }
34
-
35
- return reason;
36
- };
37
-
38
- const checkErrors = result => {
39
- if (!result || !result.body || !result.body.items) {
40
- return;
41
- }
42
-
43
- for (const item of result.body.items) {
44
- const err = getError(item);
45
-
46
- if (!err) {
47
- continue;
48
- } else if (err === "index") {
49
- if (process.env.DEBUG === "true") {
50
- console.log("Bulk response", JSON.stringify(result, null, 2));
51
- }
52
-
53
- continue;
54
- }
55
-
56
- console.log(item.error);
57
- throw new _error.default(err, "DYNAMODB_TO_ELASTICSEARCH_ERROR", item);
58
- }
59
- };
60
-
61
- var _default = () => ({
62
- type: "handler",
63
-
64
- async handle(context) {
65
- const [event] = context.args;
66
- const operations = [];
67
-
68
- for (const record of event.Records) {
69
- const newImage = _dynamodb.Converter.unmarshall(record.dynamodb.NewImage);
70
-
71
- if (newImage.ignore === true) {
72
- continue;
73
- }
74
-
75
- const oldImage = _dynamodb.Converter.unmarshall(record.dynamodb.OldImage);
76
-
77
- const keys = _dynamodb.Converter.unmarshall(record.dynamodb.Keys);
78
-
79
- const _id = `${keys.PK}:${keys.SK}`;
80
- const operation = record.eventName;
81
- /**
82
- * On operations other than REMOVE we decompress the data and store it into the Elasticsearch.
83
- * No need to try to decompress if operation is REMOVE since there is no data sent into that operation.
84
- */
85
-
86
- let data = undefined;
87
-
88
- if (operation !== Operations.REMOVE) {
89
- /**
90
- * We must decompress the data that is going into the Elasticsearch.
91
- */
92
- data = await (0, _compression.decompress)(context.plugins, newImage.data);
93
- /**
94
- * No point in writing null or undefined data into the Elasticsearch.
95
- * This might happen on some error while decompressing. We will log it.
96
- *
97
- * Data should NEVER be null or undefined in the Elasticsearch DynamoDB table, unless it is a delete operations.
98
- * If it is - it is a bug.
99
- */
100
-
101
- if (data === undefined || data === null) {
102
- console.log(`Could not get decompressed data, skipping ES operation "${operation}", ID ${_id}`);
103
- continue;
104
- }
105
- }
106
-
107
- switch (record.eventName) {
108
- case Operations.INSERT:
109
- case Operations.MODIFY:
110
- operations.push({
111
- index: {
112
- _id,
113
- _index: newImage.index
114
- }
115
- }, data);
116
- break;
117
-
118
- case Operations.REMOVE:
119
- operations.push({
120
- delete: {
121
- _id,
122
- _index: oldImage.index
123
- }
124
- });
125
- break;
126
-
127
- default:
128
- break;
129
- }
130
- }
131
-
132
- if (!operations.length) {
133
- return;
134
- }
135
-
136
- try {
137
- const res = await context.elasticsearch.bulk({
138
- body: operations
139
- });
140
- checkErrors(res);
141
-
142
- if (process.env.DEBUG === "true") {
143
- console.log("Bulk response", JSON.stringify(res, null, 2));
144
- }
145
- } catch (error) {
146
- if (process.env.DEBUG === "true") {
147
- console.log("Bulk error", JSON.stringify(error, null, 2));
148
- }
149
-
150
- throw error;
151
- }
152
-
153
- return true;
154
- }
155
-
156
- });
157
-
158
- exports.default = _default;