-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (53 loc) · 1.59 KB
/
index.js
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
const aws = require('aws-sdk')
const docClient = new aws.DynamoDB.DocumentClient()
const tableName = process.env.USER_TABLE
async function fill(event, context, callback) {
const users = [
{
name: 'Pepe',
lastName: 'Grillo'
},
{
name: 'Mr.',
lastName: 'Gold'
},
{
name: 'Emma',
lastName: 'Swan'
}
]
await Promise.all(users.map(user => docClient.put({TableName: tableName, Item: user}).promise()))
var response = {
'isBase64Encoded': false,
'headers': { 'Access-Control-Allow-Origin': '*' , 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS'},
'statusCode': 200,
'body': 'OK'
};
callback(null, response);
}
async function list(event, context, callback) {
const params = {TableName: tableName, Select: 'ALL_ATTRIBUTES'}
const {Items} = await docClient.scan(params).promise()
var response = {
'isBase64Encoded': false,
'headers': { 'Access-Control-Allow-Origin': '*' , 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS'},
'statusCode': 200,
'body': JSON.stringify(Items)
};
callback(null, response);
}
exports.handler = async function(event, context, callback){
if(event.httpMethod === 'OPTIONS'){
var response = {
'isBase64Encoded': false,
'headers': { 'Access-Control-Allow-Origin': '*' , 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS'},
'statusCode': 200,
'body': 'OK'
};
callback(null, response);
} else if(event.path === '/fill'){
await fill(event, context, callback)
} else if(event.path === '/list'){
await list(event, context, callback)
}
};