concept DynamoDB database in category node
appears as: DynamoDB database

This is an excerpt from Manning's book Serverless Applications with Node.js.
Listing 9.7 Order pizza handler connected to the DynamoDB database
'use strict' const AWS = require('aws-sdk') #1 const docClient = new AWS.DynamoDB.DocumentClient() #2 const pizzas = require('../data/pizzas.json') const pizzaMenu = require('./pizza-menu') const uuid = require('uuid/v4') #3 function orderPizza(pizzaId, sender) { const pizza = pizzas.find(pizza => pizza.id == pizzaId) return docClient.put({ #4 TableName: 'pizza-orders', Item: { orderId: uuid(), #5 pizza: pizzaId, orderStatus: 'in-progress', #6 platform: 'fb-messenger-chatbot', #7 user: sender #8 } }).promise() .then((res) => { return 'Where do you want your pizza to be delivered?' #9 }) .catch((err) => { console.log(err) return [ #10 'Oh! Something went wrong. Can you please try again?', pizzaMenu() ] }) } module.exports = orderPizza #1 Import the AWS SDK. #2 Create an instance of the DocumentClient. #3 Import the uuid module. #4 Save the order in the DynamoDB table. #5 Use the uuid function to generate a unique ID for the order. #6 Set the order status to in-progress. #7 Save the platform that was used for this order. #8 Save the ID of the user that sent the message. #9 Ask the user for the delivery address. #10 Show a friendly error message and the pizza menu again in the event of an error.