UNPKG

@moicky/dynamodb

Version:

Contains a collection of convenience functions for working with AWS DynamoDB

151 lines (150 loc) 5.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConditionOperations = exports.UpdateOperations = exports.CreateOperations = void 0; const lib_1 = require("../lib"); const references_1 = require("./references"); class CreateOperations { operation; transaction; constructor(operation, transaction) { this.operation = operation; this.transaction = transaction; } setReferences(refs) { Object.entries(arraysToSets(refs)).forEach(([attributeName, ref]) => { if (!ref) return; const refArgs = { item: this.operation.item, onAttribute: attributeName, }; const refData = ref instanceof Set ? [...ref].map((references) => (0, references_1.createReference)({ references, ...refArgs }, this.transaction)) : (0, references_1.createReference)({ references: ref, ...refArgs }, this.transaction); const parts = attributeName.split("."); parts.reduce((acc, part, index) => { if (index === parts.length - 1) { acc[part] = refData; } else { acc[part] = acc[part] || {}; return acc[part]; } }, this.operation.item); }); } } exports.CreateOperations = CreateOperations; class UpdateOperations { operation; transaction; constructor(operation, transaction) { this.operation = operation; this.transaction = transaction; } setReferences(refs) { Object.entries(arraysToSets(refs)).forEach(([attributeName, ref]) => { if (!ref) return; const refArgs = { item: this.operation.item, onAttribute: attributeName, }; this.operation.actions.push({ _type: "set", values: { [attributeName]: ref instanceof Set ? [...ref].map((references) => (0, references_1.createReference)({ references, ...refArgs }, this.transaction)) : (0, references_1.createReference)({ references: ref, ...refArgs }, this.transaction), }, }); }); } set(values) { if (Object.keys(values).length === 0) return this; this.operation.actions.push({ _type: "set", values }); return this; } adjustNumber(values) { if (Object.keys(values).length === 0) return this; this.operation.actions.push({ _type: "add", values }); return this; } removeAttributes(...attributes) { if (attributes.length === 0) return this; this.operation.actions.push({ _type: "remove", attributes }); return this; } addItemsToSet(values) { if (Object.keys(values).length === 0) return this; this.operation.actions.push({ _type: "add", values: arraysToSets(values), }); return this; } deleteItemsFromSet(values) { if (Object.keys(values).length === 0) return this; this.operation.actions.push({ _type: "delete", values: arraysToSets(values), }); return this; } onCondition({ expression, values, }) { this.operation.args = { ...this.operation.args, ConditionExpression: expression, ExpressionAttributeNames: (0, lib_1.getAttributeNames)(values, { attributesToGet: (0, lib_1.getAttributesFromExpression)(expression), }), ExpressionAttributeValues: (0, lib_1.getAttributesFromExpression)(expression, ":").reduce((acc, keyName) => { acc[`:${keyName}`] = values[keyName]; return acc; }, {}), }; } } exports.UpdateOperations = UpdateOperations; class ConditionOperations { operations; item; args; constructor(operations, item, args) { this.operations = operations; this.item = item; this.args = args; } matches({ expression, values, }) { if (Object.keys(values).length === 0) { throw new Error("[@moicky/dynamodb]: No values in ConditionCheck provided"); } const itemKey = (0, lib_1.getItemKey)(this.item, this.args); this.operations[itemKey] = { _type: "condition", item: this.item, args: { ConditionExpression: expression, ExpressionAttributeNames: (0, lib_1.getAttributeNames)(values), ExpressionAttributeValues: (0, lib_1.getAttributeValues)(values), ...this.args, }, }; } } exports.ConditionOperations = ConditionOperations; const arraysToSets = (values) => { return Object.entries(values).reduce((acc, [key, value]) => ({ ...acc, [key]: value instanceof Set ? value : Array.isArray(value) ? new Set(value) : value, }), {}); };