Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.