@webiny/api-headless-cms-ddb-es
Version:
DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.
44 lines (43 loc) • 1.17 kB
JavaScript
import DataLoader from "dataloader";
import { createPartitionKey, createPublishedSortKey } from "../keys.js";
import { createBatchScheduleFn } from "./createBatchScheduleFn.js";
export const createGetPublishedRevisionByEntryId = params => {
const {
entity,
tenant,
modelId
} = params;
const publishedKey = createPublishedSortKey();
return new DataLoader(async ids => {
const reader = entity.createEntityReader();
const keys = new Set();
for (const id of ids) {
const partitionKey = createPartitionKey({
tenant,
id
});
if (keys.has(partitionKey)) {
continue;
}
keys.add(partitionKey);
reader.get({
PK: partitionKey,
SK: publishedKey
});
}
const items = (await reader.execute()).map(item => {
return item.data;
});
return ids.map(entryId => {
return items.filter(item => {
if (item.modelId !== modelId) {
return false;
}
return entryId === item.entryId;
});
});
}, {
batchScheduleFn: createBatchScheduleFn()
});
};
//# sourceMappingURL=getPublishedRevisionByEntryId.js.map