Mirroring our previous efforts in Express, today, we will be wiring up a completely serverless, let fully functional, CRUD-Enabled API.
Creating a serverless API: Checklist
Create Table
When writing code that connects to a Dynamo Database, you’ll need to know your AWS credentials and install dynamoose
as a dependency
https://dynamoosejs.com/getting_started/Introduction
This is just like Mongoose!
'use strict';
const dynamoose = require('dynamoose');
const friendsSchema = new dynamoose.Schema({
'id': String,
'name': String,
'phone': String,
});
module.exports = dynamoose.model('friends', friendsSchema);
Be sure your Lambda function has full permissions for API Gateway, DynamoDB, and Cloudwatch
The actul schema and CRUD ops are very similar Mongoose and MongoDB
const contentModel = require('./friends.schema.js');
async function findRecord(id) {
const content = await contentModel.query("id").eq(id).exec();
console.log(content[0]);
}
async function saveRecord(name, phone) {
const id = uuid();
const record = new contentModel({ id, name, phone });
const data = await record.save();
console.log(data);
}
As your routes are invoked by users, those lambda’s will fire, with the event
receiving any POST or QUERY data