title: Handling parsing of DynamoDB query result
body: <p>Suppose that I am working on an application implementing single-table design in DynamoDB. The table holds organisations, and users resulting in something like this:</p>
  <p><a href="https://i.stack.imgur.com/0vmNn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0vmNn.png" alt="enter image description here" /></a></p>
  <p>Using the <a href="https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html" rel="nofollow noreferrer">AWS SDK</a>, I am able to issue a QueryCommand against my table and retrieve all information and related records to my organisation (the red box in the image above) by running something like this:</p>
  <pre><code>const params = {
    TableName: 'dynamo-table-name',
    KeyConditionExpression: '#PK = :PK',
    ExpressionAttributeNames: {
      '#PK': 'PK',
    },
    ExpressionAttributeValues: {
      ':PK': 'ORG#MICROSOFT',
    },
  };
  
  const result = await client.send(new QueryCommand(params));
  
  </code></pre>
  <p>Since I am using <code>@aws-sdk/lib-dynamodb</code> the result comes back as an array of JS objects which is good, but I am running into the following problem. Once I have that data, I would like to do the following:</p>
  <p>Convert this:</p>
  <pre><code>[
    {
      &quot;PK&quot;: &quot;ORG#MICROSOFT&quot;,
      &quot;SK&quot;: &quot;METADATA#MICROSOFT&quot;,
      &quot;OrgName&quot;: &quot;Microsoft&quot;,
      &quot;PlanType&quot;: &quot;Enterprise&quot;,
    },
    {
      &quot;PK&quot;: &quot;ORG#MICROSOFT&quot;,
      &quot;SK&quot;: &quot;USER#BILLGATES&quot;,
      &quot;UserName&quot;: &quot;Bill Gates&quot;,
      &quot;UserType&quot;: &quot;Member&quot;
    },
    {
      &quot;PK&quot;: &quot;ORG#MICROSOFT&quot;,
      &quot;SK&quot;: &quot;USER#SATYANADELLA&quot;,
      &quot;UserName&quot;: &quot;Satya Nadella&quot;,
      &quot;UserType&quot;: &quot;Admin&quot;
    }
  ]
  </code></pre>
  <p>To something like this:</p>
  <pre><code>{
      &quot;result&quot;: [
          {
              &quot;OrgName&quot;: &quot;Microsoft&quot;,
              &quot;PlanType&quot;: &quot;Enterprise&quot;,
              &quot;Users&quot;: [
                  {
                      &quot;UserName&quot;: &quot;Bill Gates&quot;,
                      &quot;UserType&quot;: &quot;Member&quot;
                  },
                  {
                      &quot;UserName&quot;: &quot;Satya Nadella&quot;,
                      &quot;UserType&quot;: &quot;Admin&quot;
                  }
                  
              ]
          }
      ]
  }
  </code></pre>
  <p>I have struggled with finding an elegant solution. My attempts up to this point made use of JavaScript's reduce function but they never end up feeling robust enough to be something I would really consider using. Most of my problems come from the fact that I would like it to also work if I was to not only have Users for an Organisation, but also something else like maybe Locations. I am also looking for something that, in the case where I search for all organizations, it could get them in the desired format. Does anyone have any suggestions on how I could do this?</p>