oneday-core
Advanced tools
Comparing version 1.0.37 to 1.0.38
26
db.js
@@ -14,2 +14,3 @@ const aws = require('aws-sdk'); | ||
module.exports.update = update; | ||
module.exports.softDelete = softDelete; | ||
module.exports.transactionWrite = transactionWrite; | ||
@@ -28,2 +29,3 @@ | ||
if (typeof result.Item === 'undefined') { | ||
@@ -100,4 +102,5 @@ if (isThrow) { | ||
async function create(params, requestId, isPublish = true, topicName = process.env.eventTopic) { | ||
async function create(params, isPublish = true, topicName = process.env.eventTopic) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
const now = new Date().toISOString(); | ||
@@ -107,7 +110,4 @@ params.Item.created = now; | ||
console.log("==> create"); | ||
console.log(require('./variable').getInstanceRequestId()); | ||
console.log('Creating entry in table: ' + params.TableName); | ||
const result = await dynamoDb.put(params).promise(); | ||
console.log('Created entry in table: ' + params.TableName); | ||
console.log(result); | ||
@@ -129,9 +129,7 @@ | ||
async function update(params, requestId, isPublish = true, topicName = process.env.eventTopic) { | ||
async function update(params, isPublish = true, topicName = process.env.eventTopic) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
const now = new Date().toISOString(); | ||
const expr = params.UpdateExpression; | ||
console.log("==> update"); | ||
console.log(require('./variable').getInstanceRequestId()); | ||
if (expr.includes('set')) { | ||
@@ -143,4 +141,4 @@ params.UpdateExpression = expr + ', #modified = :modified'; | ||
console.log('Updating table: ' + params.TableName); | ||
const result = await dynamoDb.update(params).promise(); | ||
console.log('Updated table: ' + params.TableName); | ||
console.log(result); | ||
@@ -154,7 +152,7 @@ | ||
async function softDelete(params, requestId, isPublish = true, topicName = process.env.eventTopic) { | ||
async function softDelete(params, isPublish = true, topicName = process.env.eventTopic) { | ||
const requestId = require('./variable').getInstanceRequestId(); | ||
const now = new Date().toISOString(); | ||
const expr = params.UpdateExpression; | ||
if (expr.includes('set')) { | ||
@@ -168,10 +166,10 @@ params.UpdateExpression = expr + ', #modified = :modified, #deleted = :deleted'; | ||
console.log('Updating table: ' + params.TableName); | ||
const result = await dynamoDb.update(params).promise(); | ||
console.log('Deleted record from table: ' + params.TableName); | ||
console.log(result); | ||
if (isPublish === true) { | ||
await resource.publishToTopic(topicName, enums.event_type.UPDATE, params, requestId, []); | ||
await resource.publishToTopic(topicName, enums.event_type.DELETE, params, requestId, []); | ||
} | ||
return result; | ||
} |
22
error.js
@@ -0,7 +1,9 @@ | ||
const variable = require('./variable'); | ||
class ValidationError extends Error { | ||
constructor(message, requestId) { | ||
constructor(message) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.statusCode = 400; | ||
this.requestId = requestId; | ||
this.requestId = variable.getInstanceRequestId(); | ||
Error.captureStackTrace(this, this.constructor); | ||
@@ -12,7 +14,7 @@ } | ||
class UnauthorizedError extends Error { | ||
constructor(message, requestId) { | ||
constructor(message) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.statusCode = 401; | ||
this.requestId = requestId; | ||
this.requestId = variable.getInstanceRequestId(); | ||
Error.captureStackTrace(this, this.constructor); | ||
@@ -23,7 +25,7 @@ } | ||
class NotFoundError extends Error { | ||
constructor(message, requestId) { | ||
constructor(message) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.statusCode = 404; | ||
this.requestId = requestId; | ||
this.requestId = variable.getInstanceRequestId(); | ||
Error.captureStackTrace(this, this.constructor); | ||
@@ -34,7 +36,7 @@ } | ||
class ConflictError extends Error { | ||
constructor(message, requestId) { | ||
constructor(message) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.statusCode = 409; | ||
this.requestId = requestId; | ||
this.requestId = variable.getInstanceRequestId(); | ||
Error.captureStackTrace(this, this.constructor); | ||
@@ -45,7 +47,7 @@ } | ||
class InternalError extends Error { | ||
constructor(message, requestId) { | ||
constructor(message) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
this.statusCode = 500; | ||
this.requestId = requestId; | ||
this.requestId = variable.getInstanceRequestId(); | ||
Error.captureStackTrace(this, this.constructor); | ||
@@ -52,0 +54,0 @@ } |
32
index.js
@@ -9,4 +9,4 @@ 'use strict'; | ||
const variable = require('./variable'); | ||
const uuid = require('uuid/v4'); | ||
module.exports.saveEvent = saveEvent; | ||
@@ -25,2 +25,3 @@ module.exports.saveError = saveError; | ||
module.exports.update = db.update; | ||
module.exports.softDelete = db.softDelete; | ||
module.exports.transactionWrite = db.transactionWrite; | ||
@@ -46,3 +47,3 @@ | ||
//ToDo: Add response code, global and lambda request tracking numbers | ||
async function saveEvent(event, type, requestId) { // Saves the incoming copy of data to a table as is | ||
async function saveEvent(event, type) { // Saves the incoming copy of data to a table as is | ||
console.log(event); | ||
@@ -52,3 +53,3 @@ const params = { | ||
Item: { | ||
id: requestId, | ||
id: variable.getInstanceRequestId(), | ||
function: type, | ||
@@ -59,7 +60,7 @@ event: event, | ||
}; | ||
return await db.create(params, null, false); | ||
return await db.create(params, false, null); | ||
} | ||
//ToDo: Add global and lambda request tracking numbers | ||
async function saveError(error, data, type, requestId) { // Saves the given error to the error table | ||
async function saveError(error, data, type) { // Saves the given error to the error table | ||
console.error(error); | ||
@@ -70,3 +71,3 @@ const params = { | ||
id: uuid(), | ||
requestId: requestId, | ||
requestId: variable.getInstanceRequestId(), | ||
function: type, | ||
@@ -80,3 +81,3 @@ data: data, | ||
}; | ||
return await db.create(params, null, false); | ||
return await db.create(params, false, null); | ||
} | ||
@@ -86,3 +87,3 @@ | ||
if (event !== null && event !== undefined) { | ||
variable.setInstanceRequestId(event.requestContext.authorizer !== undefined && event.requestContext.authorizer.requestId !== undefined ? event.requestContext.authorizer.requestId : uuid()); | ||
variable.setInstanceRequestId(event.requestContext !== undefined && event.requestContext.requestId !== undefined ? event.requestContext.requestId : uuid()); | ||
} | ||
@@ -93,6 +94,6 @@ return variable.getInstanceRequestId(); | ||
async function updateEventLogResponseCode(requestId, responseCode) { | ||
async function updateEventLogResponseCode(responseCode) { | ||
const params = { | ||
TableName: process.env.incomingTable, | ||
Key: {id: requestId}, | ||
Key: {id: variable.getInstanceRequestId()}, | ||
UpdateExpression: "set #responseCode = :code", | ||
@@ -102,8 +103,9 @@ ExpressionAttributeNames: {'#responseCode': 'responseCode'}, | ||
}; | ||
await db.update(params); | ||
await db.update(params, false, null); | ||
} | ||
async function generateErrorResponse(error, event, caller, requestId) { | ||
async function generateErrorResponse(error, event, caller) { | ||
const requestId = variable.getInstanceRequestId(); | ||
const errorLog = saveError(error, event, caller, requestId); | ||
const eventLog = updateEventLogResponseCode(requestId, error.statusCode); | ||
const eventLog = updateEventLogResponseCode(error.statusCode); | ||
@@ -120,5 +122,5 @@ await errorLog; | ||
async function generateSuccessResponse(body, requestId) { | ||
async function generateSuccessResponse(body) { | ||
await updateEventLogResponseCode(requestId, 200); | ||
await updateEventLogResponseCode(200); | ||
@@ -125,0 +127,0 @@ if (body === null) { |
{ | ||
"name": "oneday-core", | ||
"version": "1.0.37", | ||
"version": "1.0.38", | ||
"description": "Basic AWS util functions to ease development.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,2 +11,3 @@ 'use strict'; | ||
const enums = require('./enum'); | ||
const variable = require('./variable'); | ||
@@ -95,3 +96,3 @@ module.exports.sendToQueue = sendToQueue; | ||
}; | ||
return await db.create(params, null, false); | ||
return await db.create(params, false, null); | ||
} | ||
@@ -98,0 +99,0 @@ } |
24652
661