in src/QLDBKVS.ts [59:119]
constructor(ledgerName: string, tableName: string, checkForTable?: boolean) {
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 = createQldbDriver(ledgerName);
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
(async () => {
//// Listing tables names
logger.info(`${fcnName} Listing table names...`);
let tableNames: string[] = await 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.`)
await createTableWithIndex(this.qldbDriver, tableName, 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;
}