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.
107 lines (104 loc) • 5.38 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.getRevisionMetadataByDocIdAndTxId = exports.getRevisionByDocIdAndTxId = exports.getRevision = void 0;
const Util_1 = require("./Util");
const Logging_1 = require("./Logging");
const logger = Logging_1.log.getLogger("qldb-helper");
/**
* Get the revision data object for a specified document ID and block address.
* Also returns a proof of the specified revision for verification.
* @param ledgerName Name of the ledger containing the document to query.
* @param documentId Unique ID for the document to be verified, contained in the committed view of the document.
* @param blockAddress The location of the block to request.
* @param digestTipAddress The latest block location covered by the digest.
* @param qldbClient The QLDB control plane client to use.
* @returns Promise which fulfills with a GetRevisionResponse.
*/
function getRevision(ledgerName, documentId, blockAddress, digestTipAddress, qldbClient) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[GetRevision getRevision]";
try {
const request = {
Name: ledgerName,
BlockAddress: blockAddress,
DocumentId: documentId,
DigestTipAddress: digestTipAddress
};
return yield qldbClient.getRevision(request);
}
catch (err) {
throw `${fcnName} ${err} `;
}
});
}
exports.getRevision = getRevision;
/**
* Get full revision data object including the metadata for a specified document ID and block address.
* @param txn An instance of transaction executor object.
* @param tableName The name of the table containing the document.
* @param documentId The Id of the document generated by QLDB during creation.
* @param transactionId An ID of a transaction, that created or updated the document and triggered creation of new document revision.
* @returns Promise which fulfills with Ion Value.
*/
function getRevisionByDocIdAndTxId(txn, tableName, documentId, transactionId) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[GetRevision getRevisionByDocIdAndTxId]";
(0, Util_1.validateTableNameConstrains)(tableName);
const statement = `SELECT * FROM history(${tableName}) AS h WHERE h.metadata.id = ? AND h.metadata.txId = ?`;
try {
logger.debug(`${fcnName} Executing statement ${statement}`);
const result = yield txn.execute(statement, documentId, transactionId);
return result.getResultList()[0];
}
catch (err) {
throw `${fcnName} ${err}`;
}
});
}
exports.getRevisionByDocIdAndTxId = getRevisionByDocIdAndTxId;
/**
* Get ledger metadata object for a specified document ID and block address.
* @param txn An instance of transaction executor object.
* @param tableName The name of the table containing the document.
* @param documentId The Id of the document generated by QLDB during creation.
* @param transactionId An ID of a transaction, that created or updated the document and triggered creation of new document revision.
* @returns Promise which fulfills with Ion Value.
*/
function getRevisionMetadataByDocIdAndTxId(txn, tableName, documentId, transactionId) {
return __awaiter(this, void 0, void 0, function* () {
const fcnName = "[GetRevision getRevisionMetadataByDocIdAndTxId]";
(0, Util_1.validateTableNameConstrains)(tableName);
const statement = `SELECT blockAddress, hash, metadata FROM history(${tableName}) AS h WHERE h.metadata.id = ? AND h.metadata.txId = ?`;
try {
logger.debug(`${fcnName} Executing statement ${statement}`);
const result = yield txn.execute(statement, documentId, transactionId);
return result.getResultList()[0];
}
catch (err) {
throw `${fcnName} ${err}`;
}
});
}
exports.getRevisionMetadataByDocIdAndTxId = getRevisionMetadataByDocIdAndTxId;