UNPKG

@webiny/api-headless-cms-ddb

Version:

DynamoDB storage operations plugin for Headless CMS API.

56 lines (54 loc) 1.77 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGetPublishedRevisionByEntryId = void 0; var _dataloader = _interopRequireDefault(require("dataloader")); var _dbDynamodb = require("@webiny/db-dynamodb"); var _cleanup = require("@webiny/db-dynamodb/utils/cleanup"); var _keys = require("../keys"); var _utils = require("@webiny/utils"); var _createBatchScheduleFn = require("./createBatchScheduleFn"); const createGetPublishedRevisionByEntryId = params => { const { entity, locale, tenant } = params; const publishedKey = (0, _keys.createPublishedSortKey)(); return new _dataloader.default(async ids => { const queries = ids.reduce((collection, id) => { const partitionKey = (0, _keys.createPartitionKey)({ tenant, locale, id }); if (collection[partitionKey]) { return collection; } collection[partitionKey] = entity.getBatch({ PK: partitionKey, SK: publishedKey }); return collection; }, {}); const records = await (0, _dbDynamodb.batchReadAll)({ table: entity.table, items: Object.values(queries) }); const items = (0, _cleanup.cleanupItems)(entity, records); return ids.map(id => { const { id: entryId } = (0, _utils.parseIdentifier)(id); return items.filter(item => { return entryId === item.entryId; }); }); }, { batchScheduleFn: (0, _createBatchScheduleFn.createBatchScheduleFn)() }); }; exports.createGetPublishedRevisionByEntryId = createGetPublishedRevisionByEntryId; //# sourceMappingURL=getPublishedRevisionByEntryId.js.map