UNPKG

@webiny/api-headless-cms-ddb-es

Version:

DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.

44 lines (43 loc) 1.15 kB
import DataLoader from "dataloader"; import { createBatchScheduleFn } from "./createBatchScheduleFn.js"; import { createLatestSortKey, createPartitionKey } from "../keys.js"; export const createGetLatestRevisionByEntryId = params => { const { entity, tenant, modelId } = params; const latestKey = createLatestSortKey(); 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: latestKey }); } 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=getLatestRevisionByEntryId.js.map