This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# NodeJS DynamoDB CRUD | ||
|
||
Wraps basic CRUD operations for DynamoDB in Binaris functions. | ||
|
||
# Using the CRUD | ||
|
||
It is assumed you already have a Binaris account. If you don't have an account yet, worry not, signing up is painless and takes just 2 minutes. Visit [Getting Started](https://dev.binaris.com/tutorials/nodejs/getting-started/) to find concise instructions for the process. | ||
|
||
To use any of the functions in this example, you must export the following three variables into your environment (before deployment). | ||
|
||
* `AWS_ACCESS_KEY_ID` # AWS access key | ||
* `AWS_SECRET_ACCESS_KEY` # secret AWS credential | ||
* `AWS_REGION` # AWS region used for DynamoDB | ||
|
||
## Deploy | ||
|
||
A helper command "deploy" is defined in the package.json to simplify the deployment process | ||
|
||
```bash | ||
$ npm run deploy | ||
``` | ||
|
||
## Create the Table | ||
|
||
```bash | ||
$ bn invoke createDriversTable | ||
``` | ||
|
||
## Create a Driver | ||
|
||
```bash | ||
$ npm run createDriver | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
$ bn invoke createDriver --json ./queries/createDriver.json | ||
``` | ||
|
||
## Read a Driver | ||
|
||
```bash | ||
$ npm run readDriver | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
$ bn invoke readDriver --json ./queries/readDriver.json | ||
``` | ||
|
||
## Update a Driver | ||
|
||
```bash | ||
$ npm run updateDriver | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
$ bn invoke updateDriver --json ./queries/updateDriver.json | ||
``` | ||
|
||
## Delete a Driver | ||
|
||
```bash | ||
$ npm run deleteDriver | ||
``` | ||
|
||
or | ||
|
||
```bash | ||
$ bn invoke deleteDriver --json ./queries/deleteDriver.json | ||
``` | ||
|
||
|
||
## Remove | ||
|
||
A helper command "remove" is defined in the package.json to simplify the removal process | ||
|
||
```bash | ||
$ npm run remove | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
functions: | ||
createDriversTable: | ||
file: function.js | ||
entrypoint: createDriversTable | ||
executionModel: concurrent | ||
runtime: node8 | ||
env: | ||
AWS_ACCESS_KEY_ID: | ||
AWS_SECRET_ACCESS_KEY: | ||
AWS_REGION: | ||
createDriver: | ||
file: function.js | ||
entrypoint: createDriver | ||
executionModel: concurrent | ||
runtime: node8 | ||
env: | ||
AWS_ACCESS_KEY_ID: | ||
AWS_SECRET_ACCESS_KEY: | ||
AWS_REGION: | ||
readDriver: | ||
file: function.js | ||
entrypoint: readDriver | ||
executionModel: concurrent | ||
runtime: node8 | ||
env: | ||
AWS_ACCESS_KEY_ID: | ||
AWS_SECRET_ACCESS_KEY: | ||
AWS_REGION: | ||
updateDriver: | ||
file: function.js | ||
entrypoint: updateDriver | ||
executionModel: concurrent | ||
runtime: node8 | ||
env: | ||
AWS_ACCESS_KEY_ID: | ||
AWS_SECRET_ACCESS_KEY: | ||
AWS_REGION: | ||
deleteDriver: | ||
file: function.js | ||
entrypoint: deleteDriver | ||
executionModel: concurrent | ||
runtime: node8 | ||
env: | ||
AWS_ACCESS_KEY_ID: | ||
AWS_SECRET_ACCESS_KEY: | ||
AWS_REGION: |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
functions=( createDriversTable createDriver readDriver updateDriver deleteDriver ) | ||
for i in "${functions[@]}" | ||
do | ||
bn deploy $i | ||
done |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const AWS = require('aws-sdk'); | ||
|
||
const { | ||
AWS_ACCESS_KEY_ID, | ||
AWS_SECRET_ACCESS_KEY, | ||
AWS_REGION | ||
} = process.env; | ||
|
||
AWS.config.update({ | ||
accessKeyId: AWS_ACCESS_KEY_ID, | ||
secretAccessKey: AWS_SECRET_ACCESS_KEY, | ||
region: AWS_REGION, | ||
endpoint: `https://dynamodb.${AWS_REGION}.amazonaws.com`, | ||
}); | ||
|
||
const dynamoDB = new AWS.DynamoDB(); | ||
const docClient = new AWS.DynamoDB.DocumentClient(); | ||
|
||
function validatedBody(body, ...fields) { | ||
for (const field of fields) { | ||
if (!Object.prototype.hasOwnProperty.call(body, field)) { | ||
throw new Error(`Missing request body parameter: ${field}.`); | ||
} | ||
} | ||
return body; | ||
} | ||
|
||
const TableName = 'Drivers'; | ||
const PrimaryKey = 'driverID'; | ||
|
||
exports.createDriversTable = async () => { | ||
return dynamoDB.createTable({ | ||
TableName, | ||
KeySchema: [{ | ||
AttributeName: PrimaryKey, | ||
KeyType: 'HASH', | ||
}], | ||
AttributeDefinitions: [{ | ||
AttributeName: PrimaryKey, | ||
AttributeType: 'S', | ||
}], | ||
ProvisionedThroughput: { | ||
ReadCapacityUnits: 10, | ||
WriteCapacityUnits: 10, | ||
} | ||
}).promise(); | ||
}; | ||
|
||
exports.createDriver = async (body) => { | ||
const { | ||
driverID, | ||
rideStatus, | ||
lastLocation | ||
} = validatedBody(body, 'driverID', 'rideStatus', 'lastLocation'); | ||
|
||
return docClient.put({ | ||
TableName, | ||
Item: { | ||
driverID, | ||
rideStatus, | ||
lastLocation, | ||
} | ||
}).promise(); | ||
}; | ||
|
||
exports.readDriver = async (body) => { | ||
const { driverID } = validatedBody(body, 'driverID'); | ||
return docClient.get({ TableName, Key: { driverID } }).promise(); | ||
}; | ||
|
||
exports.updateDriver = async (body) => { | ||
const { | ||
driverID, | ||
rideStatus, | ||
lastLocation | ||
} = validatedBody(body, 'driverID', 'rideStatus', 'lastLocation'); | ||
|
||
return docClient.update({ | ||
TableName, | ||
Key: { driverID }, | ||
UpdateExpression: 'SET #loc.#lon = :lonVal, #loc.#lat = :latVal, #rideStatus= :r', | ||
ExpressionAttributeNames: { | ||
'#loc': 'lastLocation', | ||
'#lon': 'longitude', | ||
'#lat': 'latitude', | ||
'#rideStatus': 'rideStatus', | ||
}, | ||
ExpressionAttributeValues: { | ||
':r': rideStatus, | ||
':lonVal': lastLocation.longitude, | ||
':latVal': lastLocation.latitude, | ||
}, | ||
ReturnValues: 'UPDATED_NEW' | ||
}).promise(); | ||
}; | ||
|
||
exports.deleteDriver = async (body) => { | ||
const { driverID } = validatedBody(body, 'driverID'); | ||
return docClient.delete({ TableName, Key: { driverID } }).promise(); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "nodejs-crud-dynamo", | ||
"version": "1.0.0", | ||
"private": true, | ||
"license": "MIT", | ||
"description": "An example CRUD around DynamoDB", | ||
"main": "function.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/binaris/functions-examples.git" | ||
}, | ||
"scripts": { | ||
"deploy": "./deploy", | ||
"remove": "./remove", | ||
"createDriver": "bn invoke createDriver --json ./queries/createDriver.json", | ||
"readDriver": "bn invoke readDriver --json ./queries/readDriver.json", | ||
"updateDriver": "bn invoke updateDriver --json ./queries/updateDriver.json", | ||
"deleteDriver": "bn invoke deleteDriver --json ./queries/deleteDriver.json" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/binaris/functions-examples/issues" | ||
}, | ||
"homepage": "https://github.com/binaris/functions-examples/nodejs-crud-dynamo/README.md", | ||
"author": "Ryland Goldstein", | ||
"dependencies": { | ||
"aws-sdk": "^2.430.0" | ||
}, | ||
"keywords": [ | ||
"Binaris", | ||
"FaaS", | ||
"CRUD", | ||
"DynamoDB" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"driverID": "a21s312qew313hdg", | ||
"rideStatus": "HAS_RIDER", | ||
"lastLocation": { | ||
"longitude": "-77.0364", | ||
"latitude": "38.8951" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"driverID": "a21s312qew313hdg" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"driverID": "a21s312qew313hdg" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"driverID": "a21s312qew313hdg", | ||
"rideStatus": "NO_RIDER", | ||
"lastLocation": { | ||
"longitude": "-78.0364", | ||
"latitude": "38.8851" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
functions=( createDriversTable createDriver readDriver updateDriver deleteDriver ) | ||
for i in "${functions[@]}" | ||
do | ||
bn remove $i | ||
done |