Skip to content

Instantly share code, notes, and snippets.

@rish-16
Created December 7, 2018 11:07
Show Gist options
  • Save rish-16/f6e3b426327c67e747b7ee0bc15a1679 to your computer and use it in GitHub Desktop.
Save rish-16/f6e3b426327c67e747b7ee0bc15a1679 to your computer and use it in GitHub Desktop.
'use strict';
// Requiring the Dynamoose NPM package
var dynamoose = require('dynamoose');
// To configure Dynamose you can either:
/*
Set environment variables
export AWS_ACCESS_KEY_ID="Your AWS Access Key ID"
export AWS_SECRET_ACCESS_KEY="Your AWS Secret Access Key"
export AWS_REGION="us-east-1"
*/
// OR configure the AWS object
/*
dynamoose.AWS.config.update({
accessKeyId: 'AKID',
secretAccessKey: 'SECRET',
region: 'us-east-1'
});
*/
// OR use an AWS IAM role assigned to an AWS resource
// To use a local DynamoDB setup you can use the following line
// dynamoose.local(); // This will set the server to "http://localhost:8000" (default)
// dynamoose.local("http://localhost:1234") // This will set the server to "http://localhost:1234"
// This will create a Dynamoose model "Cat" (which is basically like a DynamoDB table), it will allow for 2 properties in the schema, `id` (number) and `name` (string)
var Cat = dynamoose.model('Cat', { id: Number, name: String });
// This will create a new instance of our "Cat" model, with the `id` as 666, and `name` as 'Garfield'
var garfield = new Cat({id: 666, name: 'Garfield'});
// This will save our new object to DynamoDB (remember this happens asynchronously, so you need to be sure to wait before trying to access the object)
garfield.save();
// This will preform an DynamoDB get on the "Cat" model/table get the object with the `id` = 666 and return a promise with the returned object.
Cat.get(666)
.then(function (badCat) {
console.log('Never trust a smiling cat. - ' + badCat.name);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment