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.
661 lines (658 loc) • 33.5 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.QLDBKVS = void 0;
const client_qldb_1 = require("@aws-sdk/client-qldb");
const QLDBHelper_1 = require("./QLDBHelper");
const GetDocument_1 = require("./GetDocument");
const GetDigest_1 = require("./GetDigest");
const GetMetadata_1 = require("./GetMetadata");
const GetRevision_1 = require("./GetRevision");
const UpsertDocument_1 = require("./UpsertDocument");
const GetDocumentHistory_1 = require("./GetDocumentHistory");
const VerifyLedgerMetadata_1 = require("./VerifyLedgerMetadata");
const VerifyRevisionHash_1 = require("./VerifyRevisionHash");
const Logging_1 = require("./Logging");
const ConnectToLedger_1 = require("./ConnectToLedger");
const Constants_1 = require("./Constants");
const ion_js_1 = require("ion-js");
const qldbClient = new client_qldb_1.QLDB();
const fs = require('fs');
const Util = require('util');
const { sleep, validateTableNameConstrains, validateLedgerNameConstrains } = require('./Util');
const logger = Logging_1.log.getLogger("qldb-kvs");
const mkdir = Util.promisify(fs.mkdir);
const writeFile = Util.promisify(fs.writeFile);
const readFile = Util.promisify(fs.readFile);
class QLDBKVS {
/**
* Initialize QLDBKVS object
* @param ledgerName A name of QLDB ledger to use
* @param tableName A name of QLDB table
* @param checkForTable A boolean value to check table if table exists and create if it is not exests (dafault=true)
* @param maxConcurrentTransactions The driver internally uses a pool of sessions to execute the transactions.
* The maxConcurrentTransactions parameter specifies the number of sessions that the driver can hold in the pool.
* @returns {QLDBKVS} initialized
*/
constructor(ledgerName, tableName, checkForTable, maxConcurrentTransactions = 5) {
const fcnName = "[QLDBKVS.constructor]";
try {
if (!ledgerName) {
throw new Error(`${fcnName}: Please specify ledgerName`);
}
if (!tableName) {
throw new Error(`${fcnName}: Please specify tableName, which is the name of a table you are planning to use`);
}
const checkForTableAndCreate = typeof checkForTable == "boolean" ? checkForTable : true;
validateLedgerNameConstrains(ledgerName);
validateTableNameConstrains(tableName);
this.ledgerName = ledgerName;
this.tableName = tableName;
this.tableState = "CHECKING";
logger.debug(`${fcnName} Creating QLDB driver`);
this.qldbDriver = (0, ConnectToLedger_1.createQldbDriver)(ledgerName, {}, maxConcurrentTransactions);
logger.debug(`${fcnName} QLDB driver created`);
if (checkForTableAndCreate) {
// Making sure the table exists and set it for creation
// next time somebody will decide to submit a new document to QLDB
(() => __awaiter(this, void 0, void 0, function* () {
//// Listing tables names
logger.info(`${fcnName} Listing table names...`);
let tableNames = yield (0, QLDBHelper_1.listTables)(this.qldbDriver);
tableNames = tableNames.map(x => { return x.toUpperCase(); });
//// Checking if table is already created and create if not
logger.info(`${fcnName} Checking if table with name ${tableName} exists`);
if (tableNames.indexOf(tableName.toUpperCase()) >= 0) {
this.tableState = "EXIST";
}
else {
this.tableState = "NOT_EXIST";
}
if (this.tableState === "NOT_EXIST") {
this.tableState = "CREATING";
logger.info(`${fcnName} Looks like a table with name ${tableName} does not exist. Creating it.`);
yield (0, QLDBHelper_1.createTableWithIndex)(this.qldbDriver, tableName, Constants_1.KEY_ATTRIBUTE_NAME);
this.tableState = "EXIST";
return true;
}
}))();
}
else {
this.tableState = "EXIST";
}
}
catch (err) {
const msg = `Could not construct an instance of QLDB KVS class`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
return this;
}
/**
* Verify that table exists and create it if it doesn't
* @param tableName string The name of the table
* @returns A promise with "true" if table exists
* @throws Error: If error happen during the process.
*/
assureTableExists(tableName) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.assureTableExists]";
// In case our table has not been created yet, waiting for it to be created
let cycles = Constants_1.TABLE_CREATION_MAX_WAIT / 100;
logger.debug(`${fcnName} Table with name ${tableName} still does not exist, waiting for it to be created.`);
do {
yield sleep(100);
cycles--;
if (this.tableState === "EXIST") {
return true;
}
if (cycles === 0) {
throw new Error(`Could not create a table with name ${tableName} in ${Constants_1.TABLE_CREATION_MAX_WAIT} milliseconds`);
}
} while (this.tableState === "CREATING" || this.tableState === "CHECKING");
});
}
/**
* Download a value as a file to the local file system
* @param key A value of a key attribute to retrieve the document.
* @param localFilePath A path on a local file system where to store the result file
* @returns A promise with a path to a new file, retrieved from QLDB.
* @throws Error: If error happen during the process.
*/
downloadAsFile(key, localFilePath) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.downloadAsFile]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const paramId = key;
const filePath = localFilePath ? localFilePath : Constants_1.DEFAULT_DOWNLOADS_PATH + key;
const startTime = new Date().getTime();
//const qldbHelper: QLDBHelper = this.qldbHelper;
try {
if (!paramId) {
throw new Error(`${fcnName}: Please specify key`);
}
if (!localFilePath) {
throw new Error(`${fcnName}: Please specify localFilePath`);
}
logger.debug(`${fcnName} Getting ${paramId} from ledger ${ledgerName} and table ${tableName} to ${filePath}`);
if (!localFilePath && !fs.existsSync(Constants_1.DEFAULT_DOWNLOADS_PATH)) {
yield mkdir(Constants_1.DEFAULT_DOWNLOADS_PATH);
}
const result = yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield (0, GetDocument_1.getByKeyAttribute)(txn, tableName, Constants_1.KEY_ATTRIBUTE_NAME, paramId).catch((err) => {
throw `Unable to get file By Key Attribute: ${err}`;
});
}));
const valueBase64 = result[0].data.get(Constants_1.VALUE_ATTRIBUTE_NAME).stringValue();
const valueObject = Buffer.from(valueBase64, "base64");
yield writeFile(localFilePath, valueObject);
return localFilePath;
}
catch (err) {
//throw `${fcnName}: ${err}`;
const msg = `Requested document does not exist`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
//Upload a file to s3 key
/**
* Upload file to QLDB as utf8 buffer (blob)
* @param key A value of a key attribute.
* @param filePath A path to a file on a local file system.
* @returns A promise with an object containing a document Id and transaction Id
* @throws Error: If error happen during the process.
*/
uploadAsFile(key, filePath) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.uploadAsFile]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const paramId = key;
const startTime = new Date().getTime();
try {
if (!paramId) {
throw new Error(`${fcnName}: Please specify key`);
}
if (!filePath) {
throw new Error(`${fcnName}: Please specify filePath`);
}
logger.debug(`${fcnName} Start uploading file ${filePath} to ledger ${ledgerName} and table ${tableName} under the key ${paramId}`);
const doc = {};
doc[Constants_1.KEY_ATTRIBUTE_NAME] = key;
const fileBuffer = yield readFile(filePath);
doc[Constants_1.VALUE_ATTRIBUTE_NAME] = fileBuffer.toString("base64");
const documentObjectSize = doc[Constants_1.KEY_ATTRIBUTE_NAME].length + doc[Constants_1.VALUE_ATTRIBUTE_NAME].length;
if (documentObjectSize > Constants_1.MAX_QLDB_DOCUMENT_SIZE) {
logger.info(`${fcnName} Unable to upload files larger than ${Constants_1.MAX_QLDB_DOCUMENT_SIZE} bytes. Current size: ${documentObjectSize}`);
return null;
}
logger.debug(`${fcnName} Length of an object is ${documentObjectSize}`);
yield this.assureTableExists(tableName);
return yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield (0, UpsertDocument_1.upsert)(txn, tableName, Constants_1.KEY_ATTRIBUTE_NAME, doc).catch((err) => {
throw err;
});
}));
}
catch (err) {
const msg = `Could not upload the file`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Get value for a corresponding key as JSON object
* @param key A value of a key attribute to retrieve the record from.
* @param withVersionNumber If true, return the value along with it's version number from the ledger
* @returns Promise with a value object as JSON.
*/
getValue(key, withVersionNumber) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.getValue]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const paramId = key;
const startTime = new Date().getTime();
try {
if (!key) {
throw new Error(`${fcnName}: Please specify a key`);
}
logger.debug(`${fcnName} Getting ${paramId} from ledger ${ledgerName} and table ${tableName} into a JSON object. (Expecting utf8 encoded string)`);
const result = yield this.getValues([key], withVersionNumber);
const valueObject = result[0];
if (!valueObject) {
throw `Requested document does not exist`;
}
return valueObject;
}
catch (err) {
const msg = `Requested document does not exist`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Get values by array of keys
* @param keys An array of values of key attribute to retrieve the record from.
* @param withVersionNumbers If true, returns values along with their version numbers from the ledger
* @returns Promise with an array of value objects as JSON.
*/
getValues(keys, withVersionNumbers) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.getValues]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const paramIds = keys;
const startTime = new Date().getTime();
try {
if (!paramIds) {
throw new Error(`${fcnName}: Please specify an array of keys`);
}
logger.debug(`${fcnName} Getting ${paramIds} from ledger ${ledgerName} and table ${tableName} into a JSON object. (Expecting utf8 encoded string)`);
const result = yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield (0, GetDocument_1.getByKeyAttributes)(txn, tableName, Constants_1.KEY_ATTRIBUTE_NAME, paramIds).catch((err) => {
throw `Unable to get key by attributes: ${err}`;
});
}));
logger.debug(`${fcnName} Got result: ${JSON.stringify(result)}`);
if (!result) {
throw `Requested documents do not exist`;
}
const returnObjects = new Array(result.length);
for (let index = 0; index < result.length; index++) {
const resultData = result[index].data;
const valueVersion = result[index].version;
//const valueObject = resultData.get(VALUE_ATTRIBUTE_NAME).stringValue();
let returnValue;
// If we have an attribute value set, we assume the value is a string
if (resultData.get(Constants_1.VALUE_ATTRIBUTE_NAME)) {
returnValue = resultData.get(Constants_1.VALUE_ATTRIBUTE_NAME).stringValue();
}
else {
let returnValueTMP;
try {
returnValueTMP = JSON.parse(JSON.stringify(resultData));
delete returnValueTMP[Constants_1.KEY_ATTRIBUTE_NAME];
}
catch (err) {
throw `Unable to convert to JSON an Ion object with key ${resultData.get(Constants_1.KEY_ATTRIBUTE_NAME).stringValue()}: ${err}`;
}
returnValue = JSON.parse(JSON.stringify(returnValueTMP));
}
returnObjects[index] = {
// Result order is non-deterministic, hence key is returned
key: resultData.get(Constants_1.KEY_ATTRIBUTE_NAME).stringValue(),
data: returnValue,
};
if (withVersionNumbers) {
returnObjects[index].version = valueVersion;
}
if (index === result.length - 1) {
return returnObjects;
}
}
}
catch (err) {
const msg = `Requested documents do not exist`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Put a JSON object to QLDB as a key/value record
* @param key A value of a key attribute to save the record with.
* @param value A value of a value attribute to save the record with. If it's not a string, it will be converted to Amazon Ion before submitting to the ledger.
* @param version number (OPTIONAL) The versions of the document to make sure we update the right version. Function will return an error if the most recent version in the ledger is different.
* @returns A promise with an object containing a document Id and transaction Id
*/
setValue(key, value, version) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.setValue]";
const self = this;
const startTime = new Date().getTime();
try {
if (!key) {
throw new Error(`${fcnName}: Please specify a key`);
}
if (!value) {
throw new Error(`${fcnName}: Please specify a value`);
}
const upsertResult = yield this.setValues([key], [value], [version]);
return upsertResult[0];
}
catch (err) {
const msg = `Could not set the value`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Put a JSON object to QLDB as a key/value record
* @param keys String[] An array of key attributes to save the records with.
* @param values any [] An array of values of a value attributes to save the records with. If they are not a string, they will be converted to Amazon Ion before submitting to the ledger.
* @param versions number [] (OPTIONAL) An array of the versions of the documents to make sure we update the right version. Function will return an error if the most recent version in the ledger is different.
* @returns A promise with an object containing a document Id and transaction Id
*/
setValues(keys, values, versions) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.setValues]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const startTime = new Date().getTime();
try {
if (keys.length < 1) {
throw new Error(`${fcnName}: Please specify at least one key`);
}
if (keys.length > 10) {
throw new Error(`${fcnName}: Unable to submit more than 10 values at a time`);
}
if (values.length < 1) {
throw new Error(`${fcnName}: Please specify at least one value`);
}
if (keys.length !== values.length) {
throw new Error(`${fcnName}: Please make sure the number of keys equals the number of values`);
}
if (versions && (values.length !== versions.length)) {
throw new Error(`${fcnName}: Please make sure the number of versions equals the number of values`);
}
const documentsArray = keys.map((key, index) => {
let document = {};
if (typeof values[index] == "string") {
document[Constants_1.KEY_ATTRIBUTE_NAME] = key;
document[Constants_1.VALUE_ATTRIBUTE_NAME] = values[index];
}
else {
// Making sure we copy the object and will not modify it
document = Object.assign({}, values[index]);
document[Constants_1.KEY_ATTRIBUTE_NAME] = key;
}
logger.debug(`${fcnName} Setting value of ${key} from ledger ${ledgerName} and table ${tableName} as utf8 encoded stringified JSON object.`);
return document;
});
// In case our table has not been created yet, waiting for it to be created
yield this.assureTableExists(tableName);
return yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield Promise.all(documentsArray.map((document, index) => {
const version = versions ? versions[index] : null;
return (0, UpsertDocument_1.upsert)(txn, tableName, Constants_1.KEY_ATTRIBUTE_NAME, document, version).catch((err) => {
throw err;
});
}));
}));
}
catch (err) {
const msg = `Could not set values`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Get most recent metadata for a corresponding key as JSON object
* @param key A value of a key attribute to retrieve the record from.
* @param transactionId A transaction Id for the version of the document you would like to retrieve (optional).
* @returns Promise with a value object as JSON.
*/
getMetadata(key) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.getMetadata]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const paramId = key;
const startTime = new Date().getTime();
try {
if (!paramId) {
throw new Error(`${fcnName}: Please specify a key`);
}
logger.debug(`${fcnName} Getting metadata for ${paramId} from ledger ${ledgerName} and table ${tableName} into a JSON object`);
const result = yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield (0, GetMetadata_1.getDocumentLedgerMetadata)(txn, this.ledgerName, tableName, Constants_1.KEY_ATTRIBUTE_NAME, paramId, qldbClient).catch((err) => {
throw err;
});
}));
if (!result) {
throw `Requested document does not exist`;
}
return result;
}
catch (err) {
const msg = `Could not get metadata`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Get the metadata for a specific documentId and transactionId as JSON object
* @param documentId A document Id generated by the QLDB service.
* @param transactionId A transaction Id for the version of the document you would like to retrieve (optional).
* @returns Promise with a value object as JSON.
*/
getMetadataByDocIdAndTxId(documentId, transactionId) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.getMetadataByDocIdAndTxId]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const startTime = new Date().getTime();
try {
logger.debug(`${fcnName} Getting metadata for document id: ${documentId} and transaction id: ${transactionId} from ledger ${ledgerName} and table ${tableName} into a JSON object`);
const result = yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield (0, GetMetadata_1.getDocumentLedgerMetadataByDocIdAndTxId)(txn, this.ledgerName, tableName, documentId, transactionId, qldbClient).catch((err) => {
throw err;
});
}));
if (!result) {
throw `Requested document does not exist`;
}
return result;
}
catch (err) {
const msg = `Could not get metadata`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Get complete history of a document, associated with the certain key
* @param {string} key A value of a key attribute to retrieve the record from.
* @param {string} fromDateISO OPTIONAL String, containing a from date and time in ISO format like `2019-06-05T00:00:00Z` to query revisions history from.
* @param {string} toDateISO OPTIONAL String, containing a to date and time in ISO format like `2019-06-05T00:00:00Z` to query revisions history to.
* @returns Promise with an array of documents as JSON.
*/
getHistory(key, fromDateISO, toDateISO) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.getHistory]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const paramId = key;
const startTime = new Date().getTime();
try {
if (!paramId) {
throw new Error(`${fcnName}: Please specify a key`);
}
logger.debug(`${fcnName} Getting history for ${paramId} from ledger ${ledgerName} and table ${tableName} into a JSON object`);
const result = yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield (0, GetDocumentHistory_1.getDocumentHistory)(txn, tableName, Constants_1.KEY_ATTRIBUTE_NAME, paramId, fromDateISO, toDateISO).catch((err) => {
throw err;
});
}));
if (!result) {
throw `Requested document does not exist`;
}
return result;
}
catch (err) {
const msg = `Could not get history`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Get value for a corresponding key as JSON object
* @param {LedgerMetadata} ledgerMetadata is an object that holds ledger metadata returned by function "getMetadata(key)"
* @returns Promise with a boolean
*/
verifyLedgerMetadata(ledgerMetadata) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.verifyLedgerMetadata]";
const self = this;
const ledgerName = self.ledgerName;
const tableName = self.tableName;
const startTime = new Date().getTime();
try {
logger.debug(`${fcnName} Verifying metadata for ${ledgerMetadata.DocumentId} from ledger ${ledgerName} and table ${tableName} into a JSON object`);
return yield this.qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () {
return yield (0, VerifyLedgerMetadata_1.verifyDocumentMetadataWithUserData)(this.ledgerName, qldbClient, ledgerMetadata).catch((err) => {
throw err;
});
}));
}
catch (err) {
const msg = `Could not verify the metadta`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Get document revision by metadata
* @param {LedgerMetadata} ledgerMetadata is an object that holds ledger metadata returned by function "getMetadata(key)"
* @returns Document with document revision
*/
getDocumentRevisionByLedgerMetadata(ledgerMetadata) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBKVS.getDocumentRevisionByLedgerMetadata]";
const self = this;
const ledgerName = self.ledgerName;
const startTime = new Date().getTime();
try {
logger.debug(`${fcnName} Retrieving document revision by metadata ${ledgerMetadata.DocumentId} from ledger ${ledgerName}`);
const revisionResponse = yield (0, GetRevision_1.getRevision)(ledgerName, ledgerMetadata.DocumentId, ledgerMetadata.BlockAddress, ledgerMetadata.LedgerDigest.DigestTipAddress, qldbClient);
const ionReader = (0, ion_js_1.makeReader)(revisionResponse.Revision.IonText);
return (0, ion_js_1.load)(ionReader);
//return revisionResponse.Revision.IonText;
}
catch (err) {
const msg = `Could not get document revision`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
});
}
/**
* Verify document revision hash
* @param {JSON} documentRevision is an object that holds document revision returned by function "getDocumentRevisionByLedgerMetadata(ledgerMetadata)" or "getHistory(key)"
* @returns Document with document revision
*/
verifyDocumentRevisionHash(documentRevision) {
const fcnName = "[QLDBKVS.verifyDocumentRevisionHash]";
const startTime = new Date().getTime();
try {
return (0, VerifyRevisionHash_1.validateRevisionHash)(documentRevision);
}
catch (err) {
const msg = `Could not verify document revision hash`;
logger.error(`${fcnName} ${msg}: ${err}`);
throw new Error(msg);
}
finally {
const endTime = new Date().getTime();
logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`);
}
}
/**
* Gets the most recent ledger digest.
* @returns A JSON document with ledger digest.
* @param ledgerName A name of the ledger
* @throws Error: If error happen during the process.
*/
getLedgerDigest(ledgerName, qldbClient) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[QLDBHelper.getLedgerDigest]";
return (0, GetDigest_1.getLedgerDigest)(ledgerName, qldbClient);
});
}
}
exports.QLDBKVS = QLDBKVS;