-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathbatch-execute-statement.js
179 lines (176 loc) · 5.44 KB
/
batch-execute-statement.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const REGION = "us-west-2";
// I am using the DynamoDB low level because the DocClient does not support executeStatement used with PartiQL
const { DynamoDB } = require("@aws-sdk/client-dynamodb");
const { unmarshall } = require("@aws-sdk/util-dynamodb");
// Create low level client
const dbclient = new DynamoDB({ region: REGION });
// In PartiQL, we can perform batchWrite operation with backExecuteStatement.
const insertTeams = async (event) => {
let partiqlInsertParams = {
Statements: []
}
for (let team of event.teams) {
let teamParams = {
pk: "TEAM",
sk: team.name + "#" + team.group + "#" + team.ranking,
display_name: team.name,
team_group: team.group,
ranking: team.ranking,
matches_played: team.matches_played,
matches_won: team.matches_won,
matches_drew: team.matches_drew,
matches_lost: team.matches_lost,
goals_for: team.goals_for,
goals_against: team.goals_against,
goals_difference: team.goals_difference,
team_points: team.team_points
}
//PartiQL INSERT statement, with the data passed as parameters.
let partiqlStmt = {
Statement: `INSERT INTO "testing-partiql" VALUE "{'pk':'${teamParams.pk}','sk':'${teamParams.sk}','display_name':'${teamParams.display_name}','team_group':'${teamParams.team_group}','ranking':${teamParams.ranking},'matches_played':${teamParams.matches_played},'matches_won':${teamParams.matches_won},'matches_drew':${teamParams.matches_drew},'matches_lost':${teamParams.matches_lost},'goals_for':${teamParams.goals_for},'goals_against':${teamParams.goals_against},'goals_difference':${teamParams.goals_difference},'team_points':${teamParams.team_points}}"`,
}
partiqlInsertParams.Statements.push(partiqlStmt)
}
//batchExecuteStatement also limits upto max 25 items when performing a put action to DynamoDB.
return response = await dbclient.batchExecuteStatement(partiqlInsertParams).promise()
}
let teamsList = [
{
"name": "Argentina",
"group": "Group A",
"ranking": 1,
"matches_played": 4,
"matches_won": 3,
"matches_drew": 1,
"matches_lost": 0,
"goals_for": 7,
"goals_against": 2,
"goals_difference": 5,
"team_points": 10
},
{
"name": "Uruguay",
"group": "Group A",
"ranking": 2,
"matches_played": 4,
"matches_won": 2,
"matches_drew": 1,
"matches_lost": 1,
"goals_for": 4,
"goals_against": 2,
"goals_difference": 2,
"team_points": 7
},
{
"name": "Paraguay",
"group": "Group A",
"ranking": 3,
"matches_played": 4,
"matches_won": 2,
"matches_drew": 0,
"matches_lost": 2,
"goals_for": 5,
"goals_against": 3,
"goals_difference": 2,
"team_points": 6
},
{
"name": "Chile",
"group": "Group A",
"ranking": 4,
"matches_played": 4,
"matches_won": 1,
"matches_drew": 2,
"matches_lost": 1,
"goals_for": 3,
"goals_against": 4,
"goals_difference": -1,
"team_points": 5
},
{
"name": "Bolivia",
"group": "Group A",
"ranking": 5,
"matches_played": 4,
"matches_won": 0,
"matches_drew": 0,
"matches_lost": 4,
"goals_for": 2,
"goals_against": 10,
"goals_difference": -8,
"team_points": 0
},
{
"name": "Brazil",
"group": "Group b",
"ranking": 1,
"matches_played": 4,
"matches_won": 3,
"matches_drew": 1,
"matches_lost": 0,
"goals_for": 10,
"goals_against": 2,
"goals_difference": 8,
"team_points": 10
},
{
"name": "Peru",
"group": "Group b",
"ranking": 2,
"matches_played": 4,
"matches_won": 2,
"matches_drew": 1,
"matches_lost": 1,
"goals_for": 5,
"goals_against": 7,
"goals_difference": -2,
"team_points": 7
},
{
"name": "Colombia",
"group": "Group B",
"ranking": 3,
"matches_played": 4,
"matches_won": 1,
"matches_drew": 1,
"matches_lost": 2,
"goals_for": 3,
"goals_against": 4,
"goals_difference": -1,
"team_points": 4
},
{
"name": "Ecuador",
"group": "Group B",
"ranking": 5,
"matches_played": 4,
"matches_won": 0,
"matches_drew": 3,
"matches_lost": 1,
"goals_for": 6,
"goals_against": 6,
"goals_difference": -1,
"team_points": 3
},
{
"name": "Venezuela",
"group": "Group B",
"ranking": 5,
"matches_played": 4,
"matches_won": 0,
"matches_drew": 2,
"matches_lost": 2,
"goals_for": 2,
"goals_against": 6,
"goals_difference": -4,
"team_points": 2
}
]
let payload = {
teams: teamsList
}
insertTeams(payload)
.then((data) => {
console.log(JSON.stringify(data, null, 2));
})
.catch ((error) => console.error(JSON.stringify(error, null, 2)));