@webiny/api-dynamodb-to-elasticsearch 5.24.0-beta.0 → 5.25.0-beta.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.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["index.ts"],"names":["Operations","getError","item","index","error","reason","match","checkErrors","result","body","items","err","process","env","DEBUG","console","log","JSON","stringify","WebinyError","type","handle","context","event","args","operations","record","Records","newImage","Converter","unmarshall","dynamodb","NewImage","ignore","oldImage","OldImage","keys","Keys","_id","PK","SK","operation","eventName","data","undefined","REMOVE","plugins","INSERT","MODIFY","push","_index","delete","length","res","elasticsearch","bulk"],"mappings":";;;;;;;;;AAAA;;AACA;;AAGA;;IAGKA,U;;WAAAA,U;AAAAA,EAAAA,U;AAAAA,EAAAA,U;AAAAA,EAAAA,U;GAAAA,U,KAAAA,U;;AAoBL,MAAMC,QAAQ,GAAIC,IAAD,IAAyD;AACtE,MAAI,CAACA,IAAI,CAACC,KAAN,IAAe,CAACD,IAAI,CAACC,KAAL,CAAWC,KAA3B,IAAoC,CAACF,IAAI,CAACC,KAAL,CAAWC,KAAX,CAAiBC,MAA1D,EAAkE;AAC9D,WAAO,IAAP;AACH;;AACD,QAAMA,MAAM,GAAGH,IAAI,CAACC,KAAL,CAAWC,KAAX,CAAiBC,MAAhC;;AACA,MAAIA,MAAM,CAACC,KAAP,CAAa,oCAAb,MAAuD,IAA3D,EAAiE;AAC7D,WAAO,OAAP;AACH;;AACD,SAAOD,MAAP;AACH,CATD;;AAWA,MAAME,WAAW,GAAIC,MAAD,IAA4D;AAC5E,MAAI,CAACA,MAAD,IAAW,CAACA,MAAM,CAACC,IAAnB,IAA2B,CAACD,MAAM,CAACC,IAAP,CAAYC,KAA5C,EAAmD;AAC/C;AACH;;AACD,OAAK,MAAMR,IAAX,IAAmBM,MAAM,CAACC,IAAP,CAAYC,KAA/B,EAAsC;AAClC,UAAMC,GAAG,GAAGV,QAAQ,CAACC,IAAD,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,CAAemC,GAAf,EAAoB,IAApB,EAA0B,CAA1B,CAA7B;AACH;AACJ,KARD,CAQE,OAAOjD,KAAP,EAAc;AACZ,UAAIQ,OAAO,CAACC,GAAR,CAAYC,KAAZ,KAAsB,MAA1B,EAAkC;AAC9BC,QAAAA,OAAO,CAACC,GAAR,CAAY,YAAZ,EAA0BC,IAAI,CAACC,SAAL,CAAed,KAAf,EAAsB,IAAtB,EAA4B,CAA5B,CAA1B;AACH;;AACD,YAAMA,KAAN;AACH;;AAED,WAAO,IAAP;AACH;;AA5EsD,CAA5C,C","sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { Converter } from \"aws-sdk/clients/dynamodb\";\nimport { HandlerPlugin } from \"@webiny/handler/types\";\nimport { ElasticsearchContext } from \"@webiny/api-elasticsearch/types\";\nimport { decompress } from \"@webiny/api-elasticsearch/compression\";\nimport { ApiResponse } from \"@webiny/api-elasticsearch/types\";\n\nenum Operations {\n INSERT = \"INSERT\",\n MODIFY = \"MODIFY\",\n REMOVE = \"REMOVE\"\n}\n\ninterface BulkOperationsResponseBodyItemIndexError {\n reason?: string;\n}\ninterface BulkOperationsResponseBodyItemIndex {\n error?: BulkOperationsResponseBodyItemIndexError;\n}\ninterface BulkOperationsResponseBodyItem {\n index?: BulkOperationsResponseBodyItemIndex;\n error?: string;\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 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 default (): HandlerPlugin<ElasticsearchContext> => ({\n type: \"handler\",\n async handle(context) {\n const [event] = context.args;\n const operations = [];\n\n for (const record of event.Records) {\n const newImage = Converter.unmarshall(record.dynamodb.NewImage) as RecordDynamoDbImage;\n\n if (newImage.ignore === true) {\n continue;\n }\n\n const oldImage = Converter.unmarshall(record.dynamodb.OldImage) as RecordDynamoDbImage;\n const keys = Converter.unmarshall(record.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 false;\n }\n\n try {\n const res = await context.elasticsearch.bulk<BulkOperationsResponseBody>({\n body: operations\n });\n checkErrors(res);\n if (process.env.DEBUG === \"true\") {\n console.log(\"Bulk response\", JSON.stringify(res, null, 2));\n }\n } catch (error) {\n if (process.env.DEBUG === \"true\") {\n console.log(\"Bulk error\", JSON.stringify(error, null, 2));\n }\n throw error;\n }\n\n return true;\n }\n});\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api-dynamodb-to-elasticsearch",
3
- "version": "5.24.0-beta.0",
3
+ "version": "5.25.0-beta.1",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,11 +11,11 @@
11
11
  "license": "MIT",
12
12
  "author": "Webiny Ltd.",
13
13
  "dependencies": {
14
- "@babel/runtime": "7.16.7",
15
- "@webiny/api-elasticsearch": "5.24.0-beta.0",
16
- "@webiny/error": "5.24.0-beta.0",
17
- "@webiny/handler": "5.24.0-beta.0",
18
- "aws-sdk": "2.1066.0"
14
+ "@babel/runtime": "7.17.2",
15
+ "@webiny/api-elasticsearch": "5.25.0-beta.1",
16
+ "@webiny/error": "5.25.0-beta.1",
17
+ "@webiny/handler": "5.25.0-beta.1",
18
+ "aws-sdk": "2.1086.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@babel/cli": "^7.16.0",
@@ -24,9 +24,9 @@
24
24
  "@babel/plugin-transform-runtime": "^7.16.4",
25
25
  "@babel/preset-env": "^7.16.4",
26
26
  "@babel/preset-typescript": "^7.16.0",
27
- "@webiny/cli": "^5.24.0-beta.0",
28
- "@webiny/project-utils": "^5.24.0-beta.0",
29
- "typescript": "^4.1.3"
27
+ "@webiny/cli": "^5.25.0-beta.1",
28
+ "@webiny/project-utils": "^5.25.0-beta.1",
29
+ "typescript": "^4.5.5"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public",
@@ -46,5 +46,5 @@
46
46
  ]
47
47
  }
48
48
  },
49
- "gitHead": "05b90b92bbaf2ef3adf275d008c4641580cf5f42"
49
+ "gitHead": "c5cbc36b39a28e3f1c58de7d768fdcd113ad8e15"
50
50
  }