oneday-core
Advanced tools
Comparing version 1.0.9 to 1.0.10
66
db.js
@@ -6,12 +6,26 @@ const aws = require('aws-sdk'); | ||
module.exports.getFromDb = getFromDb; | ||
module.exports.getFromDbElseThrow = getFromDbElseThrow; | ||
module.exports.queryDb = queryDb; | ||
module.exports.queryDbElseThrow = queryDbElseThrow; | ||
async function getFromDbElseThrow(params, filter) { | ||
return await getItem(params, filter, true); | ||
} | ||
async function getFromDb(params, filter) { | ||
return await getItem(params, filter, false); | ||
} | ||
async function getItem(params, filter, isThrow) { | ||
const result = await dynamoDb.get(params).promise(); | ||
if (typeof result.Item === 'undefined') { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
if (isThrow) { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
} else { | ||
return null; | ||
} | ||
} | ||
@@ -22,6 +36,10 @@ | ||
if (filtered.size === 0) { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
if (isThrow) { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
} else { | ||
return null; | ||
} | ||
} | ||
@@ -33,10 +51,22 @@ return filtered; | ||
async function queryDbElseThrow(params, filter) { | ||
return await query(params, filter, true); | ||
} | ||
async function queryDb(params, filter) { | ||
return await query(params, filter, false); | ||
} | ||
async function query(params, filter, isThrow) { | ||
const result = await dynamoDb.query(params).promise(); | ||
if (result.Count === 0) { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
if (isThrow) { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
} else { | ||
return []; | ||
} | ||
} | ||
@@ -47,6 +77,10 @@ | ||
if (filtered.size === 0) { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
if (isThrow) { | ||
throw new { | ||
code: "NOT_FOUND", | ||
message: "No data found for given params: " + params | ||
}; | ||
} else { | ||
return []; | ||
} | ||
} | ||
@@ -53,0 +87,0 @@ return filtered; |
@@ -19,4 +19,7 @@ 'use strict'; | ||
module.exports.getFromDb = db.getFromDb; | ||
module.exports.getFromDbElseThrow = db.getFromDbElseThrow; | ||
module.exports.queryDb = db.queryDb; | ||
module.exports.queryDbElseThrow = db.queryDbElseThrow; | ||
module.exports.hasProperties = util.hasProperties; | ||
@@ -23,0 +26,0 @@ module.exports.isArray = util.isArray; |
{ | ||
"name": "oneday-core", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Basic AWS util functions to ease development.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
14695
394