amazon-qldb-kvs-nodejs
Version:
A helper module, simplifying basic interactions with Amazon Quantum Ledger Database for Node.js through a simple key-value store interface.
120 lines (117 loc) • 6.52 kB
JavaScript
;
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.getDocumentHistory = exports.getDocumentRevisionByIdAndBlock = void 0;
const Util_1 = require("./Util");
const GetDocument_1 = require("./GetDocument");
const ion_js_1 = require("ion-js");
const Logging_1 = require("./Logging");
const logger = Logging_1.log.getLogger("qldb-helper");
/**
* Gets a document version by Id and Block Sequence Number
* @param txn The {@linkcode TransactionExecutor} for lambda execute.
* @param {string} tableName The name of a table.
* @param {string} documentId An Id of the document, generated by QLDB. Can be retrieved through Utils.getDocumentIdsAndVersions function.
* @param {number} blockSequenceNo A Block Sequence Number. Can be retrieved through GetMetadata.lookupBlockAddressAndDocIdForKey
* @returns An ION document.
* @throws Error: If error happen during the process.
*/
function getDocumentRevisionByIdAndBlock(txn, tableName, documentId, blockAddress) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[GetDocumentHistory.getDocumentRevisionByIdAndBlock]";
const startTime = new Date().getTime();
try {
const blockAddressIon = ion_js_1.dom.load(blockAddress.IonText);
const blockSequenceNo = blockAddressIon.get("sequenceNo");
(0, Util_1.validateTableNameConstrains)(tableName);
const query = `SELECT * FROM history( ${tableName} ) AS h WHERE h.metadata.id = ? AND h.blockAddress.sequenceNo = ?`;
logger.debug(`${fcnName} Retrieving document values for Id: ${documentId} and Block Sequence Number: ${blockSequenceNo}`);
logger.debug(`${fcnName} Query statement: ${query}`);
const result = yield txn.execute(query, documentId, blockSequenceNo);
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
const resultList = result.getResultList();
if (resultList.length === 0) {
throw new Error(`${fcnName} Unable to find document with Id: ${documentId} and Block Sequence Number: ${blockSequenceNo}`);
}
return resultList[0];
}
catch (err) {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
throw err;
}
});
}
exports.getDocumentRevisionByIdAndBlock = getDocumentRevisionByIdAndBlock;
/**
* Gets all document versions for a specific key
* @param txn The {@linkcode TransactionExecutor} for lambda execute.
* @param {string} tableName The name of a table.
* @param {string} keyAttributeName A keyAttributeName to query.
* @param {string} keyAttributeValue The key of the given keyAttributeName.
* @param {string} fromDateISO OPTIONAL String, containing a from date and time to query revisions history from.
* @param {string} toDateISO OPTIONAL String, containing a to date and time to query revisions history to.
* @returns An ION document.
* @throws Error: If error happen during the process.
*/
function getDocumentHistory(txn, tableName, keyAttributeName, keyAttributeValue, fromDateISO, toDateISO) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[GetDocumentHistory.getDocumentRevisionByIdAndBlock]";
const startTime = new Date().getTime();
let documentId;
try {
const documentIds = yield (0, GetDocument_1.getDocumentIdsAndVersions)(txn, tableName, keyAttributeName, keyAttributeValue);
documentId = documentIds[0].id;
(0, Util_1.validateTableNameConstrains)(tableName);
let query = `SELECT * FROM history( ${tableName} ) AS h WHERE h.metadata.id = ? `;
if (fromDateISO) {
(0, Util_1.validateStringAsISODateTime)(fromDateISO);
if (toDateISO) {
(0, Util_1.validateStringAsISODateTime)(toDateISO);
query = `SELECT * FROM history( ${tableName}, \`${fromDateISO}\`, \`${toDateISO}\`) AS h WHERE h.metadata.id = ? `;
}
else {
query = `SELECT * FROM history( ${tableName}, \`${fromDateISO}\`) AS h WHERE h.metadata.id = ? `;
}
}
logger.debug(`${fcnName} Retrieving document history for Id: ${documentId}`);
logger.debug(`${fcnName} Query statement: ${query}`);
const result = yield txn.execute(query, documentId);
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
const resultList = result.getResultList();
if (resultList.length === 0) {
throw `${fcnName} Unable to find document history with Id: ${documentId}`;
}
return resultList;
}
catch (err) {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
throw new Error(`${fcnName} ${err}`);
}
});
}
exports.getDocumentHistory = getDocumentHistory;