Skip to content

Instantly share code, notes, and snippets.

@merictaze
Created July 3, 2018 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merictaze/330cfd207751f558231a3003eb0d9510 to your computer and use it in GitHub Desktop.
Save merictaze/330cfd207751f558231a3003eb0d9510 to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
const bluebird = require('bluebird');
AWS.config.setPromisesDependency(bluebird);
// use local dynamodb if running offline
const dbClient = new AWS.DynamoDB.DocumentClient(
process.env.IS_OFFLINE ? {region: 'localhost', endpoint: 'http://localhost:8000'} : {}
);
module.exports.usersGet = async (event, context, callback) => {
const params = {
TableName: process.env.DYNAMODB_USER_TABLE
};
let result;
try {
result = await dbClient.scan(params).promise();
} catch(error) {
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Failed to get users'
});
return;
}
callback(null, {
statusCode: 200,
body: JSON.stringify(result.Items)
});
};
module.exports.usersCreate = async (event, context, callback) => {
const params = {
TableName: process.env.DYNAMODB_USER_TABLE,
Item: {
email: `${Math.random().toString(36).substring(7)}@test.com`
}
};
let result;
try {
result = await dbClient.put(params).promise();
} catch (error) {
callback(null, {
statusCode: error.statusCode || 501,
headers: {'Content-Type': 'text/plain'},
body: 'Failed to create user'
});
return;
}
callback(null, {
statusCode: 200,
body: JSON.stringify(params.Item)
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment