@openstax/ts-utils 1.34.1 → 1.35.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.
- package/dist/cjs/services/searchProvider/index.d.ts +2 -0
- package/dist/cjs/services/searchProvider/openSearch.js +10 -2
- package/dist/cjs/services/searchProvider/streamIndexer.d.ts +17 -0
- package/dist/cjs/services/searchProvider/streamIndexer.js +41 -0
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/searchProvider/index.d.ts +2 -0
- package/dist/esm/services/searchProvider/openSearch.js +10 -2
- package/dist/esm/services/searchProvider/streamIndexer.d.ts +17 -0
- package/dist/esm/services/searchProvider/streamIndexer.js +37 -0
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +7 -1
|
@@ -60,7 +60,9 @@ export const openSearchService = (initializer = {}) => (configProvider) => {
|
|
|
60
60
|
index: indexConfig.name,
|
|
61
61
|
body: params.body,
|
|
62
62
|
id: params.id,
|
|
63
|
-
refresh: true
|
|
63
|
+
refresh: true,
|
|
64
|
+
version: params.version,
|
|
65
|
+
version_type: params.version_type
|
|
64
66
|
}, {
|
|
65
67
|
requestTimeout: 10000,
|
|
66
68
|
maxRetries: 1,
|
|
@@ -71,7 +73,13 @@ export const openSearchService = (initializer = {}) => (configProvider) => {
|
|
|
71
73
|
await openSearchClient.bulk({
|
|
72
74
|
index: indexConfig.name,
|
|
73
75
|
body: items.flatMap((item) => [
|
|
74
|
-
{
|
|
76
|
+
{
|
|
77
|
+
index: {
|
|
78
|
+
_id: item.id,
|
|
79
|
+
...(item.version !== undefined ? { version: item.version } : {}),
|
|
80
|
+
...(item.version_type !== undefined ? { version_type: item.version_type } : {})
|
|
81
|
+
}
|
|
82
|
+
},
|
|
75
83
|
item.body
|
|
76
84
|
]),
|
|
77
85
|
refresh: true
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DynamoDBRecord } from 'aws-lambda';
|
|
2
|
+
import { Logger } from '../logger';
|
|
3
|
+
import { IndexOptions } from '../searchProvider';
|
|
4
|
+
export interface SearchProvider<T> {
|
|
5
|
+
index: (options: IndexOptions<T>) => Promise<void>;
|
|
6
|
+
bulkIndex: (items: IndexOptions<T>[]) => Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
export interface SearchIndexUpdaterConfig<T> {
|
|
9
|
+
searchProvider: SearchProvider<T>;
|
|
10
|
+
logger: Logger;
|
|
11
|
+
getDocumentId: (document: T) => string;
|
|
12
|
+
getDocumentVersion?: (document: T) => number | undefined;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Function to update search index from DynamoDB Stream records.
|
|
16
|
+
*/
|
|
17
|
+
export declare const updateSearchIndexFromStream: <T>(records: DynamoDBRecord[], config: SearchIndexUpdaterConfig<T>) => Promise<void>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { unmarshall } from '@aws-sdk/util-dynamodb';
|
|
2
|
+
import { Level } from '../logger';
|
|
3
|
+
const VALID_EVENTS = new Set(['INSERT', 'MODIFY']);
|
|
4
|
+
/**
|
|
5
|
+
* Function to update search index from DynamoDB Stream records.
|
|
6
|
+
*/
|
|
7
|
+
export const updateSearchIndexFromStream = async (records, config) => {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
const { searchProvider, logger, getDocumentId, getDocumentVersion } = config;
|
|
10
|
+
const itemsToIndex = [];
|
|
11
|
+
for (const record of records) {
|
|
12
|
+
if (!record.eventName || !VALID_EVENTS.has(record.eventName)) {
|
|
13
|
+
logger.log(`Skipping ${record.eventName || 'undefined'} event`, Level.Error);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!((_a = record.dynamodb) === null || _a === void 0 ? void 0 : _a.NewImage)) {
|
|
17
|
+
logger.log('Missing NewImage in record, skipping', Level.Error);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const document = unmarshall(record.dynamodb.NewImage);
|
|
21
|
+
const documentId = getDocumentId(document);
|
|
22
|
+
const indexOptions = {
|
|
23
|
+
id: documentId,
|
|
24
|
+
body: document,
|
|
25
|
+
};
|
|
26
|
+
const version = (_b = getDocumentVersion === null || getDocumentVersion === void 0 ? void 0 : getDocumentVersion(document)) !== null && _b !== void 0 ? _b : record.dynamodb.ApproximateCreationDateTime;
|
|
27
|
+
if (version !== undefined) {
|
|
28
|
+
indexOptions.version = version;
|
|
29
|
+
indexOptions.version_type = 'external';
|
|
30
|
+
}
|
|
31
|
+
itemsToIndex.push(indexOptions);
|
|
32
|
+
}
|
|
33
|
+
if (itemsToIndex.length > 0) {
|
|
34
|
+
await searchProvider.bulkIndex(itemsToIndex);
|
|
35
|
+
logger.log(`Successfully indexed ${itemsToIndex.length} documents`);
|
|
36
|
+
}
|
|
37
|
+
};
|