-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamodb-controller.js
More file actions
64 lines (56 loc) · 1.64 KB
/
dynamodb-controller.js
File metadata and controls
64 lines (56 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const AWS = require('aws-sdk');
const config = require('./../../config.js');
const uuidv1 = require('uuid/v1');
//Initiates a scan operation to retrieve all items from a specified DynamoDB table. Sends a response with the retrieved vehicles or an error message.
const getVehicles = function (req, res) {
AWS.config.update(config.aws_remote_config);
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: config.aws_table_name
};
docClient.scan(params, function (err, data) {
if (err) {
console.log(err)
res.send({
success: false,
message: err
});
} else {
const { Items } = data;
res.send({
success: true,
vehicles: Items
});
}
});
}
//Vehicle registration/add
const vregister = function (req, res) {
AWS.config.update(config.aws_remote_config);
const docClient = new AWS.DynamoDB.DocumentClient();
const Item = { ...req.body };
Item.id = uuidv1(); //generate a unique identifier for the new item
var params = {
TableName: config.aws_table_name,
Item: Item
};
// Call DynamoDB to add the item to the table
docClient.put(params, function (err, data) {
if (err) {
res.send({
success: false,
message: err
});
} else {
res.send({
success: true,
message: 'Added vehicle',
vehicle: data
});
}
});
}
module.exports = {
getVehicles,
vregister
}