AWS DynamoDB noSQL JavaScript Example for Blog Post

AWS DynamoDB is a noSQL database provided by Amazon Web Services. It can be accessed using JavaScript and node.Js. Here is an example of a simple blog table: More information can be found at https://aws.amazon.com/dynamodb/ module.exports.DynamoDB = () => { const uuid = require('uuid') const config = require('../config/dynamoConfigLocal') const dynamoConfig = process.env.NODE_ENV === 'production' ? {} : {endpoint: config.endpoint, port: config.port} let dynamoDB = require('@stacksavings/dynamodb') dynamoDB = new dynamoDB(dynamoConfig) const crearPost = async (req) => { if (!req.body.user || !req.body.content) { return {code: 403, res: 'Los parametros user y content son obligatorios'} } else { const idPub = String(uuid.v1()) const timeStamp = String(Math.floor(Date.now() / 1000)) const parametros = { TableName: 'blog', Item: { id: idPub, idUsuario: String(req.body.user), post: req.body.content, fechaCreacion: timeStamp } } const resultInsert = await dynamoDB.insertItems(parametros).catch(error => { return {code: 403, res: error } }) return {code: 200, res: 'Insertado con exito'} } } const obtenerPosts = async (req) => { const parametros = { TableName: 'blog', AttributesToGet: ['id', 'idUsuario', 'post', 'fechaCreacion'] } const resultItems = await dynamoDB.scanItems(parametros).catch(error => { return {code: 403, res: error} }) return {code: 200, res: resultItems.data} } const obtenerPostPorId = async (req) => { const parametros = { TableName: 'blog', ExpressionAttributeNames: { '#id': 'id' }, ExpressionAttributeValues: { ':id': req.params.id }, KeyConditionExpression: '#id = :id' } const resultItems = await dynamoDB.getItems(parametros).catch(error => { return { code: 403, res: error } }) return {code: 200, res: resultItems.data} } return { crearPost: crearPost, obtenerPosts: obtenerPosts, obtenerPostPorId: obtenerPostPorId } }