dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
107 lines (106 loc) • 5.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchGetCommand = void 0;
const index_js_1 = require("../../../entity/actions/parsePaths/index.js");
const index_js_2 = require("../../../errors/index.js");
const consistent_js_1 = require("../../../options/consistent.js");
const rejectExtraOptions_js_1 = require("../../../options/rejectExtraOptions.js");
const tableName_js_1 = require("../../../options/tableName.js");
const deduper_js_1 = require("../../../schema/actions/utils/deduper.js");
const index_js_3 = require("../../../table/index.js");
const isEmpty_js_1 = require("../../../utils/isEmpty.js");
const constants_js_1 = require("./constants.js");
class BatchGetCommand extends index_js_3.TableAction {
constructor(table, entities = [], requests, options = {}) {
super(table, entities);
this[constants_js_1.$requests] = requests;
this[constants_js_1.$options] = options;
}
requests(...requests) {
const entities = [];
const entityNames = new Set();
for (const request of requests) {
if (entityNames.has(request.entity.entityName)) {
continue;
}
entities.push(request.entity);
entityNames.add(request.entity.entityName);
}
return new BatchGetCommand(this.table, entities, requests, this[constants_js_1.$options]);
}
options(nextOptions) {
return new BatchGetCommand(this.table, this[index_js_3.$entities], this[constants_js_1.$requests], typeof nextOptions === 'function' ? nextOptions(this[constants_js_1.$options]) : nextOptions);
}
params() {
var _a;
const requests = this[constants_js_1.$requests];
if (requests === undefined || requests.length === 0) {
throw new index_js_2.DynamoDBToolboxError('actions.incompleteAction', {
message: 'BatchGetCommand incomplete: No BatchGetRequest supplied'
});
}
const { consistent, attributes: _attributes, tableName, ...extraOptions } = (_a = this[constants_js_1.$options]) !== null && _a !== void 0 ? _a : {};
(0, rejectExtraOptions_js_1.rejectExtraOptions)(extraOptions);
if (tableName) {
(0, tableName_js_1.parseTableNameOption)(tableName);
}
const attributes = _attributes;
let projectionExpression = undefined;
const expressionAttributeNames = {};
if (attributes !== undefined && attributes.length > 0) {
const transformedPaths = new deduper_js_1.Deduper({ serializer: value => value });
for (const entity of this[index_js_3.$entities]) {
const entityTransformedPaths = entity
.build(index_js_1.EntityPathParser)
.transform(attributes, { strict: false });
if (entityTransformedPaths.length === 0) {
throw new index_js_2.DynamoDBToolboxError('batchGetCommand.invalidProjectionExpression', {
message: `Unable to match any expression attribute path with entity: ${entity.entityName}`,
payload: { entity: entity.entityName }
});
}
for (const transformedPath of entityTransformedPaths) {
transformedPaths.push(transformedPath);
}
}
const { ExpressionAttributeNames: projectionAttributeNames, ProjectionExpression } = index_js_1.EntityPathParser.express(transformedPaths.values);
Object.assign(expressionAttributeNames, projectionAttributeNames);
projectionExpression = ProjectionExpression;
const { partitionKey, sortKey, entityAttributeSavedAs } = this.table;
const filteredAttributes = new Set(Object.values(expressionAttributeNames));
// table partitionKey and sortKey are required at all times for response re-ordering
if (!filteredAttributes.has(partitionKey.name)) {
projectionExpression += `, #_pk`;
expressionAttributeNames['#_pk'] = partitionKey.name;
}
if (sortKey !== undefined && !filteredAttributes.has(sortKey.name)) {
projectionExpression += `, #_sk`;
expressionAttributeNames['#_sk'] = sortKey.name;
}
// include the entityAttrSavedAs for faster formatting
if (!filteredAttributes.has(entityAttributeSavedAs)) {
projectionExpression += `, #_et`;
expressionAttributeNames['#_et'] = entityAttributeSavedAs;
}
}
const keys = [];
for (const request of requests) {
const key = request.params();
keys.push(key);
}
return {
[tableName !== null && tableName !== void 0 ? tableName : this.table.getName()]: {
Keys: keys,
...(consistent !== undefined ? { ConsistentRead: (0, consistent_js_1.parseConsistentOption)(consistent) } : {}),
...(projectionExpression !== undefined
? { ProjectionExpression: projectionExpression }
: {}),
...(!(0, isEmpty_js_1.isEmpty)(expressionAttributeNames)
? { ExpressionAttributeNames: expressionAttributeNames }
: {})
}
};
}
}
exports.BatchGetCommand = BatchGetCommand;
BatchGetCommand.actionName = 'batchGet';