@xapp/dynamo-service
Advanced tools
Comparing version 0.0.105 to 0.0.106
@@ -38,2 +38,3 @@ import { DynamoDB } from "aws-sdk"; | ||
export declare type Append<T> = Partial<T>; | ||
export declare type Interceptor<T> = (obj: T) => T; | ||
export interface UpdateBody<T> { | ||
@@ -50,3 +51,7 @@ set?: Set<T>; | ||
readonly db: DynamoDB.DocumentClient; | ||
private readonly putInterceptors; | ||
private readonly updateInterceptors; | ||
constructor(db: ConstructorDB); | ||
addPutInterceptor<T>(interceptor: Interceptor<T>): void; | ||
addUpdateInterceptor<T>(interceptor: Interceptor<UpdateBody<T>>): void; | ||
put(TableName: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap): Promise<DynamoDB.DocumentClient.PutItemOutput>; | ||
@@ -53,0 +58,0 @@ put(TableName: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap, condition: ConditionExpression): Promise<DynamoDB.DocumentClient.PutItemOutput>; |
@@ -21,6 +21,21 @@ "use strict"; | ||
this.db = getDb(db); | ||
this.putInterceptors = []; | ||
this.updateInterceptors = []; | ||
} | ||
addPutInterceptor(interceptor) { | ||
if (!interceptor) { | ||
throw new Error("Put interceptor can not be undefined."); | ||
} | ||
this.putInterceptors.push(interceptor); | ||
} | ||
addUpdateInterceptor(interceptor) { | ||
if (!interceptor) { | ||
throw new Error("Update interceptor can not be undefined."); | ||
} | ||
this.updateInterceptors.push(interceptor); | ||
} | ||
put(TableName, obj, condition = {}) { | ||
if (Array.isArray(obj)) { | ||
return this.batchWrites(TableName, createPutBatchWriteRequests(obj), condition).then(unprocessed => { | ||
const putObjs = interceptObj(this.putInterceptors, obj); | ||
if (Array.isArray(putObjs)) { | ||
return this.batchWrites(TableName, createPutBatchWriteRequests(putObjs), condition).then(unprocessed => { | ||
const unProcessedItems = []; | ||
@@ -33,8 +48,9 @@ for (let u of unprocessed) { | ||
} | ||
const params = Object.assign({ TableName, Item: obj }, condition); | ||
const params = Object.assign({ TableName, Item: putObjs }, condition); | ||
return this.db.put(params).promise(); | ||
} | ||
update(table, key, update, conditionOrReturns = {}, returns = "NONE") { | ||
const newUpdate = transferUndefinedToRemove(update); | ||
newUpdate.set = removeUndefinedAndBlanks(update.set); | ||
let newUpdate = interceptObj(this.updateInterceptors, update); | ||
newUpdate = transferUndefinedToRemove(newUpdate); | ||
newUpdate.set = removeUndefinedAndBlanks(newUpdate.set); | ||
const updateExpression = getUpdateParameters(newUpdate); | ||
@@ -165,2 +181,15 @@ const conditionExpression = (typeof conditionOrReturns === "object") ? conditionOrReturns : {}; | ||
exports.DynamoService = DynamoService; | ||
function interceptObj(interceptors, obj) { | ||
return Array.isArray(obj) ? obj.map(o => intercept(interceptors, o)) : intercept(interceptors, obj); | ||
} | ||
function intercept(interceptors, obj) { | ||
let returnObj = obj; | ||
for (const interceptor of interceptors) { | ||
returnObj = interceptor(returnObj); | ||
} | ||
if (returnObj === undefined) { | ||
throw new Error("Interceptors must return an object."); | ||
} | ||
return returnObj; | ||
} | ||
function createPutBatchWriteRequests(objs) { | ||
@@ -220,3 +249,3 @@ if (Array.isArray(objs)) { | ||
else { | ||
throw new ValidationError_1.ValidationError("Could not construct DynamoService. Bad input."); | ||
throw new ValidationError_1.ValidationError("Could not construct DynamoService. Bad input."); | ||
} | ||
@@ -223,0 +252,0 @@ } |
{ | ||
"name": "@xapp/dynamo-service", | ||
"version": "0.0.105", | ||
"version": "0.0.106", | ||
"description": "A dynamo help class which will help maintain data integrity.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
149384
3975