Skip to content

Commit

Permalink
NodeJS Upsert Lambda Function - putItem method fixed
Browse files Browse the repository at this point in the history
- Unit tests added
  • Loading branch information
juanroldan1989 committed Feb 22, 2024
1 parent cc8bafe commit a545968
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 122 deletions.
45 changes: 23 additions & 22 deletions terraform/upsert_lambda_function/coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1678989003263" clover="3.2.0">
<project timestamp="1678989003263" name="All files">
<metrics statements="27" coveredstatements="27" conditionals="8" coveredconditionals="8" methods="2" coveredmethods="2" elements="37" coveredelements="37" complexity="0" loc="27" ncloc="27" packages="1" files="1" classes="1"/>
<coverage generated="1708641084476" clover="3.2.0">
<project timestamp="1708641084476" name="All files">
<metrics statements="28" coveredstatements="28" conditionals="10" coveredconditionals="10" methods="3" coveredmethods="3" elements="41" coveredelements="41" complexity="0" loc="28" ncloc="28" packages="1" files="1" classes="1"/>
<file name="function.js" path="/Users/juan/code/terraform-url-shortener/terraform/upsert_lambda_function/function.js">
<metrics statements="27" coveredstatements="27" conditionals="8" coveredconditionals="8" methods="2" coveredmethods="2"/>
<metrics statements="28" coveredstatements="28" conditionals="10" coveredconditionals="10" methods="3" coveredmethods="3"/>
<line num="1" count="1" type="stmt"/>
<line num="2" count="1" type="stmt"/>
<line num="4" count="1" type="stmt"/>
<line num="5" count="5" type="stmt"/>
<line num="7" count="5" type="stmt"/>
<line num="9" count="5" type="stmt"/>
<line num="10" count="5" type="stmt"/>
<line num="14" count="5" type="cond" truecount="2" falsecount="0"/>
<line num="16" count="5" type="stmt"/>
<line num="17" count="4" type="stmt"/>
<line num="18" count="4" type="stmt"/>
<line num="20" count="4" type="stmt"/>
<line num="21" count="4" type="stmt"/>
<line num="22" count="4" type="stmt"/>
<line num="24" count="4" type="cond" truecount="2" falsecount="0"/>
<line num="25" count="3" type="cond" truecount="2" falsecount="0"/>
<line num="5" count="6" type="stmt"/>
<line num="7" count="6" type="stmt"/>
<line num="9" count="6" type="stmt"/>
<line num="11" count="6" type="cond" truecount="2" falsecount="0"/>
<line num="13" count="6" type="stmt"/>
<line num="14" count="5" type="stmt"/>
<line num="15" count="5" type="stmt"/>
<line num="17" count="5" type="stmt"/>
<line num="18" count="5" type="stmt"/>
<line num="19" count="5" type="stmt"/>
<line num="21" count="5" type="cond" truecount="2" falsecount="0"/>
<line num="22" count="4" type="cond" truecount="2" falsecount="0"/>
<line num="23" count="3" type="cond" truecount="2" falsecount="0"/>
<line num="25" count="2" type="stmt"/>
<line num="26" count="2" type="cond" truecount="2" falsecount="0"/>
<line num="27" count="1" type="stmt"/>
<line num="28" count="1" type="stmt"/>
<line num="29" count="1" type="stmt"/>
<line num="31" count="1" type="stmt"/>
<line num="33" count="1" type="stmt"/>
<line num="35" count="1" type="stmt"/>
<line num="32" count="1" type="stmt"/>
<line num="36" count="3" type="stmt"/>
<line num="37" count="3" type="stmt"/>
<line num="38" count="3" type="stmt"/>
<line num="39" count="3" type="stmt"/>
<line num="43" count="5" type="stmt"/>
<line num="48" count="5" type="stmt"/>
<line num="42" count="6" type="stmt"/>
<line num="47" count="6" type="stmt"/>
</file>
</project>
</coverage>
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"/Users/juan/code/terraform-url-shortener/terraform/upsert_lambda_function/function.js": {"path":"/Users/juan/code/terraform-url-shortener/terraform/upsert_lambda_function/function.js","statementMap":{"0":{"start":{"line":1,"column":12},"end":{"line":1,"column":30}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":43}},"2":{"start":{"line":4,"column":0},"end":{"line":49,"column":2}},"3":{"start":{"line":5,"column":2},"end":{"line":5,"column":65}},"4":{"start":{"line":7,"column":14},"end":{"line":7,"column":32}},"5":{"start":{"line":9,"column":19},"end":{"line":9,"column":22}},"6":{"start":{"line":10,"column":15},"end":{"line":12,"column":3}},"7":{"start":{"line":14,"column":2},"end":{"line":14,"column":73}},"8":{"start":{"line":14,"column":35},"end":{"line":14,"column":71}},"9":{"start":{"line":16,"column":2},"end":{"line":41,"column":5}},"10":{"start":{"line":17,"column":20},"end":{"line":17,"column":31}},"11":{"start":{"line":18,"column":4},"end":{"line":18,"column":76}},"12":{"start":{"line":20,"column":4},"end":{"line":40,"column":5}},"13":{"start":{"line":21,"column":6},"end":{"line":21,"column":40}},"14":{"start":{"line":22,"column":6},"end":{"line":22,"column":49}},"15":{"start":{"line":24,"column":6},"end":{"line":24,"column":87}},"16":{"start":{"line":24,"column":31},"end":{"line":24,"column":85}},"17":{"start":{"line":25,"column":6},"end":{"line":25,"column":93}},"18":{"start":{"line":25,"column":34},"end":{"line":25,"column":91}},"19":{"start":{"line":26,"column":6},"end":{"line":26,"column":111}},"20":{"start":{"line":26,"column":43},"end":{"line":26,"column":109}},"21":{"start":{"line":28,"column":6},"end":{"line":28,"column":64}},"22":{"start":{"line":29,"column":6},"end":{"line":29,"column":77}},"23":{"start":{"line":31,"column":6},"end":{"line":31,"column":38}},"24":{"start":{"line":33,"column":6},"end":{"line":33,"column":42}},"25":{"start":{"line":35,"column":6},"end":{"line":35,"column":35}},"26":{"start":{"line":37,"column":6},"end":{"line":37,"column":49}},"27":{"start":{"line":38,"column":6},"end":{"line":38,"column":23}},"28":{"start":{"line":39,"column":6},"end":{"line":39,"column":33}},"29":{"start":{"line":43,"column":19},"end":{"line":46,"column":3}},"30":{"start":{"line":48,"column":2},"end":{"line":48,"column":18}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":4,"column":18},"end":{"line":4,"column":19}},"loc":{"start":{"line":4,"column":44},"end":{"line":49,"column":1}},"line":4},"1":{"name":"(anonymous_1)","decl":{"start":{"line":16,"column":24},"end":{"line":16,"column":25}},"loc":{"start":{"line":16,"column":40},"end":{"line":41,"column":3}},"line":16}},"branchMap":{"0":{"loc":{"start":{"line":14,"column":2},"end":{"line":14,"column":73}},"type":"if","locations":[{"start":{"line":14,"column":2},"end":{"line":14,"column":73}},{"start":{},"end":{}}],"line":14},"1":{"loc":{"start":{"line":24,"column":6},"end":{"line":24,"column":87}},"type":"if","locations":[{"start":{"line":24,"column":6},"end":{"line":24,"column":87}},{"start":{},"end":{}}],"line":24},"2":{"loc":{"start":{"line":25,"column":6},"end":{"line":25,"column":93}},"type":"if","locations":[{"start":{"line":25,"column":6},"end":{"line":25,"column":93}},{"start":{},"end":{}}],"line":25},"3":{"loc":{"start":{"line":26,"column":6},"end":{"line":26,"column":111}},"type":"if","locations":[{"start":{"line":26,"column":6},"end":{"line":26,"column":111}},{"start":{},"end":{}}],"line":26}},"s":{"0":1,"1":1,"2":1,"3":5,"4":5,"5":5,"6":5,"7":5,"8":1,"9":5,"10":4,"11":4,"12":4,"13":4,"14":4,"15":4,"16":1,"17":3,"18":1,"19":2,"20":1,"21":1,"22":1,"23":1,"24":1,"25":1,"26":3,"27":3,"28":3,"29":5,"30":5},"f":{"0":5,"1":4},"b":{"0":[1,4],"1":[1,3],"2":[1,2],"3":[1,1]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"d5acc2aee5a261f216db252b431fc36b5c9637ef"}
{"/Users/juan/code/terraform-url-shortener/terraform/upsert_lambda_function/function.js": {"path":"/Users/juan/code/terraform-url-shortener/terraform/upsert_lambda_function/function.js","statementMap":{"0":{"start":{"line":1,"column":12},"end":{"line":1,"column":30}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":43}},"2":{"start":{"line":4,"column":0},"end":{"line":48,"column":2}},"3":{"start":{"line":5,"column":2},"end":{"line":5,"column":65}},"4":{"start":{"line":7,"column":14},"end":{"line":7,"column":32}},"5":{"start":{"line":9,"column":19},"end":{"line":9,"column":22}},"6":{"start":{"line":11,"column":2},"end":{"line":11,"column":73}},"7":{"start":{"line":11,"column":35},"end":{"line":11,"column":71}},"8":{"start":{"line":13,"column":2},"end":{"line":40,"column":5}},"9":{"start":{"line":14,"column":18},"end":{"line":14,"column":29}},"10":{"start":{"line":15,"column":4},"end":{"line":15,"column":76}},"11":{"start":{"line":17,"column":4},"end":{"line":39,"column":5}},"12":{"start":{"line":18,"column":6},"end":{"line":18,"column":40}},"13":{"start":{"line":19,"column":6},"end":{"line":19,"column":49}},"14":{"start":{"line":21,"column":6},"end":{"line":21,"column":87}},"15":{"start":{"line":21,"column":31},"end":{"line":21,"column":85}},"16":{"start":{"line":22,"column":6},"end":{"line":22,"column":93}},"17":{"start":{"line":22,"column":34},"end":{"line":22,"column":91}},"18":{"start":{"line":23,"column":6},"end":{"line":23,"column":111}},"19":{"start":{"line":23,"column":43},"end":{"line":23,"column":109}},"20":{"start":{"line":25,"column":6},"end":{"line":34,"column":9}},"21":{"start":{"line":26,"column":8},"end":{"line":33,"column":9}},"22":{"start":{"line":27,"column":10},"end":{"line":27,"column":53}},"23":{"start":{"line":28,"column":10},"end":{"line":28,"column":27}},"24":{"start":{"line":29,"column":10},"end":{"line":29,"column":37}},"25":{"start":{"line":31,"column":10},"end":{"line":31,"column":48}},"26":{"start":{"line":32,"column":10},"end":{"line":32,"column":30}},"27":{"start":{"line":36,"column":6},"end":{"line":36,"column":49}},"28":{"start":{"line":37,"column":6},"end":{"line":37,"column":23}},"29":{"start":{"line":38,"column":6},"end":{"line":38,"column":33}},"30":{"start":{"line":42,"column":19},"end":{"line":45,"column":3}},"31":{"start":{"line":47,"column":2},"end":{"line":47,"column":18}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":4,"column":18},"end":{"line":4,"column":19}},"loc":{"start":{"line":4,"column":29},"end":{"line":48,"column":1}},"line":4},"1":{"name":"(anonymous_1)","decl":{"start":{"line":13,"column":24},"end":{"line":13,"column":25}},"loc":{"start":{"line":13,"column":36},"end":{"line":40,"column":3}},"line":13},"2":{"name":"(anonymous_2)","decl":{"start":{"line":25,"column":31},"end":{"line":25,"column":32}},"loc":{"start":{"line":25,"column":51},"end":{"line":34,"column":7}},"line":25}},"branchMap":{"0":{"loc":{"start":{"line":11,"column":2},"end":{"line":11,"column":73}},"type":"if","locations":[{"start":{"line":11,"column":2},"end":{"line":11,"column":73}},{"start":{},"end":{}}],"line":11},"1":{"loc":{"start":{"line":21,"column":6},"end":{"line":21,"column":87}},"type":"if","locations":[{"start":{"line":21,"column":6},"end":{"line":21,"column":87}},{"start":{},"end":{}}],"line":21},"2":{"loc":{"start":{"line":22,"column":6},"end":{"line":22,"column":93}},"type":"if","locations":[{"start":{"line":22,"column":6},"end":{"line":22,"column":93}},{"start":{},"end":{}}],"line":22},"3":{"loc":{"start":{"line":23,"column":6},"end":{"line":23,"column":111}},"type":"if","locations":[{"start":{"line":23,"column":6},"end":{"line":23,"column":111}},{"start":{},"end":{}}],"line":23},"4":{"loc":{"start":{"line":26,"column":8},"end":{"line":33,"column":9}},"type":"if","locations":[{"start":{"line":26,"column":8},"end":{"line":33,"column":9}},{"start":{"line":30,"column":15},"end":{"line":33,"column":9}}],"line":26}},"s":{"0":1,"1":1,"2":1,"3":6,"4":6,"5":6,"6":6,"7":1,"8":6,"9":5,"10":5,"11":5,"12":5,"13":5,"14":5,"15":1,"16":4,"17":1,"18":3,"19":1,"20":2,"21":2,"22":1,"23":1,"24":1,"25":1,"26":1,"27":3,"28":3,"29":3,"30":6,"31":6},"f":{"0":6,"1":5,"2":2},"b":{"0":[1,5],"1":[1,4],"2":[1,3],"3":[1,2],"4":[1,1]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"175dd9b08732889a7862a72256933943772c0228"}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ <h1><a href="index.html">All files</a> function.js</h1>
<div class='fl pad1y space-right2'>
<span class="strong">100% </span>
<span class="quiet">Statements</span>
<span class='fraction'>31/31</span>
<span class='fraction'>32/32</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">100% </span>
<span class="quiet">Branches</span>
<span class='fraction'>8/8</span>
<span class='fraction'>10/10</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">100% </span>
<span class="quiet">Functions</span>
<span class='fraction'>2/2</span>
<span class='fraction'>3/3</span>
</div>


<div class='fl pad1y space-right2'>
<span class="strong">100% </span>
<span class="quiet">Lines</span>
<span class='fraction'>27/27</span>
<span class='fraction'>28/28</span>
</div>


Expand Down Expand Up @@ -111,73 +111,68 @@ <h1><a href="index.html">All files</a> function.js</h1>
<a name='L46'></a><a href='#L46'>46</a>
<a name='L47'></a><a href='#L47'>47</a>
<a name='L48'></a><a href='#L48'>48</a>
<a name='L49'></a><a href='#L49'>49</a>
<a name='L50'></a><a href='#L50'>50</a></td><td class="line-coverage quiet"><span class="cline-any cline-yes">1x</span>
<a name='L49'></a><a href='#L49'>49</a></td><td class="line-coverage quiet"><span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">6x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">6x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">6x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-yes">3x</span>
<span class="cline-any cline-yes">2x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">2x</span>
<span class="cline-any cline-yes">2x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">3x</span>
<span class="cline-any cline-yes">3x</span>
<span class="cline-any cline-yes">3x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">6x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">6x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
&nbsp;
exports.handler = async (event, context) =&gt; {
exports.handler = (event) =&gt; {
console.log('Received event:', JSON.stringify(event, null, 2));
&nbsp;
const ddb = new AWS.DynamoDB();
let responseBody;
let statusCode = 200;
let params = {
TableName: 'Urls'
};
&nbsp;
if (event.Records.length == 0) { responseBody = '0 records provided'; };
&nbsp;
event.Records.forEach(async record =&gt; {
const message = record.body;
event.Records.forEach((record) =&gt; {
let message = record.body;
console.log('UPSERT LAMBDA FUNCTION - SQS Message received: ', message);
&nbsp;
try {
Expand All @@ -188,14 +183,16 @@ <h1><a href="index.html">All files</a> function.js</h1>
if (!messageJSON.Item.Id) { throw new Error('`Item.Id` not provided within message'); };
if (!messageJSON.Item.OriginalUrl) { throw new Error('`Item.OriginalUrl` not provided within message'); };
&nbsp;
params['Item'] = { 'Id' : { S : messageJSON.Item.Id.S } };
params['Item']['OriginalUrl'] = { S : messageJSON.Item.OriginalUrl.S };
&nbsp;
console.log("PARAMS: ", params);
&nbsp;
await ddb.putItem(params).promise();
&nbsp;
console.log("AFTER putItem");
ddb.putItem(messageJSON, function(err, data) {
if (err) {
console.log("UPSERT ERROR: ", err.message);
statusCode = 400;
responseBody = err.message;
} else {
console.log("UPSERT SUCCESS: ", data);
responseBody = data;
}
});
} catch (err) {
console.log("UPSERT ERROR: ", err.message);
statusCode = 400;
Expand All @@ -217,7 +214,7 @@ <h1><a href="index.html">All files</a> function.js</h1>
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
at 2023-03-16T17:50:03.251Z
at 2024-02-22T22:31:24.465Z
</div>
<script src="prettify.js"></script>
<script>
Expand Down
Loading

0 comments on commit a545968

Please sign in to comment.