Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
Handling parsing of DynamoDB query result

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:

enter image description here

Using the AWS SDK, 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:

const params = {
  TableName: 'dynamo-table-name',
  KeyConditionExpression: '#PK = :PK',
  ExpressionAttributeNames: {
    '#PK': 'PK',
  },
  ExpressionAttributeValues: {
    ':PK': 'ORG#MICROSOFT',
  },
};

const result = await client.send(new QueryCommand(params));

Since I am using @aws-sdk/lib-dynamodb 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:

Convert this:

[
  {
    "PK": "ORG#MICROSOFT",
    "SK": "METADATA#MICROSOFT",
    "OrgName": "Microsoft",
    "PlanType": "Enterprise",
  },
  {
    "PK": "ORG#MICROSOFT",
    "SK": "USER#BILLGATES",
    "UserName": "Bill Gates",
    "UserType": "Member"
  },
  {
    "PK": "ORG#MICROSOFT",
    "SK": "USER#SATYANADELLA",
    "UserName": "Satya Nadella",
    "UserType": "Admin"
  }
]

To something like this:

{
    "result": [
        {
            "OrgName": "Microsoft",
            "PlanType": "Enterprise",
            "Users": [
                {
                    "UserName": "Bill Gates",
                    "UserType": "Member"
                },
                {
                    "UserName": "Satya Nadella",
                    "UserType": "Admin"
                }
                
            ]
        }
    ]
}

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here's an idea for how to solve this. Not sure if it substantially improves over what you already have tried. Embed the resulting result array as needed e.g. {result}.

This code uses repeated filters over the same data so, while easy to understand and modify, it's not optimal for a large dataset.

const records = [
  {
    PK: "ORG#MICROSOFT",
    SK: "METADATA#MICROSOFT",
    OrgName: "Microsoft",
    PlanType: "Enterprise",
  },
  {
    PK: "ORG#MICROSOFT",
    SK: "USER#BILLGATES",
    UserName: "Bill Gates",
    UserType: "Member",
  },
  {
    PK: "ORG#MICROSOFT",
    SK: "USER#SATYANADELLA",
    UserName: "Satya Nadella",
    UserType: "Admin",
  },
  {
    PK: "ORG#APPLE",
    SK: "METADATA#APPLE",
    OrgName: "Apple",
    PlanType: "Enterprise",
  },
  {
    PK: "ORG#APPLE",
    SK: "USER#TIMCOOK",
    UserName: "Tim Cook",
    UserType: "Admin",
  },
];

const orgs = records.filter((rec) => rec.OrgName);
const users = records.filter((rec) => rec.UserName);

const result = orgs.map((org) => ({
  OrgName: org.OrgName,
  PlanType: org.PlanType,
  Users: users
    .filter((user) => user.PK === org.PK)
    .map(({ UserName, UserType }) => ({ UserName, UserType })),
}));
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda