dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
62 lines (61 loc) • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrimaryKeyParser = void 0;
const dynamoDBToolboxError_js_1 = require("../../../errors/dynamoDBToolboxError.js");
const schema_js_1 = require("../../../schema/binary/schema.js");
const schema_js_2 = require("../../../schema/number/schema.js");
const schema_js_3 = require("../../../schema/string/schema.js");
const index_js_1 = require("../../../table/index.js");
const isValidPrimitive_js_1 = require("../../../utils/validation/isValidPrimitive.js");
class PrimaryKeyParser extends index_js_1.TableAction {
constructor(table) {
super(table);
}
parse(keyInput) {
const table = this.table;
const { partitionKey, sortKey } = table;
const primaryKey = {};
const partitionKeyValue = keyInput[partitionKey.name];
if (!(0, isValidPrimitive_js_1.isValidPrimitive)(getKeySchema(partitionKey), partitionKeyValue)) {
throw new dynamoDBToolboxError_js_1.DynamoDBToolboxError('actions.parsePrimaryKey.invalidKeyPart', {
message: `Invalid partition key: ${partitionKey.name}`,
path: partitionKey.name,
payload: {
expected: partitionKey.type,
received: partitionKeyValue,
keyPart: 'partitionKey'
}
});
}
primaryKey[partitionKey.name] = partitionKeyValue;
if (sortKey === undefined) {
return primaryKey;
}
const sortKeyValue = keyInput[sortKey.name];
if (!(0, isValidPrimitive_js_1.isValidPrimitive)(getKeySchema(sortKey), sortKeyValue)) {
throw new dynamoDBToolboxError_js_1.DynamoDBToolboxError('actions.parsePrimaryKey.invalidKeyPart', {
message: `Invalid sort key: ${sortKey.name}`,
path: sortKey.name,
payload: {
expected: sortKey.type,
received: sortKeyValue,
keyPart: 'sortKey'
}
});
}
primaryKey[sortKey.name] = sortKeyValue;
return primaryKey;
}
}
exports.PrimaryKeyParser = PrimaryKeyParser;
PrimaryKeyParser.actionName = 'parsePrimaryKey';
const getKeySchema = (key) => {
switch (key.type) {
case 'number':
return new schema_js_2.NumberSchema({ big: true });
case 'string':
return new schema_js_3.StringSchema({});
case 'binary':
return new schema_js_1.BinarySchema({});
}
};