UNPKG

@webiny/api-headless-cms-ddb

Version:

DynamoDB storage operations plugin for Headless CMS API.

56 lines (54 loc) 1.75 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGetLatestRevisionByEntryId = void 0; var _dataloader = _interopRequireDefault(require("dataloader")); var _dbDynamodb = require("@webiny/db-dynamodb"); var _cleanup = require("@webiny/db-dynamodb/utils/cleanup"); var _createBatchScheduleFn = require("./createBatchScheduleFn"); var _keys = require("../keys"); var _utils = require("@webiny/utils"); const createGetLatestRevisionByEntryId = params => { const { entity, locale, tenant } = params; const latestKey = (0, _keys.createLatestSortKey)(); 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: latestKey }); 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.createGetLatestRevisionByEntryId = createGetLatestRevisionByEntryId; //# sourceMappingURL=getLatestRevisionByEntryId.js.map