UNPKG

@studyportals/sp-r2d2

Version:

A framework that contains various components used when developing projects that will be deployed via AWS λ.

197 lines 7.97 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 }); exports.DynamoDBAdapter = void 0; const client_dynamodb_1 = require("@aws-sdk/client-dynamodb"); const __1 = require("../"); const attribute_map_normalizer_class_1 = require("./attribute-map-normalizer.class"); const batch_get_item_result_class_1 = require("./batch-get-item-result.class"); const dynamodb_items_per_table_class_1 = require("./dynamodb-items-per-table.class"); const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb"); class DynamoDBAdapter { get region() { return this._region; } get dynamoDB() { if (!this._dynamoDB) { this._dynamoDB = new client_dynamodb_1.DynamoDB({ region: this.region }); } return this._dynamoDB; } get dynamoDBClient() { if (!this._dynamoDBClient) { this._dynamoDBClient = new client_dynamodb_1.DynamoDBClient({ region: this.region }); } return this._dynamoDBClient; } get dynamoDBDocumentClient() { if (!this._dynamoDBDocumentClient) { this._dynamoDBDocumentClient = lib_dynamodb_1.DynamoDBDocumentClient.from(this.dynamoDBClient); } return this._dynamoDBDocumentClient; } constructor(region = '') { this.attributeMapNormalizer = new attribute_map_normalizer_class_1.AttributeMapNormalizer(); this._region = region; } query(input) { return __awaiter(this, void 0, void 0, function* () { const output = yield this.dynamoDB.query(input); if (!output) { return []; } const items = this.handleQueryOutput(output); if (undefined === output.LastEvaluatedKey) { return items; } const nextPageItems = yield this.queryNextPage(input, output); return items.concat(nextPageItems); }); } queryNextPage(input, lastPageOutput) { return __awaiter(this, void 0, void 0, function* () { const nextPageInput = Object.assign({}, input); nextPageInput.ExclusiveStartKey = lastPageOutput.LastEvaluatedKey; return yield this.query(nextPageInput); }); } batchGetItem(input) { return __awaiter(this, void 0, void 0, function* () { const output = yield this.dynamoDB.batchGetItem(input); if (!output) { throw new Error('No output from batchGetItem'); } const items = this.handleGetBatchItemOutput(output); if (output.UnprocessedKeys && Object.keys(output.UnprocessedKeys).length) { yield this.addResultsOfNextBatchGetItemsPages(items, output.UnprocessedKeys); } return items; }); } addResultsOfNextBatchGetItemsPages(previousPageResult, unprocessedKeys) { return __awaiter(this, void 0, void 0, function* () { const nextItems = yield this.batchGetItem({ RequestItems: unprocessedKeys }); if (!nextItems) { return; } previousPageResult.merge(nextItems); }); } scan(input) { return __awaiter(this, void 0, void 0, function* () { const output = yield this.dynamoDB.scan(input); if (!output) { return []; } const items = this.handleScanOutput(output); if (undefined === output.LastEvaluatedKey) { return items; } const nextPageItems = yield this.scanNextPage(input, output); return items.concat(nextPageItems); }); } scanNextPage(input, output) { return __awaiter(this, void 0, void 0, function* () { const nextPageInput = Object.assign({}, input); nextPageInput.ExclusiveStartKey = output.LastEvaluatedKey; return yield this.scan(nextPageInput); }); } putItem(input) { return __awaiter(this, void 0, void 0, function* () { try { yield this.dynamoDB.putItem(input); } catch (err) { console.error('An error occurred when attempting to put item: ', err); throw err; } }); } getItem(input) { return __awaiter(this, void 0, void 0, function* () { try { const response = yield this.dynamoDB.getItem(input); return this.handleGetItemOutput(response); } catch (err) { console.error('An error occurred when attempting to get item: ', err); throw err; } }); } updateItem(input) { return __awaiter(this, void 0, void 0, function* () { try { return yield this.dynamoDB.updateItem(input); } catch (err) { console.error('An error occurred when attempting to update item: ', err); throw err; } }); } documentUpdateItem(input) { return __awaiter(this, void 0, void 0, function* () { if (input.ReturnValues === undefined) { input.ReturnValues = 'ALL_NEW'; } const command = new lib_dynamodb_1.UpdateCommand(input); return this.dynamoDBDocumentClient.send(command); }); } deleteItem(input) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => this.dynamoDB.deleteItem(input, (err) => (err ? reject(err) : resolve()))); }); } beginWriteTransaction() { return new __1.DynamoDBTransactionWrite(this); } commitWriteTransaction(transaction) { return __awaiter(this, void 0, void 0, function* () { const items = transaction.getItems(); if (items.length > 0) { const input = { TransactItems: transaction.getItems(), }; return new Promise((resolve, reject) => this.dynamoDB.transactWriteItems(input, (err) => (err ? reject(err) : resolve()))); } }); } handleQueryOutput(data) { return data.Items ? this.attributeMapNormalizer.normalizeMaps(data.Items) : []; } handleGetItemOutput(data) { if (!data) { return null; } return data.Item ? this.attributeMapNormalizer.normalizeMap(data.Item) : null; } handleScanOutput(data) { return data.Items ? this.attributeMapNormalizer.normalizeMaps(data.Items) : []; } handleGetBatchItemOutput(data) { const items = []; const responses = data.Responses; if (responses) { const tableNames = Object.keys(responses); tableNames.forEach((tableName) => { const output = this.attributeMapNormalizer.normalizeMaps(responses[tableName]); items.push(new dynamodb_items_per_table_class_1.DynamoDBItemsPerTable(tableName, output)); }); } return new batch_get_item_result_class_1.BatchGetItemsResult(items); } } exports.DynamoDBAdapter = DynamoDBAdapter; //# sourceMappingURL=dynamodb-adapter.class.js.map