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.
101 lines (98 loc) • 3.68 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBlockAddressValue = exports.getStrandId = exports.getSequenceNo = exports.getMetadataId = exports.blockAddressToValueHolder = exports.BlockAddress = void 0;
const ion_js_1 = require("ion-js");
const Logging_1 = require("./Logging");
const logger = Logging_1.log.getLogger("qldb-helper");
class BlockAddress {
constructor(strandId, sequenceNo) {
this._strandId = strandId;
this._sequenceNo = sequenceNo;
}
}
exports.BlockAddress = BlockAddress;
/**
* Convert a block address from an Ion value into a ValueHolder.
* Shape of the ValueHolder must be: {'IonText': "{strandId: <"strandId">, sequenceNo: <sequenceNo>}"}
* @param value The Ion value that contains the block address values to convert.
* @returns The ValueHolder that contains the strandId and sequenceNo.
*/
function blockAddressToValueHolder(value) {
const fcnName = "[BlockAddress blockAddressToValueHolder]";
try {
const blockAddressValue = getBlockAddressValue(value);
const strandId = getStrandId(blockAddressValue);
const sequenceNo = getSequenceNo(blockAddressValue);
const valueHolder = `{strandId: "${strandId}", sequenceNo: ${sequenceNo}}`;
return { IonText: valueHolder };
}
catch (err) {
logger.debug(`${fcnName} ${err} `);
throw `${fcnName} ${err} `;
}
}
exports.blockAddressToValueHolder = blockAddressToValueHolder;
/**
* Helper method that to get the Metadata ID.
* @param value The Ion value.
* @returns The Metadata ID.
*/
function getMetadataId(value) {
const metaDataId = value.get("id");
if (metaDataId === null) {
throw new Error(`Expected field name id, but not found.`);
}
return metaDataId.stringValue();
}
exports.getMetadataId = getMetadataId;
/**
* Helper method to get the Sequence No.
* @param value The Ion value.
* @returns The Sequence No.
*/
function getSequenceNo(value) {
const sequenceNo = value.get("sequenceNo");
if (sequenceNo === null) {
throw new Error(`Expected field name sequenceNo, but not found.`);
}
return sequenceNo.numberValue();
}
exports.getSequenceNo = getSequenceNo;
/**
* Helper method to get the Strand ID.
* @param value The Ion value.
* @returns The Strand ID.
*/
function getStrandId(value) {
const strandId = value.get("strandId");
if (strandId === null) {
throw new Error(`Expected field name strandId, but not found.`);
}
return strandId.stringValue();
}
exports.getStrandId = getStrandId;
function getBlockAddressValue(value) {
const type = value.getType();
if (type !== ion_js_1.IonTypes.STRUCT) {
throw new Error(`Unexpected format: expected struct, but got IonType: ${type.name}`);
}
const blockAddress = value.get("blockAddress");
if (blockAddress == null) {
throw new Error(`Expected field name blockAddress, but not found.`);
}
return blockAddress;
}
exports.getBlockAddressValue = getBlockAddressValue;