UNPKG

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

Version:

DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.

54 lines (53 loc) 1.34 kB
import DataLoader from "dataloader"; import { createPartitionKey, createRevisionSortKey } from "../keys.js"; import { parseIdentifier } from "@webiny/utils"; import { createBatchScheduleFn } from "./createBatchScheduleFn.js"; export const createGetRevisionById = params => { const { entity, tenant, modelId } = params; return new DataLoader(async ids => { const reader = entity.createEntityReader(); const keys = new Set(); for (const id of ids) { const partitionKey = createPartitionKey({ tenant, id }); const { version } = parseIdentifier(id); if (version === null) { continue; } const sortKey = createRevisionSortKey({ version }); const key = `${partitionKey}__${sortKey}`; if (keys.has(key)) { continue; } keys.add(key); reader.get({ PK: partitionKey, SK: sortKey }); } const items = (await reader.execute()).map(item => { return item.data; }); return ids.map(id => { return items.filter(item => { if (item.modelId !== modelId) { return false; } return id === item.id; }); }); }, { batchScheduleFn: createBatchScheduleFn() }); }; //# sourceMappingURL=getRevisionById.js.map