async getValues()

in src/QLDBKVS.ts [300:371]


    async getValues(keys: string[], withVersionNumbers?: boolean): Promise<object[] | Buffer[]> {
        const fcnName = "[QLDBKVS.getValues]";
        const self: QLDBKVS = this;
        const ledgerName: string = self.ledgerName;
        const tableName: string = self.tableName;
        const paramIds: string[] = keys;
        const startTime: number = 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: GetDocumentResult[] = await this.qldbDriver.executeLambda(async (txn: TransactionExecutor) => {
                return await getByKeyAttributes(txn, tableName, 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`;
            }

            let 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(VALUE_ATTRIBUTE_NAME)) {
                    returnValue = resultData.get(VALUE_ATTRIBUTE_NAME).stringValue();
                } else {
                    let returnValueTMP: { [k: string]: any }
                    try {
                        returnValueTMP = JSON.parse(JSON.stringify(resultData));
                        delete returnValueTMP[KEY_ATTRIBUTE_NAME];
                    } catch (err) {
                        throw `Unable to convert to JSON an Ion object with key ${resultData.get(KEY_ATTRIBUTE_NAME).stringValue()}: ${err}`;
                    }
                    returnValue = JSON.parse(JSON.stringify(returnValueTMP));

                }

                if (withVersionNumbers) {
                    returnObjects[index] = {
                        data: returnValue,
                        version: valueVersion
                    }
                } else {
                    returnObjects[index] = returnValue
                }
                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: number = new Date().getTime();
            logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`)
        }

    }