Please go to our Blogger site at blog.backspace.academy This site will no longer be updated.
Add Records to the CognitoSync Dataset
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();
cognitosync.listRecords({
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);
addRecord();
}
});
}
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 */
Value: FACEBOOK_USER.id
}
]
};
console.log("UserID: ".cyan + FACEBOOK_USER.id);
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 /auth/facebook/callback?code=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 302 348ms - 72b
FACEBOOK_TOKEN:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
GET /success 304 17ms
Cognito Identity Id: us-east-1:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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"}
SyncSessionToken: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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 this tutorial? Please click the share buttons to tell others.