Skip to content

Instantly share code, notes, and snippets.

@rish-16
Created December 7, 2018 11:16
Show Gist options
  • Save rish-16/24b90f1ca21aca1f5bef897b645b7294 to your computer and use it in GitHub Desktop.
Save rish-16/24b90f1ca21aca1f5bef897b645b7294 to your computer and use it in GitHub Desktop.
'use strict';
// Requiring the Dynamoose NPM package
var dynamoose = require('dynamoose');
// Setting our table name prefix to "example-"
dynamoose.setDefaults({
prefix: 'example-',
suffix: ''
});
// Creating a new Dynamomoose model, with 3 attributes (id, name, and ttl), the name of our table is "example-Cat" (due to our prefix default set above, and our suffix being an empty string)
var Cat = dynamoose.model('Cat', {
id: Number,
name: String
}, {
expires: {
// ttl (time to live) will be set to 1 day (86,400 seconds), this value must always be in seconds
ttl: 1 * 24 * 60 * 60,
// This is the name of our attribute to be stored in DynamoDB
attribute: 'ttl'
}
});
// Creating a new instance of our "Cat" model
var garfield = new Cat({id: 1, name: 'Fluffy'});
// Saving our new cat to DynamoDB
garfield.save()
.then(function () {
// Getting our cat from DynamoDB after it has completed saving
return Cat.get(1);
})
.then(function (fluffy) {
// After getting our cat from DynamoDB we print the object that we received from DynamoDB
console.log(JSON.stringify(fluffy, null, ' '));
/*
{
"id": 3,
"name": "Fluffy",
"ttl": "2017-05-28T01:35:01.000Z"
}
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment