This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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