skip to Main Content

I am using following code to fetch data from the DynamoDB.

async fetchData(params: QueryParams) {
    return await this.docClient.send(new QueryCommand(params));
  }



const dbObject: QueryParams = {
        TableName: process.env.TABLE_NAME,
        KeyConditionExpression: "PK = :id",
        FilterExpression: "ACTIVE = :active",
        ExpressionAttributeValues: {
          ":id": { S: id },
          ":active": { S: "Y" },
        },
        ProjectionExpression:
          "FUND_MANAGER, FUND_NAME,PRICE_DECIMAL ,ANNUALIZED_YIELD,YTD_GROWTH, AUM_VALUE",
      };

I am currently getting results as follows. (Example, not actual response)

{
"Id": {
"S": "456"
},
"Price": {
"N": "650"
},
"ProductCategory": {
"S": "Sporting Goods"
} }

How can I get responses directly as Strings or Numbers, Like this directly

{ Id : "456", Price : 650 }

2

Answers


  1. You could try transforming the data with something like this:

    async function fetchData(params: QueryParams) {
      const response = await this.docClient.send(new QueryCommand(params));
      return transformResponse(response.Items);
    }
    
    function transformResponse(items: any[]): any[] {
      return items.map((item) => {
        const transformedItem: any = {};
        for (const [key, value] of Object.entries(item)) {
          transformedItem[key] = transformAttributeValue(value);
        }
        return transformedItem;
      });
    }
    
    function transformAttributeValue(value: any): string | number {
      const attributeType = Object.keys(value)[0];
      const attributeValue = value[attributeType];
    
      switch (attributeType) {
        case 'S': // String
          return attributeValue;
        case 'N': // Number
          return Number(attributeValue);
        default:
          return attributeValue;
      }
    }
    

    Do note, I am assuming that the DynamoDB response follows the structure you provided, where each attribute has a single key-value pair representing its type and value. If your actual response structure is different, you’ll need to change the code but this should give you an idea of how you might approach it.

    Login or Signup to reply.
  2. Assuming you’ve set your client to match you naming convention, you’re using the document client which provides native JSON abstraction by default, they query your shared wouldn’t actually work. Try this, and you will obtain the data as you desire:

    async fetchData(params: QueryParams) {
        return await this.docClient.send(new QueryCommand(params));
      }
    
    
    
    const dbObject: QueryParams = {
            TableName: process.env.TABLE_NAME,
            KeyConditionExpression: "PK = :id",
            FilterExpression: "ACTIVE = :active",
            ExpressionAttributeValues: {
              ":id": id,
              ":active": "Y",
            },
            ProjectionExpression:
              "FUND_MANAGER, FUND_NAME,PRICE_DECIMAL ,ANNUALIZED_YIELD,YTD_GROWTH, AUM_VALUE",
          };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search