Using AWS Cognito with Node.JS - Part 3

Add Records to the CognitoSync Dataset

back to Part 2

The complete code for the tutorial is at GitHub.

Now that we have our CognitoSync session token we can use this to add, modify or delete CognitoSync dataset records.
To demonstrate we are going to call addRecord to add a record. Open index.js in your editor and add a call to the addRecord in the callback of getCognitoSynToken().
 function getCognitoSynToken(){
  /* Other AWS SDKs will automatically use the Cognito Credentials provider */
  /* configured in the JavaScript SDK. */
  cognitosync = new AWS.CognitoSync();
    DatasetName: COGNITO_DATASET_NAME, /* required */
    IdentityId: COGNITO_IDENTITY_ID,  /* required */
    IdentityPoolId: COGNITO_IDENTITY_POOL_ID  /* required */
  }, function(err, data) {
    if (err) console.log("listRecords: ".red + err, err.stack); /* an error occurred */
      else {
        console.log("listRecords: ".green + JSON.stringify(data));
        COGNITO_SYNC_TOKEN = data.SyncSessionToken;
        COGNITO_SYNC_COUNT = data.DatasetSyncCount;
        console.log("SyncSessionToken: ".green + COGNITO_SYNC_TOKEN);           /* successful response */
        console.log("DatasetSyncCount: ".green + COGNITO_SYNC_COUNT);
Now lets add a record user_id that stores the users facebook id. We need to not only pass the CognitoSync session token but also the syncount that we got from the call to listRecords.
 function addRecord(){
  var params = {
    DatasetName: COGNITO_DATASET_NAME, /* required */
    IdentityId: COGNITO_IDENTITY_ID, /* required */
    IdentityPoolId: COGNITO_IDENTITY_POOL_ID, /* required */
    SyncSessionToken: COGNITO_SYNC_TOKEN, /* required */
    RecordPatches: [
        Key: 'USER_ID', /* required */
        Op: 'replace', /* required */
        SyncCount: COGNITO_SYNC_COUNT, /* required */
  console.log("UserID: ".cyan +;
  cognitosync.updateRecords(params, function(err, data) {
    if (err) console.log("updateRecords: ".red + err, err.stack); /* an error occurred */
    else     console.log("Value: ".green + JSON.stringify(data));           /* successful response */
Now run the app with npm start again and you should get something like the following from the console after you have logged in from the browser.
GET / 304 318ms
GET / 200 17ms - 338b
GET /stylesheets/style.css 304 2ms
GET /auth/facebook 302 4ms - 388b
GET /success 304 17ms
listRecords: {"Count":1,"DatasetDeletedAfterRequestedSyncCount":false,"DatasetExists":true,"DatasetSyncCount":1,"LastModifiedBy":"XXXXXXXXXXXX","Records":[{"DeviceLastModifiedDate":"2014-08-15T15:33:58.627Z","Key":"USER_ID","LastModifiedBy":"XXXXXXXXXX","LastModifiedDate":"2014-08-15T15:33:58.627Z","SyncCount":1,"Value":"XXXXXXXXXXXX"}],"SyncSessionToken":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}
DatasetSyncCount: 1
UserID: 524972310968618
Value: {"Records":[{"Key":"USER_ID","SyncCount":2,"Value":"XXXXXXXXXXXXXXX"}]}

Finished! We have logged in with Facebook, created a dataset and added a record to it. Stay tuned for more AWS development tutorials.
Like coding? AWS Certified Developers are in HOT demand! Check out BackSpace Academy for more details.
Paul Coady CEO BackSpace Technology LLC

Providing the best value AWS certification courses and exam engines.

No comments:

Post a Comment