UNPKG

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.

93 lines (90 loc) 4.56 kB
"use strict"; /* * 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.createTableWithIndex = exports.listTables = void 0; const { validateTableNameConstrains, validateAttributeNameConstrains } = require('./Util'); const Logging_1 = require("./Logging"); const logger = Logging_1.log.getLogger("qldb-helper"); /** * Return an array of table names in UPPER CASE * @returns Returns an array of table names in UPPER CASE. * @throws Error: If error happen during the process. */ function listTables(qldbDriver) { return __awaiter(this, void 0, void 0, function* () { const fcnName = "[QLDBHelper.listTables]"; try { logger.debug(`${fcnName} Listing table names...`); let tableNames = yield qldbDriver.getTableNames(); tableNames = tableNames.map(x => { return x.toUpperCase(); }); return tableNames; } catch (err) { throw new Error(`${fcnName} ${err}`); } }); } exports.listTables = listTables; /** * Create table with index in a single transaction. * @param tableName Name of the table to create. * @param keyAttributeName A name of a key attribute belonging to a document. * @returns Promise which fulfills with the number of changes to the database. Returns 0 if table already exists. */ function createTableWithIndex(qldbDriver, tableName, keyAttributeName) { return __awaiter(this, void 0, void 0, function* () { const fcnName = "[QLDBHelper.createTableWithIndex]"; try { //// Listing tables names const tableNames = yield listTables(qldbDriver); //// Checking if table is already created and create if not logger.debug(`${fcnName} Checking if table with name ${tableName} exists`); if (tableNames.indexOf(tableName.toUpperCase()) < 0) { // Creating table return qldbDriver.executeLambda((txn) => __awaiter(this, void 0, void 0, function* () { let resultsTotal = 0; validateTableNameConstrains(tableName); const statement = `CREATE TABLE ${tableName}`; const resultCreateTable = yield txn.execute(statement); logger.info(`${fcnName} Successfully created table ${tableName}. Creating index.`); resultsTotal += resultCreateTable.getResultList().length; validateAttributeNameConstrains(keyAttributeName); const createIndexStatement = `CREATE INDEX on ${tableName} (${keyAttributeName})`; const resultCreateIndex = yield txn.execute(createIndexStatement); logger.info(`${fcnName} Successfully created index ${keyAttributeName} on table ${tableName}.`); resultsTotal += resultCreateIndex.getResultList().length; return resultsTotal; })); } else { logger.debug(`${fcnName} Table with name "${tableName}" already exists`); return 0; } } catch (err) { throw new Error(`${fcnName} ${err}`); } }); } exports.createTableWithIndex = createTableWithIndex;