Skip to content
This repository was archived by the owner on May 21, 2020. It is now read-only.

Commit 8acd1d7

Browse files
committed
add nodejs dynamo crud
1 parent f9b447a commit 8acd1d7

File tree

11 files changed

+299
-0
lines changed

11 files changed

+299
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ Each example contains a README.md with an explanation about the solution and it'
2727
| [`kmeans-sklearn`](kmeans-sklearn) <br/> Simple kmeans examples using sklearn | python2 |
2828
| [`naive-serverless-map-reduce`](naive-serverless-map-reduce) <br/> Naive serverless MapReduce | nodeJS |
2929
| [`typescript-function`](typescript-function) <br/> Boilerplate TypeScript Binaris function | nodeJS and TypeScript |
30+
| [`nodejs-crud-dynamo`](nodejs-crud-dynamo) <br/> Basic CRUD example using DynamoDB | nodeJS |

nodejs-crud-dynamo/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# NodeJS DynamoDB CRUD
2+
3+
Wraps basic CRUD operations for DynamoDB in Binaris functions.
4+
5+
# Using the CRUD
6+
7+
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.
8+
9+
To use any of the functions in this example, you must export the following three variables into your environment (before deployment).
10+
11+
* `AWS_ACCESS_KEY_ID` # AWS access key
12+
* `AWS_SECRET_ACCESS_KEY` # secret AWS credential
13+
* `AWS_REGION` # AWS region used for DynamoDB
14+
15+
## Deploy
16+
17+
A helper command "deploy" is defined in the package.json to simplify the deployment process
18+
19+
```bash
20+
$ npm run deploy
21+
```
22+
23+
## Create the Table
24+
25+
```bash
26+
$ bn invoke createDriversTable
27+
```
28+
29+
## Create a Driver
30+
31+
```bash
32+
$ npm run createDriver
33+
```
34+
35+
or
36+
37+
```bash
38+
$ bn invoke createDriver --json ./queries/createDriver.json
39+
```
40+
41+
## Read a Driver
42+
43+
```bash
44+
$ npm run readDriver
45+
```
46+
47+
or
48+
49+
```bash
50+
$ bn invoke readDriver --json ./queries/readDriver.json
51+
```
52+
53+
## Update a Driver
54+
55+
```bash
56+
$ npm run updateDriver
57+
```
58+
59+
or
60+
61+
```bash
62+
$ bn invoke updateDriver --json ./queries/updateDriver.json
63+
```
64+
65+
## Delete a Driver
66+
67+
```bash
68+
$ npm run deleteDriver
69+
```
70+
71+
or
72+
73+
```bash
74+
$ bn invoke deleteDriver --json ./queries/deleteDriver.json
75+
```
76+
77+
78+
## Remove
79+
80+
A helper command "remove" is defined in the package.json to simplify the removal process
81+
82+
```bash
83+
$ npm run remove
84+
```

nodejs-crud-dynamo/binaris.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
functions:
2+
createDriversTable:
3+
file: function.js
4+
entrypoint: createDriversTable
5+
executionModel: concurrent
6+
runtime: node8
7+
env:
8+
AWS_ACCESS_KEY_ID:
9+
AWS_SECRET_ACCESS_KEY:
10+
AWS_REGION:
11+
createDriver:
12+
file: function.js
13+
entrypoint: createDriver
14+
executionModel: concurrent
15+
runtime: node8
16+
env:
17+
AWS_ACCESS_KEY_ID:
18+
AWS_SECRET_ACCESS_KEY:
19+
AWS_REGION:
20+
readDriver:
21+
file: function.js
22+
entrypoint: readDriver
23+
executionModel: concurrent
24+
runtime: node8
25+
env:
26+
AWS_ACCESS_KEY_ID:
27+
AWS_SECRET_ACCESS_KEY:
28+
AWS_REGION:
29+
updateDriver:
30+
file: function.js
31+
entrypoint: updateDriver
32+
executionModel: concurrent
33+
runtime: node8
34+
env:
35+
AWS_ACCESS_KEY_ID:
36+
AWS_SECRET_ACCESS_KEY:
37+
AWS_REGION:
38+
deleteDriver:
39+
file: function.js
40+
entrypoint: deleteDriver
41+
executionModel: concurrent
42+
runtime: node8
43+
env:
44+
AWS_ACCESS_KEY_ID:
45+
AWS_SECRET_ACCESS_KEY:
46+
AWS_REGION:

nodejs-crud-dynamo/deploy.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
functions=( createDriversTable createDriver readDriver updateDriver deleteDriver )
3+
for i in "${functions[@]}"
4+
do
5+
bn deploy $i
6+
done

nodejs-crud-dynamo/function.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const AWS = require('aws-sdk');
2+
3+
const {
4+
AWS_ACCESS_KEY_ID,
5+
AWS_SECRET_ACCESS_KEY,
6+
AWS_REGION
7+
} = process.env;
8+
9+
AWS.config.update({
10+
accessKeyId: AWS_ACCESS_KEY_ID,
11+
secretAccessKey: AWS_SECRET_ACCESS_KEY,
12+
region: AWS_REGION,
13+
endpoint: `https://dynamodb.${AWS_REGION}.amazonaws.com`,
14+
});
15+
16+
const dynamoDB = new AWS.DynamoDB();
17+
const docClient = new AWS.DynamoDB.DocumentClient();
18+
19+
function validatedBody(body, ...fields) {
20+
for (const field of fields) {
21+
if (!Object.prototype.hasOwnProperty.call(body, field)) {
22+
throw new Error(`Missing request body parameter: ${field}.`);
23+
}
24+
}
25+
return body;
26+
}
27+
28+
const TableName = 'Drivers';
29+
const PrimaryKey = 'driverID';
30+
31+
exports.createDriversTable = async () => {
32+
return dynamoDB.createTable({
33+
TableName,
34+
KeySchema: [{
35+
AttributeName: PrimaryKey,
36+
KeyType: 'HASH',
37+
}],
38+
AttributeDefinitions: [{
39+
AttributeName: PrimaryKey,
40+
AttributeType: 'S',
41+
}],
42+
ProvisionedThroughput: {
43+
ReadCapacityUnits: 10,
44+
WriteCapacityUnits: 10,
45+
}
46+
}).promise();
47+
};
48+
49+
exports.createDriver = async (body) => {
50+
const {
51+
driverID,
52+
rideStatus,
53+
lastLocation
54+
} = validatedBody(body, 'driverID', 'rideStatus', 'lastLocation');
55+
56+
return docClient.put({
57+
TableName,
58+
Item: {
59+
driverID,
60+
rideStatus,
61+
lastLocation,
62+
}
63+
}).promise();
64+
};
65+
66+
exports.readDriver = async (body) => {
67+
const { driverID } = validatedBody(body, 'driverID');
68+
return docClient.get({ TableName, Key: { driverID } }).promise();
69+
};
70+
71+
exports.updateDriver = async (body) => {
72+
const {
73+
driverID,
74+
rideStatus,
75+
lastLocation
76+
} = validatedBody(body, 'driverID', 'rideStatus', 'lastLocation');
77+
78+
return docClient.update({
79+
TableName,
80+
Key: { driverID },
81+
UpdateExpression: 'SET #loc.#lon = :lonVal, #loc.#lat = :latVal, #rideStatus= :r',
82+
ExpressionAttributeNames: {
83+
'#loc': 'lastLocation',
84+
'#lon': 'longitude',
85+
'#lat': 'latitude',
86+
'#rideStatus': 'rideStatus',
87+
},
88+
ExpressionAttributeValues: {
89+
':r': rideStatus,
90+
':lonVal': lastLocation.longitude,
91+
':latVal': lastLocation.latitude,
92+
},
93+
ReturnValues: 'UPDATED_NEW'
94+
}).promise();
95+
};
96+
97+
exports.deleteDriver = async (body) => {
98+
const { driverID } = validatedBody(body, 'driverID');
99+
return docClient.delete({ TableName, Key: { driverID } }).promise();
100+
};

nodejs-crud-dynamo/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "nodejs-crud-dynamo",
3+
"version": "1.0.0",
4+
"private": true,
5+
"license": "MIT",
6+
"description": "An example CRUD around DynamoDB",
7+
"main": "function.js",
8+
"repository": {
9+
"type": "git",
10+
"url": "git+https://github.com/binaris/functions-examples.git"
11+
},
12+
"scripts": {
13+
"deploy": "./deploy",
14+
"remove": "./remove",
15+
"createDriver": "bn invoke createDriver --json ./queries/createDriver.json",
16+
"readDriver": "bn invoke readDriver --json ./queries/readDriver.json",
17+
"updateDriver": "bn invoke updateDriver --json ./queries/updateDriver.json",
18+
"deleteDriver": "bn invoke deleteDriver --json ./queries/deleteDriver.json"
19+
},
20+
"bugs": {
21+
"url": "https://github.com/binaris/functions-examples/issues"
22+
},
23+
"homepage": "https://github.com/binaris/functions-examples/nodejs-crud-dynamo/README.md",
24+
"author": "Ryland Goldstein",
25+
"dependencies": {
26+
"aws-sdk": "^2.430.0"
27+
},
28+
"keywords": [
29+
"Binaris",
30+
"FaaS",
31+
"CRUD",
32+
"DynamoDB"
33+
]
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"driverID": "a21s312qew313hdg",
3+
"rideStatus": "HAS_RIDER",
4+
"lastLocation": {
5+
"longitude": "-77.0364",
6+
"latitude": "38.8951"
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"driverID": "a21s312qew313hdg"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"driverID": "a21s312qew313hdg"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"driverID": "a21s312qew313hdg",
3+
"rideStatus": "NO_RIDER",
4+
"lastLocation": {
5+
"longitude": "-78.0364",
6+
"latitude": "38.8851"
7+
}
8+
}

0 commit comments

Comments
 (0)