@webiny/api-elasticsearch-tasks 5.41.1 → 5.41.2-beta.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 (75) hide show
  1. package/definitions/entry.d.ts +2 -2
  2. package/definitions/entry.js.map +1 -1
  3. package/definitions/table.d.ts +2 -2
  4. package/definitions/table.js.map +1 -1
  5. package/helpers/scan.d.ts +3 -3
  6. package/helpers/scan.js +2 -2
  7. package/helpers/scan.js.map +1 -1
  8. package/index.js +1 -1
  9. package/index.js.map +1 -1
  10. package/package.json +30 -21
  11. package/settings/IndexManager.d.ts +3 -0
  12. package/settings/IndexManager.js +12 -1
  13. package/settings/IndexManager.js.map +1 -1
  14. package/tasks/Manager.d.ts +12 -9
  15. package/tasks/Manager.js +1 -0
  16. package/tasks/Manager.js.map +1 -1
  17. package/tasks/createIndexes/CreateIndexesTaskRunner.d.ts +2 -1
  18. package/tasks/createIndexes/CreateIndexesTaskRunner.js.map +1 -1
  19. package/tasks/createIndexes/index.js +6 -4
  20. package/tasks/createIndexes/index.js.map +1 -1
  21. package/tasks/dataSynchronization/DataSynchronizationTaskRunner.d.ts +15 -0
  22. package/tasks/dataSynchronization/DataSynchronizationTaskRunner.js +55 -0
  23. package/tasks/dataSynchronization/DataSynchronizationTaskRunner.js.map +1 -0
  24. package/tasks/dataSynchronization/createFactories.d.ts +2 -0
  25. package/tasks/dataSynchronization/createFactories.js +17 -0
  26. package/tasks/dataSynchronization/createFactories.js.map +1 -0
  27. package/tasks/dataSynchronization/elasticsearch/ElasticsearchFetcher.d.ts +10 -0
  28. package/tasks/dataSynchronization/elasticsearch/ElasticsearchFetcher.js +98 -0
  29. package/tasks/dataSynchronization/elasticsearch/ElasticsearchFetcher.js.map +1 -0
  30. package/tasks/dataSynchronization/elasticsearch/ElasticsearchSynchronize.d.ts +14 -0
  31. package/tasks/dataSynchronization/elasticsearch/ElasticsearchSynchronize.js +76 -0
  32. package/tasks/dataSynchronization/elasticsearch/ElasticsearchSynchronize.js.map +1 -0
  33. package/tasks/dataSynchronization/elasticsearch/ElasticsearchToDynamoDbSynchronization.d.ts +10 -0
  34. package/tasks/dataSynchronization/elasticsearch/ElasticsearchToDynamoDbSynchronization.js +77 -0
  35. package/tasks/dataSynchronization/elasticsearch/ElasticsearchToDynamoDbSynchronization.js.map +1 -0
  36. package/tasks/dataSynchronization/elasticsearch/abstractions/ElasticsearchFetcher.d.ts +21 -0
  37. package/tasks/dataSynchronization/elasticsearch/abstractions/ElasticsearchFetcher.js +7 -0
  38. package/tasks/dataSynchronization/elasticsearch/abstractions/ElasticsearchFetcher.js.map +1 -0
  39. package/tasks/dataSynchronization/elasticsearch/abstractions/ElasticsearchSynchronize.d.ts +17 -0
  40. package/tasks/dataSynchronization/elasticsearch/abstractions/ElasticsearchSynchronize.js +7 -0
  41. package/tasks/dataSynchronization/elasticsearch/abstractions/ElasticsearchSynchronize.js.map +1 -0
  42. package/tasks/dataSynchronization/elasticsearch/shouldIgnoreEsResponseError.d.ts +2 -0
  43. package/tasks/dataSynchronization/elasticsearch/shouldIgnoreEsResponseError.js +13 -0
  44. package/tasks/dataSynchronization/elasticsearch/shouldIgnoreEsResponseError.js.map +1 -0
  45. package/tasks/dataSynchronization/entities/getElasticsearchEntity.d.ts +713 -0
  46. package/tasks/dataSynchronization/entities/getElasticsearchEntity.js +46 -0
  47. package/tasks/dataSynchronization/entities/getElasticsearchEntity.js.map +1 -0
  48. package/tasks/dataSynchronization/entities/getElasticsearchEntityType.d.ts +11 -0
  49. package/tasks/dataSynchronization/entities/getElasticsearchEntityType.js +29 -0
  50. package/tasks/dataSynchronization/entities/getElasticsearchEntityType.js.map +1 -0
  51. package/tasks/dataSynchronization/entities/getTable.d.ts +7 -0
  52. package/tasks/dataSynchronization/entities/getTable.js +29 -0
  53. package/tasks/dataSynchronization/entities/getTable.js.map +1 -0
  54. package/tasks/dataSynchronization/entities/index.d.ts +3 -0
  55. package/tasks/dataSynchronization/entities/index.js +40 -0
  56. package/tasks/dataSynchronization/entities/index.js.map +1 -0
  57. package/tasks/dataSynchronization/index.d.ts +4 -0
  58. package/tasks/dataSynchronization/index.js +80 -0
  59. package/tasks/dataSynchronization/index.js.map +1 -0
  60. package/tasks/dataSynchronization/types.d.ts +38 -0
  61. package/tasks/dataSynchronization/types.js +7 -0
  62. package/tasks/dataSynchronization/types.js.map +1 -0
  63. package/tasks/enableIndexing/EnableIndexingTaskRunner.d.ts +2 -1
  64. package/tasks/enableIndexing/EnableIndexingTaskRunner.js.map +1 -1
  65. package/tasks/enableIndexing/index.js +7 -5
  66. package/tasks/enableIndexing/index.js.map +1 -1
  67. package/tasks/index.d.ts +1 -0
  68. package/tasks/index.js +11 -0
  69. package/tasks/index.js.map +1 -1
  70. package/tasks/reindexing/ReindexingTaskRunner.d.ts +2 -2
  71. package/tasks/reindexing/ReindexingTaskRunner.js.map +1 -1
  72. package/tasks/reindexing/reindexingTaskDefinition.js +7 -5
  73. package/tasks/reindexing/reindexingTaskDefinition.js.map +1 -1
  74. package/types.d.ts +7 -5
  75. package/types.js.map +1 -1
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ElasticsearchFetcher = void 0;
7
+ var _shouldIgnoreEsResponseError = require("./shouldIgnoreEsResponseError");
8
+ var _nodeUtil = require("node:util");
9
+ class ElasticsearchFetcher {
10
+ constructor(params) {
11
+ this.client = params.client;
12
+ }
13
+ async fetch({
14
+ index,
15
+ cursor,
16
+ limit
17
+ }) {
18
+ let response;
19
+ try {
20
+ response = await this.client.search({
21
+ index,
22
+ body: {
23
+ query: {
24
+ match_all: {}
25
+ },
26
+ sort: {
27
+ "id.keyword": {
28
+ order: "asc"
29
+ }
30
+ },
31
+ size: limit + 1,
32
+ track_total_hits: true,
33
+ search_after: cursor,
34
+ _source: false
35
+ }
36
+ });
37
+ } catch (ex) {
38
+ /**
39
+ * If we ignore the error, we can continue with the next index.
40
+ */
41
+ if ((0, _shouldIgnoreEsResponseError.shouldIgnoreEsResponseError)(ex)) {
42
+ if (process.env.DEBUG === "true") {
43
+ console.error((0, _nodeUtil.inspect)(ex, {
44
+ depth: 5,
45
+ showHidden: true
46
+ }));
47
+ }
48
+ return {
49
+ done: true,
50
+ totalCount: 0,
51
+ items: []
52
+ };
53
+ }
54
+ console.error("Failed to fetch data from Elasticsearch.", ex);
55
+ throw ex;
56
+ }
57
+ const {
58
+ hits,
59
+ total
60
+ } = response.body.hits;
61
+ if (hits.length === 0) {
62
+ return {
63
+ done: true,
64
+ cursor: undefined,
65
+ totalCount: total.value,
66
+ items: []
67
+ };
68
+ }
69
+ const hasMoreItems = hits.length > limit;
70
+ let nextCursor;
71
+ if (hasMoreItems) {
72
+ hits.pop();
73
+ nextCursor = hits.at(-1)?.sort;
74
+ }
75
+ const items = hits.reduce((collection, hit) => {
76
+ const [PK, SK] = hit._id.split(":");
77
+ if (!PK || !SK) {
78
+ return collection;
79
+ }
80
+ collection.push({
81
+ PK,
82
+ SK,
83
+ _id: hit._id,
84
+ index: hit._index
85
+ });
86
+ return collection;
87
+ }, []);
88
+ return {
89
+ totalCount: total.value,
90
+ cursor: nextCursor,
91
+ done: !nextCursor,
92
+ items
93
+ };
94
+ }
95
+ }
96
+ exports.ElasticsearchFetcher = ElasticsearchFetcher;
97
+
98
+ //# sourceMappingURL=ElasticsearchFetcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_shouldIgnoreEsResponseError","require","_nodeUtil","ElasticsearchFetcher","constructor","params","client","fetch","index","cursor","limit","response","search","body","query","match_all","sort","order","size","track_total_hits","search_after","_source","ex","shouldIgnoreEsResponseError","process","env","DEBUG","console","error","inspect","depth","showHidden","done","totalCount","items","hits","total","length","undefined","value","hasMoreItems","nextCursor","pop","at","reduce","collection","hit","PK","SK","_id","split","push","_index","exports"],"sources":["ElasticsearchFetcher.ts"],"sourcesContent":["import { Client } from \"@webiny/api-elasticsearch\";\nimport {\n IElasticsearchFetcher,\n IElasticsearchFetcherFetchParams,\n IElasticsearchFetcherFetchResponse,\n IElasticsearchFetcherFetchResponseItem\n} from \"./abstractions/ElasticsearchFetcher\";\nimport { ElasticsearchSearchResponse, PrimitiveValue } from \"@webiny/api-elasticsearch/types\";\nimport { shouldIgnoreEsResponseError } from \"./shouldIgnoreEsResponseError\";\nimport { inspect } from \"node:util\";\n\nexport interface IElasticsearchFetcherParams {\n client: Client;\n}\n\nexport class ElasticsearchFetcher implements IElasticsearchFetcher {\n private readonly client: Client;\n\n public constructor(params: IElasticsearchFetcherParams) {\n this.client = params.client;\n }\n public async fetch({\n index,\n cursor,\n limit\n }: IElasticsearchFetcherFetchParams): Promise<IElasticsearchFetcherFetchResponse> {\n let response: ElasticsearchSearchResponse<undefined>;\n try {\n response = await this.client.search({\n index,\n body: {\n query: {\n match_all: {}\n },\n sort: {\n \"id.keyword\": {\n order: \"asc\"\n }\n },\n size: limit + 1,\n track_total_hits: true,\n search_after: cursor,\n _source: false\n }\n });\n } catch (ex) {\n /**\n * If we ignore the error, we can continue with the next index.\n */\n if (shouldIgnoreEsResponseError(ex)) {\n if (process.env.DEBUG === \"true\") {\n console.error(\n inspect(ex, {\n depth: 5,\n showHidden: true\n })\n );\n }\n return {\n done: true,\n totalCount: 0,\n items: []\n };\n }\n console.error(\"Failed to fetch data from Elasticsearch.\", ex);\n throw ex;\n }\n\n const { hits, total } = response.body.hits;\n if (hits.length === 0) {\n return {\n done: true,\n cursor: undefined,\n totalCount: total.value,\n items: []\n };\n }\n\n const hasMoreItems = hits.length > limit;\n let nextCursor: PrimitiveValue[] | undefined;\n if (hasMoreItems) {\n hits.pop();\n nextCursor = hits.at(-1)?.sort;\n }\n const items = hits.reduce<IElasticsearchFetcherFetchResponseItem[]>((collection, hit) => {\n const [PK, SK] = hit._id.split(\":\");\n if (!PK || !SK) {\n return collection;\n }\n collection.push({\n PK,\n SK,\n _id: hit._id,\n index: hit._index\n });\n\n return collection;\n }, []);\n\n return {\n totalCount: total.value,\n cursor: nextCursor,\n done: !nextCursor,\n items\n };\n }\n}\n"],"mappings":";;;;;;AAQA,IAAAA,4BAAA,GAAAC,OAAA;AACA,IAAAC,SAAA,GAAAD,OAAA;AAMO,MAAME,oBAAoB,CAAkC;EAGxDC,WAAWA,CAACC,MAAmC,EAAE;IACpD,IAAI,CAACC,MAAM,GAAGD,MAAM,CAACC,MAAM;EAC/B;EACA,MAAaC,KAAKA,CAAC;IACfC,KAAK;IACLC,MAAM;IACNC;EAC8B,CAAC,EAA+C;IAC9E,IAAIC,QAAgD;IACpD,IAAI;MACAA,QAAQ,GAAG,MAAM,IAAI,CAACL,MAAM,CAACM,MAAM,CAAC;QAChCJ,KAAK;QACLK,IAAI,EAAE;UACFC,KAAK,EAAE;YACHC,SAAS,EAAE,CAAC;UAChB,CAAC;UACDC,IAAI,EAAE;YACF,YAAY,EAAE;cACVC,KAAK,EAAE;YACX;UACJ,CAAC;UACDC,IAAI,EAAER,KAAK,GAAG,CAAC;UACfS,gBAAgB,EAAE,IAAI;UACtBC,YAAY,EAAEX,MAAM;UACpBY,OAAO,EAAE;QACb;MACJ,CAAC,CAAC;IACN,CAAC,CAAC,OAAOC,EAAE,EAAE;MACT;AACZ;AACA;MACY,IAAI,IAAAC,wDAA2B,EAACD,EAAE,CAAC,EAAE;QACjC,IAAIE,OAAO,CAACC,GAAG,CAACC,KAAK,KAAK,MAAM,EAAE;UAC9BC,OAAO,CAACC,KAAK,CACT,IAAAC,iBAAO,EAACP,EAAE,EAAE;YACRQ,KAAK,EAAE,CAAC;YACRC,UAAU,EAAE;UAChB,CAAC,CACL,CAAC;QACL;QACA,OAAO;UACHC,IAAI,EAAE,IAAI;UACVC,UAAU,EAAE,CAAC;UACbC,KAAK,EAAE;QACX,CAAC;MACL;MACAP,OAAO,CAACC,KAAK,CAAC,0CAA0C,EAAEN,EAAE,CAAC;MAC7D,MAAMA,EAAE;IACZ;IAEA,MAAM;MAAEa,IAAI;MAAEC;IAAM,CAAC,GAAGzB,QAAQ,CAACE,IAAI,CAACsB,IAAI;IAC1C,IAAIA,IAAI,CAACE,MAAM,KAAK,CAAC,EAAE;MACnB,OAAO;QACHL,IAAI,EAAE,IAAI;QACVvB,MAAM,EAAE6B,SAAS;QACjBL,UAAU,EAAEG,KAAK,CAACG,KAAK;QACvBL,KAAK,EAAE;MACX,CAAC;IACL;IAEA,MAAMM,YAAY,GAAGL,IAAI,CAACE,MAAM,GAAG3B,KAAK;IACxC,IAAI+B,UAAwC;IAC5C,IAAID,YAAY,EAAE;MACdL,IAAI,CAACO,GAAG,CAAC,CAAC;MACVD,UAAU,GAAGN,IAAI,CAACQ,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE3B,IAAI;IAClC;IACA,MAAMkB,KAAK,GAAGC,IAAI,CAACS,MAAM,CAA2C,CAACC,UAAU,EAAEC,GAAG,KAAK;MACrF,MAAM,CAACC,EAAE,EAAEC,EAAE,CAAC,GAAGF,GAAG,CAACG,GAAG,CAACC,KAAK,CAAC,GAAG,CAAC;MACnC,IAAI,CAACH,EAAE,IAAI,CAACC,EAAE,EAAE;QACZ,OAAOH,UAAU;MACrB;MACAA,UAAU,CAACM,IAAI,CAAC;QACZJ,EAAE;QACFC,EAAE;QACFC,GAAG,EAAEH,GAAG,CAACG,GAAG;QACZzC,KAAK,EAAEsC,GAAG,CAACM;MACf,CAAC,CAAC;MAEF,OAAOP,UAAU;IACrB,CAAC,EAAE,EAAE,CAAC;IAEN,OAAO;MACHZ,UAAU,EAAEG,KAAK,CAACG,KAAK;MACvB9B,MAAM,EAAEgC,UAAU;MAClBT,IAAI,EAAE,CAACS,UAAU;MACjBP;IACJ,CAAC;EACL;AACJ;AAACmB,OAAA,CAAAlD,oBAAA,GAAAA,oBAAA","ignoreList":[]}
@@ -0,0 +1,14 @@
1
+ import { ITimer } from "@webiny/handler-aws";
2
+ import { Context } from "../../../types";
3
+ import { IElasticsearchSynchronize, IElasticsearchSynchronizeExecuteParams, IElasticsearchSynchronizeExecuteResponse } from "./abstractions/ElasticsearchSynchronize";
4
+ export interface IElasticsearchSynchronizeParams {
5
+ timer: ITimer;
6
+ context: Context;
7
+ }
8
+ export declare class ElasticsearchSynchronize implements IElasticsearchSynchronize {
9
+ private readonly timer;
10
+ private readonly context;
11
+ constructor(params: IElasticsearchSynchronizeParams);
12
+ execute(params: IElasticsearchSynchronizeExecuteParams): Promise<IElasticsearchSynchronizeExecuteResponse>;
13
+ private getEntity;
14
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ElasticsearchSynchronize = void 0;
7
+ var _dbDynamodb = require("@webiny/db-dynamodb");
8
+ var _apiDynamodbToElasticsearch = require("@webiny/api-dynamodb-to-elasticsearch");
9
+ var _entities = require("../entities");
10
+ class ElasticsearchSynchronize {
11
+ constructor(params) {
12
+ this.timer = params.timer;
13
+ this.context = params.context;
14
+ }
15
+ async execute(params) {
16
+ const {
17
+ items,
18
+ done,
19
+ index
20
+ } = params;
21
+ if (items.length === 0) {
22
+ return {
23
+ done: true
24
+ };
25
+ }
26
+ const table = (0, _entities.getTable)({
27
+ type: "es",
28
+ context: this.context
29
+ });
30
+ const readableItems = items.map(item => {
31
+ const entity = this.getEntity(item);
32
+ return entity.item.getBatch({
33
+ PK: item.PK,
34
+ SK: item.SK
35
+ });
36
+ });
37
+ const tableItems = await (0, _dbDynamodb.batchReadAll)({
38
+ items: readableItems,
39
+ table
40
+ });
41
+ const elasticsearchSyncBuilder = (0, _apiDynamodbToElasticsearch.createSynchronizationBuilder)({
42
+ timer: this.timer,
43
+ context: this.context
44
+ });
45
+ /**
46
+ * We need to find the items we have in the Elasticsearch but not in the DynamoDB-Elasticsearch table.
47
+ */
48
+ for (const item of items) {
49
+ const exists = tableItems.some(ddbItem => {
50
+ return ddbItem.PK === item.PK && ddbItem.SK === item.SK;
51
+ });
52
+ if (exists) {
53
+ continue;
54
+ }
55
+ elasticsearchSyncBuilder.delete({
56
+ index,
57
+ id: item._id
58
+ });
59
+ }
60
+ const executeWithRetry = elasticsearchSyncBuilder.build();
61
+ await executeWithRetry();
62
+ return {
63
+ done
64
+ };
65
+ }
66
+ getEntity(params) {
67
+ const type = (0, _entities.getElasticsearchEntityType)(params);
68
+ return (0, _entities.getElasticsearchEntity)({
69
+ type,
70
+ context: this.context
71
+ });
72
+ }
73
+ }
74
+ exports.ElasticsearchSynchronize = ElasticsearchSynchronize;
75
+
76
+ //# sourceMappingURL=ElasticsearchSynchronize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_dbDynamodb","require","_apiDynamodbToElasticsearch","_entities","ElasticsearchSynchronize","constructor","params","timer","context","execute","items","done","index","length","table","getTable","type","readableItems","map","item","entity","getEntity","getBatch","PK","SK","tableItems","batchReadAll","elasticsearchSyncBuilder","createSynchronizationBuilder","exists","some","ddbItem","delete","id","_id","executeWithRetry","build","getElasticsearchEntityType","getElasticsearchEntity","exports"],"sources":["ElasticsearchSynchronize.ts"],"sourcesContent":["import { batchReadAll } from \"@webiny/db-dynamodb\";\nimport { createSynchronizationBuilder } from \"@webiny/api-dynamodb-to-elasticsearch\";\nimport {\n getElasticsearchEntity,\n getElasticsearchEntityType,\n getTable,\n IGetElasticsearchEntityTypeParams\n} from \"~/tasks/dataSynchronization/entities\";\nimport { ITimer } from \"@webiny/handler-aws\";\nimport { Context } from \"~/types\";\nimport {\n IElasticsearchSynchronize,\n IElasticsearchSynchronizeExecuteParams,\n IElasticsearchSynchronizeExecuteResponse\n} from \"./abstractions/ElasticsearchSynchronize\";\n\nexport interface IElasticsearchSynchronizeParams {\n timer: ITimer;\n context: Context;\n}\n\ninterface IDynamoDbItem {\n PK: string;\n SK: string;\n}\n\nexport class ElasticsearchSynchronize implements IElasticsearchSynchronize {\n private readonly timer: ITimer;\n private readonly context: Context;\n\n public constructor(params: IElasticsearchSynchronizeParams) {\n this.timer = params.timer;\n this.context = params.context;\n }\n\n public async execute(\n params: IElasticsearchSynchronizeExecuteParams\n ): Promise<IElasticsearchSynchronizeExecuteResponse> {\n const { items, done, index } = params;\n if (items.length === 0) {\n return {\n done: true\n };\n }\n\n const table = getTable({\n type: \"es\",\n context: this.context\n });\n\n const readableItems = items.map(item => {\n const entity = this.getEntity(item);\n return entity.item.getBatch({\n PK: item.PK,\n SK: item.SK\n });\n });\n\n const tableItems = await batchReadAll<IDynamoDbItem>({\n items: readableItems,\n table\n });\n\n const elasticsearchSyncBuilder = createSynchronizationBuilder({\n timer: this.timer,\n context: this.context\n });\n /**\n * We need to find the items we have in the Elasticsearch but not in the DynamoDB-Elasticsearch table.\n */\n for (const item of items) {\n const exists = tableItems.some(ddbItem => {\n return ddbItem.PK === item.PK && ddbItem.SK === item.SK;\n });\n if (exists) {\n continue;\n }\n elasticsearchSyncBuilder.delete({\n index,\n id: item._id\n });\n }\n\n const executeWithRetry = elasticsearchSyncBuilder.build();\n await executeWithRetry();\n\n return {\n done\n };\n }\n\n private getEntity(\n params: IGetElasticsearchEntityTypeParams\n ): ReturnType<typeof getElasticsearchEntity> {\n const type = getElasticsearchEntityType(params);\n return getElasticsearchEntity({\n type,\n context: this.context\n });\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,WAAA,GAAAC,OAAA;AACA,IAAAC,2BAAA,GAAAD,OAAA;AACA,IAAAE,SAAA,GAAAF,OAAA;AAwBO,MAAMG,wBAAwB,CAAsC;EAIhEC,WAAWA,CAACC,MAAuC,EAAE;IACxD,IAAI,CAACC,KAAK,GAAGD,MAAM,CAACC,KAAK;IACzB,IAAI,CAACC,OAAO,GAAGF,MAAM,CAACE,OAAO;EACjC;EAEA,MAAaC,OAAOA,CAChBH,MAA8C,EACG;IACjD,MAAM;MAAEI,KAAK;MAAEC,IAAI;MAAEC;IAAM,CAAC,GAAGN,MAAM;IACrC,IAAII,KAAK,CAACG,MAAM,KAAK,CAAC,EAAE;MACpB,OAAO;QACHF,IAAI,EAAE;MACV,CAAC;IACL;IAEA,MAAM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C/B,MAAM,CAAC;IAC/C,OAAO,IAAAgC,gCAAsB,EAAC;MAC1BtB,IAAI;MACJR,OAAO,EAAE,IAAI,CAACA;IAClB,CAAC,CAAC;EACN;AACJ;AAAC+B,OAAA,CAAAnC,wBAAA,GAAAA,wBAAA","ignoreList":[]}
@@ -0,0 +1,10 @@
1
+ import { IDataSynchronizationInput, IElasticsearchSyncParams, ISynchronization, ISynchronizationRunResult } from "../types";
2
+ export declare class ElasticsearchToDynamoDbSynchronization implements ISynchronization {
3
+ private readonly manager;
4
+ private readonly indexManager;
5
+ private readonly synchronize;
6
+ private readonly fetcher;
7
+ constructor(params: IElasticsearchSyncParams);
8
+ run(input: IDataSynchronizationInput): Promise<ISynchronizationRunResult>;
9
+ private fetchAllIndexes;
10
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ElasticsearchToDynamoDbSynchronization = void 0;
7
+ class ElasticsearchToDynamoDbSynchronization {
8
+ constructor(params) {
9
+ this.manager = params.manager;
10
+ this.indexManager = params.indexManager;
11
+ this.synchronize = params.synchronize;
12
+ this.fetcher = params.fetcher;
13
+ }
14
+ async run(input) {
15
+ const lastIndex = input.elasticsearchToDynamoDb?.index;
16
+ let cursor = input.elasticsearchToDynamoDb?.cursor;
17
+ const indexes = await this.fetchAllIndexes();
18
+ let next = 0;
19
+ if (lastIndex) {
20
+ next = indexes.findIndex(index => index === lastIndex);
21
+ }
22
+ let currentIndex = indexes[next];
23
+ while (currentIndex) {
24
+ if (this.manager.isAborted()) {
25
+ return this.manager.response.aborted();
26
+ }
27
+ /**
28
+ * We will put 180 seconds because we are writing to the Elasticsearch/OpenSearch directly.
29
+ * We want to leave enough time for possible retries.
30
+ */
31
+ //
32
+ else if (this.manager.isCloseToTimeout(180)) {
33
+ return this.manager.response.continue({
34
+ ...input,
35
+ elasticsearchToDynamoDb: {
36
+ ...input.elasticsearchToDynamoDb,
37
+ index: currentIndex,
38
+ cursor
39
+ }
40
+ });
41
+ }
42
+ const result = await this.fetcher.fetch({
43
+ index: currentIndex,
44
+ cursor,
45
+ limit: 100
46
+ });
47
+ const syncResult = await this.synchronize.execute({
48
+ done: result.done,
49
+ index: currentIndex,
50
+ items: result.items
51
+ });
52
+ if (!syncResult.done && result.cursor) {
53
+ cursor = result.cursor;
54
+ continue;
55
+ }
56
+ cursor = undefined;
57
+ const next = indexes.findIndex(index => index === currentIndex) + 1;
58
+ currentIndex = indexes[next];
59
+ }
60
+ return this.manager.response.continue({
61
+ ...input,
62
+ elasticsearchToDynamoDb: {
63
+ finished: true
64
+ }
65
+ });
66
+ }
67
+ async fetchAllIndexes() {
68
+ const result = await this.indexManager.list();
69
+ if (result.length > 0) {
70
+ return result;
71
+ }
72
+ throw new Error("No Elasticsearch / OpenSearch indexes found.");
73
+ }
74
+ }
75
+ exports.ElasticsearchToDynamoDbSynchronization = ElasticsearchToDynamoDbSynchronization;
76
+
77
+ //# sourceMappingURL=ElasticsearchToDynamoDbSynchronization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["ElasticsearchToDynamoDbSynchronization","constructor","params","manager","indexManager","synchronize","fetcher","run","input","lastIndex","elasticsearchToDynamoDb","index","cursor","indexes","fetchAllIndexes","next","findIndex","currentIndex","isAborted","response","aborted","isCloseToTimeout","continue","result","fetch","limit","syncResult","execute","done","items","undefined","finished","list","length","Error","exports"],"sources":["ElasticsearchToDynamoDbSynchronization.ts"],"sourcesContent":["import {\n IDataSynchronizationInput,\n IDataSynchronizationManager,\n IElasticsearchSyncParams,\n ISynchronization,\n ISynchronizationRunResult\n} from \"../types\";\nimport { IIndexManager } from \"~/settings/types\";\nimport { NonEmptyArray } from \"@webiny/api/types\";\nimport { IElasticsearchSynchronize } from \"./abstractions/ElasticsearchSynchronize\";\nimport { IElasticsearchFetcher } from \"./abstractions/ElasticsearchFetcher\";\n\nexport class ElasticsearchToDynamoDbSynchronization implements ISynchronization {\n private readonly manager: IDataSynchronizationManager;\n private readonly indexManager: IIndexManager;\n private readonly synchronize: IElasticsearchSynchronize;\n private readonly fetcher: IElasticsearchFetcher;\n\n public constructor(params: IElasticsearchSyncParams) {\n this.manager = params.manager;\n this.indexManager = params.indexManager;\n this.synchronize = params.synchronize;\n this.fetcher = params.fetcher;\n }\n\n public async run(input: IDataSynchronizationInput): Promise<ISynchronizationRunResult> {\n const lastIndex = input.elasticsearchToDynamoDb?.index;\n let cursor = input.elasticsearchToDynamoDb?.cursor;\n const indexes = await this.fetchAllIndexes();\n\n let next = 0;\n if (lastIndex) {\n next = indexes.findIndex(index => index === lastIndex);\n }\n\n let currentIndex = indexes[next];\n\n while (currentIndex) {\n if (this.manager.isAborted()) {\n return this.manager.response.aborted();\n }\n /**\n * We will put 180 seconds because we are writing to the Elasticsearch/OpenSearch directly.\n * We want to leave enough time for possible retries.\n */\n //\n else if (this.manager.isCloseToTimeout(180)) {\n return this.manager.response.continue({\n ...input,\n elasticsearchToDynamoDb: {\n ...input.elasticsearchToDynamoDb,\n index: currentIndex,\n cursor\n }\n });\n }\n\n const result = await this.fetcher.fetch({\n index: currentIndex,\n cursor,\n limit: 100\n });\n\n const syncResult = await this.synchronize.execute({\n done: result.done,\n index: currentIndex,\n items: result.items\n });\n\n if (!syncResult.done && result.cursor) {\n cursor = result.cursor;\n continue;\n }\n cursor = undefined;\n\n const next = indexes.findIndex(index => index === currentIndex) + 1;\n currentIndex = indexes[next];\n }\n\n return this.manager.response.continue({\n ...input,\n elasticsearchToDynamoDb: {\n finished: true\n }\n });\n }\n\n private async fetchAllIndexes(): Promise<NonEmptyArray<string>> {\n const result = await this.indexManager.list();\n if (result.length > 0) {\n return result as NonEmptyArray<string>;\n }\n throw new Error(\"No Elasticsearch / OpenSearch indexes found.\");\n }\n}\n"],"mappings":";;;;;;AAYO,MAAMA,sCAAsC,CAA6B;EAMrEC,WAAWA,CAACC,MAAgC,EAAE;IACjD,IAAI,CAACC,OAAO,GAAGD,MAAM,CAACC,OA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AACrB,WAAW,CAACsB,OAAO,CAAC;QAC9CC,IAAI,EAAEL,MAAM,CAACK,IAAI;QACjBjB,KAAK,EAAEM,YAAY;QACnBY,KAAK,EAAEN,MAAM,CAACM;MAClB,CAAC,CAAC;MAEF,IAAI,CAACH,UAAU,CAACE,IAAI,IAAIL,MAAM,CAACX,MAAM,EAAE;QACnCA,MAAM,GAAGW,MAAM,CAACX,MAAM;QACtB;MACJ;MACAA,MAAM,GAAGkB,SAAS;MAElB,MAAMf,IAAI,GAAGF,OAAO,CAACG,SAAS,CAACL,KAAK,IAAIA,KAAK,KAAKM,YAAY,CAAC,GAAG,CAAC;MACnEA,YAAY,GAAGJ,OAAO,CAACE,IAAI,CAAC;IAChC;IAEA,OAAO,IAAI,CAACZ,OAAO,CAACgB,QAAQ,CAACG,QAAQ,CAAC;MAClC,GAAGd,KAAK;MACRE,uBAAuB,EAAE;QACrBqB,QAAQ,EAAE;MACd;IACJ,CAAC,CAAC;EACN;EAEA,MAAcjB,eAAeA,CAAA,EAAmC;IAC5D,MAAMS,MAAM,GAAG,MAAM,IAAI,CAACnB,YAAY,CAAC4B,IAAI,CAAC,CAAC;IAC7C,IAAIT,MAAM,CAACU,MAAM,GAAG,CAAC,EAAE;MACnB,OAAOV,MAAM;IACjB;IACA,MAAM,IAAIW,KAAK,CAAC,8CAA8C,CAAC;EACnE;AACJ;AAACC,OAAA,CAAAnC,sCAAA,GAAAA,sCAAA","ignoreList":[]}
@@ -0,0 +1,21 @@
1
+ import { PrimitiveValue } from "@webiny/api-elasticsearch/types";
2
+ export interface IElasticsearchFetcherFetchResponseItem {
3
+ PK: string;
4
+ SK: string;
5
+ _id: string;
6
+ index: string;
7
+ }
8
+ export interface IElasticsearchFetcherFetchParams {
9
+ index: string;
10
+ cursor?: PrimitiveValue[];
11
+ limit: number;
12
+ }
13
+ export interface IElasticsearchFetcherFetchResponse {
14
+ done: boolean;
15
+ totalCount: number;
16
+ cursor?: PrimitiveValue[];
17
+ items: IElasticsearchFetcherFetchResponseItem[];
18
+ }
19
+ export interface IElasticsearchFetcher {
20
+ fetch(params: IElasticsearchFetcherFetchParams): Promise<IElasticsearchFetcherFetchResponse>;
21
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ //# sourceMappingURL=ElasticsearchFetcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["ElasticsearchFetcher.ts"],"sourcesContent":["import { PrimitiveValue } from \"@webiny/api-elasticsearch/types\";\n\nexport interface IElasticsearchFetcherFetchResponseItem {\n PK: string;\n SK: string;\n _id: string;\n index: string;\n}\n\nexport interface IElasticsearchFetcherFetchParams {\n index: string;\n cursor?: PrimitiveValue[];\n limit: number;\n}\n\nexport interface IElasticsearchFetcherFetchResponse {\n done: boolean;\n totalCount: number;\n cursor?: PrimitiveValue[];\n items: IElasticsearchFetcherFetchResponseItem[];\n}\n\nexport interface IElasticsearchFetcher {\n fetch(params: IElasticsearchFetcherFetchParams): Promise<IElasticsearchFetcherFetchResponse>;\n}\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,17 @@
1
+ export interface IElasticsearchSynchronizeExecuteParamsItem {
2
+ PK: string;
3
+ SK: string;
4
+ _id: string;
5
+ index: string;
6
+ }
7
+ export interface IElasticsearchSynchronizeExecuteParams {
8
+ done: boolean;
9
+ index: string;
10
+ items: IElasticsearchSynchronizeExecuteParamsItem[];
11
+ }
12
+ export interface IElasticsearchSynchronizeExecuteResponse {
13
+ done: boolean;
14
+ }
15
+ export interface IElasticsearchSynchronize {
16
+ execute(params: IElasticsearchSynchronizeExecuteParams): Promise<IElasticsearchSynchronizeExecuteResponse>;
17
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ //# sourceMappingURL=ElasticsearchSynchronize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["ElasticsearchSynchronize.ts"],"sourcesContent":["export interface IElasticsearchSynchronizeExecuteParamsItem {\n PK: string;\n SK: string;\n _id: string;\n index: string;\n}\n\nexport interface IElasticsearchSynchronizeExecuteParams {\n done: boolean;\n index: string;\n items: IElasticsearchSynchronizeExecuteParamsItem[];\n}\n\nexport interface IElasticsearchSynchronizeExecuteResponse {\n done: boolean;\n}\nexport interface IElasticsearchSynchronize {\n execute(\n params: IElasticsearchSynchronizeExecuteParams\n ): Promise<IElasticsearchSynchronizeExecuteResponse>;\n}\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,2 @@
1
+ import WebinyError from "@webiny/error";
2
+ export declare const shouldIgnoreEsResponseError: (error: WebinyError) => boolean;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.shouldIgnoreEsResponseError = void 0;
7
+ const IGNORED_ES_SEARCH_EXCEPTIONS = ["index_not_found_exception", "search_phase_execution_exception", "illegal_argument_exception"];
8
+ const shouldIgnoreEsResponseError = error => {
9
+ return IGNORED_ES_SEARCH_EXCEPTIONS.includes(error.message);
10
+ };
11
+ exports.shouldIgnoreEsResponseError = shouldIgnoreEsResponseError;
12
+
13
+ //# sourceMappingURL=shouldIgnoreEsResponseError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["IGNORED_ES_SEARCH_EXCEPTIONS","shouldIgnoreEsResponseError","error","includes","message","exports"],"sources":["shouldIgnoreEsResponseError.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\n\nconst IGNORED_ES_SEARCH_EXCEPTIONS = [\n \"index_not_found_exception\",\n \"search_phase_execution_exception\",\n \"illegal_argument_exception\"\n];\n\nexport const shouldIgnoreEsResponseError = (error: WebinyError) => {\n return IGNORED_ES_SEARCH_EXCEPTIONS.includes(error.message);\n};\n"],"mappings":";;;;;;AAEA,MAAMA,4BAA4B,GAAG,CACjC,2BAA2B,EAC3B,kCAAkC,EAClC,4BAA4B,CAC/B;AAEM,MAAMC,2BAA2B,GAAIC,KAAkB,IAAK;EAC/D,OAAOF,4BAA4B,CAACG,QAAQ,CAACD,KAAK,CAACE,OAAO,CAAC;AAC/D,CAAC;AAACC,OAAA,CAAAJ,2BAAA,GAAAA,2BAAA","ignoreList":[]}