https://github.com/imjaroiswebdev/dynamoose-sls-crud
Implementation of JSON API endpoints with AWS Gateway using Serverless and Dynamoose
https://github.com/imjaroiswebdev/dynamoose-sls-crud
aws-lambda dynamoose serverless-framework webpack4
Last synced: 2 months ago
JSON representation
Implementation of JSON API endpoints with AWS Gateway using Serverless and Dynamoose
- Host: GitHub
- URL: https://github.com/imjaroiswebdev/dynamoose-sls-crud
- Owner: imjaroiswebdev
- Created: 2018-03-06T11:41:13.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-08T18:54:51.000Z (almost 8 years ago)
- Last Synced: 2025-06-04T22:12:02.074Z (about 1 year ago)
- Topics: aws-lambda, dynamoose, serverless-framework, webpack4
- Language: JavaScript
- Homepage:
- Size: 205 KB
- Stars: 16
- Watchers: 0
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](http://www.serverless.com)
[](https://standardjs.com)
# ⚡ Serverless Basic CRUD API for use of DynamoDB with Dynamoose
#### Basic implementation of JSON API endpoints for creating, reading, update and delete (CRUD) for a generic data structure called just item with the interest of making of this a boilerplate for quick starting projects based on [Serverless Framework](https://serverless.com/) focused in [AWS Lambda](https://aws.amazon.com/lambda/?nc1=h_ls).
## Features
* Includes __webpack 4 support__ for packaging and including babel transpilation.
* Yarn support (_previous versions of serverless webpack did not have well yarn support_).
* Complete supprt for __ES6+ syntax__ up to stage 2.
* Basic schema for "Item" model with [__Dynamoose__](https://github.com/automategreen/dynamoose).
* Create, Read, Update, Delete, List and Batch creation of items.
* In the case of new item data entries it includes auto id creation based on uuid/v1.
* Handlers only manages IO.
* Error parsing, logging and response to endpoint.
* API Gateway path described for every http verb included.
## Starting
```bash
$ yarn
$ export AWS_PROFILE="Your AWS credentials profile"
$ serverless deploy
```
## 📢 Highlight
Every handler is built inside a template strutured for reusing with the folowing parts:
```javascript
export const handler = async ({ body }, context, callback) => {
// Action invocation through promisified method
// handleErr and to are declared below.
const [err, result] = await to(crudItem(body))
// Error handling
if (err) {
callback(null, handleErr(err))
} else {
// Response construction
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(result)
}
console.log(` => Action logging...`)
callback(null, response)
}
}
// 🚧 Outside the handler 🚧
// Function responsible of data manipulation (✨ Where magic happens)
const crudItem = data => {
const itemData = JSON.parse(data)
// Dynamoose promisified API method for data manipulation
return Item.crud()
}
// *** Error handling support in promises
const to = promise =>
promise
.then(data => [null, data])
.catch(err => [pe(err)]) // parse-error (npm module)
const handleErr = (error, statusCode = 500) => {
console.error(' => ERROR:', error.stack)
return {
statusCode,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ error })
}
}
```
### Endpoint description
#### Get an item by its id (handler get.getById)
`GET` -> `https://[api-gateway-code].execute-api.[aws-region].amazonaws.com/[stage]/api/item/:id`
#### List all the stored items (handler list.listAll)
`GET` -> `https://[api-gateway-code].execute-api.[aws-region].amazonaws.com/[stage]/api/items`
#### Add one item (handler add.addOne)
`POST` -> `https://[api-gateway-code].execute-api.[aws-region].amazonaws.com/[stage]/api/add/one`
#### Batch add an array of items (handler batchAdd.batchAdd)
`POST` -> `https://[api-gateway-code].execute-api.[aws-region].amazonaws.com/[stage]/api/add/batch`
#### Update an item (handler update.updateOne)
`PUT` -> `https://[api-gateway-code].execute-api.[aws-region].amazonaws.com/[stage]/api/item/:id`
#### Delete an item (handler delete.deleteOne)
`DELETE` -> `https://[api-gateway-code].execute-api.[aws-region].amazonaws.com/[stage]/api/item/:id`
---
### 🤚 Disclaimer
> Considere this as a quick start project boilerplate if you like some of the things implemented here, because that is the use i plan to give it. Considere that for production level even for little project there room for various validations that were ommited for the sake of general use of this.
## 😎 If you came all through here then i might tell you that you can use this at your will. Enjoy! 🤘