Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB Array of Array of strings schema

I'm looking for assistance on how I can achieve the schema below into my dynamodb database.

Example of my JSON

var users = [{
    userId: 123,
    userName: "John Smith",
    additionalInfomation: [
      ["favoriteColor", "blue"], 
      ["hobbies", "ping-pong"]
    ]
}]

This is what I have so far to achieve the userId and userName schema. I'm having trouble setting up additionalInfomation part.

  const params = {
    AttributeDefinitions: [
      {
        AttributeName: 'userId',
        AttributeType: 'N'
      },
      {
        AttributeName: 'userName',
        AttributeType: 'S'
      },
      {
        AttributeName: 'additionalInformation',
        AttributeType: <NEED HELP HERE> // <-------------------- ?????
      }
    ],
    KeySchema: [
      {
        AttributeName: 'userId',
        KeyType: 'HASH'
      },
      {
        AttributeName: 'userName',
        KeyType: 'RANGE'
      },
      {
        AttributeName: 'additionalInformation',
        KeyType: <NEED HELP HERE> // <-------------------- ?????
      }
    ],
    ProvisionedThroughput: {
      ReadCapacityUnits: 1,
      WriteCapacityUnits: 1
    },
    TableName: 'USERS',
    StreamSpecification: {
      StreamEnabled: false
    }
  };
  // Call DynamoDB to create the table
  ddb.createTable(params, function(err, data) {
    if (err) {
      console.log('Error', err);
    } else {
      console.log('Table Created', data);
    }
  });

Need help setting the additionalInformation schema up. Please excuse my ignorance if this isn't the right approach. Still learning dynamoDB and the aws doc isn't quite helpful for a beginner.

like image 654
Victor Le Avatar asked Aug 31 '25 10:08

Victor Le


1 Answers

For this use case, I recommend that you choose either userId or userName as your HASH key (aka partition key) for your table, assuming that either of this attributes will uniquely identify the user. RANGE key (aka sort key) are beneficial when you have several items associated with one partition key, c.f. artists and song titles in the Primary Key paragraph of the DynamoDB user guide. Consequently, this means that you only need to specify one attribute in the AttributeDefinitions and KeySchema.

Furthermore, I recommend that you omit the additionalInformation from your schema based on the assumption that you will neither use that information as partition key nor a sort key.

Instead, you can add them as two separate attributes to individual items when calling putItem() or updateItem (or the corresponding put() and update() functions if you use the DynamoDB DocumentClient).

const params = {
  Item: {
   "userId": {
     N: "123"
    }, 
   "userName": {
     S: "dummy"
    }, 
   "favoriteColor": {
     S: "blue"
    },
   "hobbies": {
     SS: ["ping-pong", "another hobby"]
    }
  }, 
  TableName: "USERS"
 };
 const putItemPromise = dynamodb.putItem(params).promise()

Note, in the example above I have specified favoriteColor as S, i.e. a single string, but the hobbies as SS meaning that it is a array of strings. This may or may not what you want, but since the attribute name is "hobbies" (and not "hobby"), I figured that it would make sense to allow more than one.

like image 193
matsev Avatar answered Sep 02 '25 23:09

matsev