简体   繁体   中英

Adding JSON array to DynamoDB Item

I understand that I can create "Lists" only from primitive data types, so look at my (Node.js using AWS Document Client) code as pseudo code. My objective is to attach a JSON array to an item so that I can later retrieve/update/delete the device (and corresponding data) from the customer's record. I understand I may be able to use Maps to do this, but I'm a beginner and the documentation regarding how to do that using document client is unclear to me.

This is what I am trying to do:

var deviceData = {
    'deviceID': deviceID,
    'attributes': [
        {'firmwareVersion': firmwareVersion},
        {'productID': productID},
        {'knickName': 'New Device'},
        {'dateAdded': (new Date()).getTime()}
    ]
};

var newCustomerData = {
    TableName: process.env.customerMasterFile,
    Key: {
        'email': email
    },
    ReturnValues: 'UPDATED_NEW',
    UpdateExpression: 'ADD #device :device SET #customerEmailDomain = :customerEmailDomain, #friendlyName = :friendlyName, #created = :created, #updated = :updated',
    ExpressionAttributeNames: {
        '#device': 'deviceList',
        '#customerEmailDomain': 'customerEmaiDomain',
        '#friendlyName': 'friendlyName',
        '#created': 'createAccountTime',
        '#updated': 'updateAccountTime',
    },
    ExpressionAttributeValues: {
        ':device': docClient.createSet([deviceData]),  // I know this is incorrect...
        ':customerEmailDomain': customerEmailDomain,
        ':friendlyName': friendlyName,
        ':created': (new Date()).getTime(),
        ':updated': (new Date()).getTime()
    }
};

docClient.update(newCustomerData, function(err, data) {
    if (err) console.log(err);
    else console.log(data);
});

Normally, JSON data will be persisted as Map on DynamoDB. If you store JSON array on DynamoDB, it will be stored as "List of Map" data type on DynamoDB which will make it difficult to update, delete, retrieve without knowing the index of the List data type (ie device). It is not recommended to use "List of Map" if you need to accomplish update/delete without knowing the index of list (ie index of an array).

1) Changed to SET for all attributes including device

To store single JSON object as Map which will allow to update/delete without knowing the index of an array:-

var params = {
    TableName: process.env.customerMasterFile,
    Key: {
        'email': email
    },
    ReturnValues: 'UPDATED_NEW',
    UpdateExpression: 'SET #device = :device, #customerEmailDomain = :customerEmailDomain ,#friendlyName = :friendlyName, #created = :created, #updated = :updated',
    ExpressionAttributeNames: {
        '#device': 'deviceList',
        '#customerEmailDomain': 'customerEmaiDomain',
        '#friendlyName': 'friendlyName',
        '#created': 'createAccountTime',
        '#updated': 'updateAccountTime',
    },
    ExpressionAttributeValues: {
        ':device': deviceData,
        ':customerEmailDomain': customerEmailDomain,
        ':friendlyName': friendlyName,
        ':created': (new Date()).getTime(),
        ':updated': (new Date()).getTime()
    }
};

Sample device as Map:-

在此处输入图片说明

Alternate Approach:-

  • Add device id as sort key of the table
  • The attributes email and device id forms the unique combination for an item on DynamoDB
  • You can accomplish the update/delete easily with this data model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM