UNPKG

dynamodb-fast-access

Version:

Default CRUD operations for managing AWS DynamoDB table items in an easy-to-extend structure.

83 lines (82 loc) 4.85 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ExpressionCreator_1 = require("./ExpressionCreator"); const DB_1 = require("./DB"); const DatabaseConfig_1 = require("../util/DatabaseConfig"); const Extender_1 = require("./Extender"); const RelatedDeleter_1 = require("./RelatedDeleter"); const DynamoDBFastAccessError_1 = require("../util/DynamoDBFastAccessError"); function DBMutable(tableName, extend = Extender_1.DefaultExtender, deleteRelated = RelatedDeleter_1.DefaultRelatedDeleter) { return class DBMutable extends DB_1.DB(tableName, extend, deleteRelated) { /** * Updates the provided attributes of the object with the provided id. * @param id The id of the object to update. * @param updateAttributes The attributes of the object to update. */ static updateById(id, updateAttributes) { return DBMutable.updateByIdWithDelete(id, updateAttributes, []); } static updateByIdWithDelete(id, updateAttributes, deleteAttributes) { return __awaiter(this, void 0, void 0, function* () { if ((!updateAttributes || Object.keys(updateAttributes).length === 0) && (!deleteAttributes || deleteAttributes.length === 0)) { return {}; } const tableName = DBMutable.getTableName(); const partitionKeyName = DBMutable.getPartitionKeyName(); const sortKeyName = DBMutable.getSortKeyName(); const sortKeySeparator = DBMutable.getSortKeySeparator(); const deleteAttributesObject = {}; for (const deleteAttribute of deleteAttributes) { deleteAttributesObject[deleteAttribute] = ''; } const updateParams = { TableName: tableName, Key: {}, UpdateExpression: '', ReturnValues: 'UPDATED_NEW', }; const ExpressionAttributeNames = ExpressionCreator_1.ExpressionCreator.getExpressionAttributeNames(Object.assign({}, updateAttributes, deleteAttributesObject)); const ExpressionAttributeValues = ExpressionCreator_1.ExpressionCreator.getExpressionAttributeValues(Object.assign({}, updateAttributes)); if (Object.keys(ExpressionAttributeNames).length > 0) { updateParams.ExpressionAttributeNames = ExpressionAttributeNames; } if (Object.keys(ExpressionAttributeValues).length > 0) { updateParams.ExpressionAttributeValues = ExpressionAttributeValues; } if (sortKeyName === undefined) { updateParams.Key[partitionKeyName] = id; } else { if (id.indexOf(sortKeySeparator) < 0) { throw new DynamoDBFastAccessError_1.DynamoDBFastAccessError('Composite key must include ' + sortKeySeparator + ' that separates the keys.'); } updateParams.Key[partitionKeyName] = DBMutable._castKey(id).partitionKey; updateParams.Key[sortKeyName] = DBMutable._castKey(id).sortKey; } if (updateAttributes && Object.keys(updateAttributes).length > 0) { updateParams.UpdateExpression += 'SET ' + ExpressionCreator_1.ExpressionCreator.getUpdateExpression(Object.assign({}, updateAttributes)); } if (deleteAttributes && deleteAttributes.length > 0) { updateParams.UpdateExpression += ' REMOVE ' + ExpressionCreator_1.ExpressionCreator.getUpdateExpressionRemove(deleteAttributes); } const entityData = yield DatabaseConfig_1.DatabaseConfig.DynamoDBDocumentClient().update(updateParams).promise(); if (!entityData.Attributes) return {}; return entityData.Attributes; }); } }; } exports.DBMutable = DBMutable;