public async query()

in packages/aws-rfdk/lib/lambdas/nodejs/lib/dynamodb/composite-table.ts [346:388]


  public async query(
    primaryKeyValue: string,
    pageLimit?: number,
  ): Promise<{ [key: string]: { [key: string]: any }}> {
    if (!this.tableName) {
      throw Error('Attempt to Query a deleted table');
    }
    // See: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#query-property
    const request: QueryCommandInput = {
      TableName: this.tableName,
      Select: 'ALL_ATTRIBUTES',
      ConsistentRead: true,
      ReturnConsumedCapacity: 'NONE',
      ExpressionAttributeNames: {
        '#PK': this.primaryKey,
      },
      ExpressionAttributeValues: {
        ':PKV': convertToAttr(primaryKeyValue),
      },
      KeyConditionExpression: '#PK = :PKV',
      Limit: pageLimit,
    };
    console.debug(`DynamoDB.Query: ${JSON.stringify(request)}`);
    const items: { [key: string]: { [key: string]: any }} = {};
    try {
      do {
        const response: QueryCommandOutput = await this.client.send(new QueryCommand(request));
        request.ExclusiveStartKey = response.LastEvaluatedKey;
        if (response.Items) {
          for (const item of response.Items) {
            const unmarshalled = unmarshall(item);
            const sortValue: string = unmarshalled[this.sortKey];
            delete unmarshalled[this.primaryKey];
            delete unmarshalled[this.sortKey];
            items[sortValue] = unmarshalled;
          }
        }
      } while (request.ExclusiveStartKey);
      return items;
    } catch (e) {
      throw new Error(`Query '${primaryKeyValue}':" ${(e as Error)?.name} -- ${(e as Error)?.message}`);
    }
  }