4

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I appreciate your knowledge about the HASH and RANGE key. But there's a reason why I needed it as a type of [["key", "value"], ["key", "value"]]. Can you update your answer to allow for that use case? Each nested array will always have a size of two and both are strings. Empty string is okay
This is not supported AFAIK. Maybe you can implement a workaround by using a string array that you then split manually, e.g. something like "additionalInfomation": {SS: ["key#value", "key#value"] }
it's okay I solved it. additionalInformation: { L: [ {SS: ["", ""] }, {SS: ["", ""] } ] }
I stand corrected, I had overlooked the L (List) and the M (Map) Attribute Values (see also the DynamoDB Data Types)
Thanks. That helped a lot! I wished the aws documentation had more examples 😅

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.