async setValues()

in src/QLDBKVS.ts [414:477]


    async setValues(keys: string[], values: any[], versions?: number[]): Promise<UpsertResult[]> {
        const fcnName = "[QLDBKVS.setValues]";
        const self: QLDBKVS = this;
        const ledgerName: string = self.ledgerName;
        const tableName: string = self.tableName;
        const startTime: number = new Date().getTime();

        try {
            if (keys.length < 1) {
                throw new Error(`${fcnName}: Please specify at least one key`);
            }
            if (keys.length > 10) {
                throw new Error(`${fcnName}: Unable to submit more than 10 values at a time`);
            }
            if (values.length < 1) {
                throw new Error(`${fcnName}: Please specify at least one value`);
            }
            if (keys.length !== values.length) {
                throw new Error(`${fcnName}: Please make sure the number of keys equals the number of values`);
            }
            if (versions && (values.length !== versions.length)) {
                throw new Error(`${fcnName}: Please make sure the number of versions equals the number of values`);
            }

            const documentsArray: { [k: string]: any }[] = keys.map((key, index) => {

                let document: { [k: string]: any } = {};


                if (typeof values[index] == "string") {
                    document[KEY_ATTRIBUTE_NAME] = key
                    document[VALUE_ATTRIBUTE_NAME] = values[index];
                } else {
                    // Making sure we copy the object and will not modify it
                    document = Object.assign({}, values[index]);
                    document[KEY_ATTRIBUTE_NAME] = key
                }

                logger.debug(`${fcnName} Setting value of ${key} from ledger ${ledgerName} and table ${tableName} as utf8 encoded stringified JSON object.`);
                return document
            })

            // In case our table has not been created yet, waiting for it to be created
            await this.assureTableExists(tableName);

            return await this.qldbDriver.executeLambda(async (txn: TransactionExecutor) => {
                return await Promise.all(documentsArray.map((document, index) => {
                    const version = versions ? versions[index] : null;
                    return upsert(txn, tableName, KEY_ATTRIBUTE_NAME, document, version).catch((err) => {
                        throw err
                    });
                }));
            })

        } catch (err) {
            const msg = `Could not set values`;
            logger.error(`${fcnName} ${msg}: ${err}`);
            throw new Error(msg);
        } finally {
            const endTime: number = new Date().getTime();
            logger.debug(`${fcnName} Execution time: ${endTime - startTime}ms`)
        }

    }