Victor Le
Victor Le

Reputation: 1788

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.

Upvotes: 4

Views: 6714

Answers (1)

matsev
matsev

Reputation: 33759

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.

Upvotes: 4

Related Questions