@xapp/dynamo-service
Advanced tools
Comparing version 0.0.46 to 0.0.47
@@ -43,3 +43,5 @@ import { DynamoDB } from "aws-sdk"; | ||
constructor(db: ConstructorDB); | ||
put(table: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap, condition?: ConditionExpression): Promise<DynamoDB.DocumentClient.PutItemOutput>; | ||
put(TableName: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap): Promise<DynamoDB.DocumentClient.PutItemOutput>; | ||
put(TableName: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap, condition: ConditionExpression): Promise<DynamoDB.DocumentClient.PutItemOutput>; | ||
put(TableName: string, obj: DynamoDB.DocumentClient.PutItemInputAttributeMap[]): Promise<DynamoDB.DocumentClient.PutItemInputAttributeMap[]>; | ||
update<T>(table: string, key: DynamoDB.DocumentClient.Key, update: UpdateBody<T>): Promise<void>; | ||
@@ -63,3 +65,5 @@ update<T>(table: string, key: DynamoDB.DocumentClient.Key, update: UpdateBody<T>, condition: ConditionExpression): Promise<void>; | ||
scan<T, P extends keyof T>(table: string, myParams: ScanParams, projection: P | P[]): Promise<ScanResult<Pick<T, P>>>; | ||
delete(TableName: string, Key: DynamoDB.DocumentClient.Key): Promise<void>; | ||
delete(TableName: string, Key: DynamoDB.DocumentClient.Key | DynamoDB.DocumentClient.Key[]): Promise<void>; | ||
private batchWrites(TableName, writeRequests); | ||
private batchWriteUntilCompleteOrRunout(input, attempts?); | ||
} |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const aws_sdk_1 = require("aws-sdk"); | ||
const Object_1 = require("../utils/Object"); | ||
const util_1 = require("util"); | ||
class DynamoService { | ||
@@ -9,4 +18,13 @@ constructor(db) { | ||
} | ||
put(table, obj, condition = {}) { | ||
const params = Object.assign({ TableName: table, Item: obj }, condition); | ||
put(TableName, obj, condition = {}) { | ||
if (util_1.isArray(obj)) { | ||
return this.batchWrites(TableName, createPutBatchWriteRequests(obj)).then(unprocessed => { | ||
const unProcessedItems = []; | ||
for (let u of unprocessed) { | ||
unProcessedItems.push(u.PutRequest.Item); | ||
} | ||
return unProcessedItems; | ||
}); | ||
} | ||
const params = Object.assign({ TableName, Item: obj }, condition); | ||
return this.db.put(params).promise(); | ||
@@ -87,2 +105,5 @@ } | ||
delete(TableName, Key) { | ||
if (Array.isArray(Key)) { | ||
return this.batchWrites(TableName, createDeleteBatchWriteRequests(Key)).then(r => { }); | ||
} | ||
return this.db.delete({ | ||
@@ -93,4 +114,70 @@ TableName, | ||
} | ||
batchWrites(TableName, writeRequests) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let unprocessedRequests = []; | ||
for (let i = 0; i < writeRequests.length; i += 25) { | ||
const sliced = writeRequests.slice(i, i + 25); | ||
const unprocessed = yield this.batchWriteUntilCompleteOrRunout({ | ||
RequestItems: { | ||
[TableName]: sliced | ||
} | ||
}); | ||
if (Object.keys(unprocessed).length > 0) { | ||
unprocessedRequests.push(...unprocessed[TableName]); | ||
} | ||
} | ||
return unprocessedRequests; | ||
}); | ||
} | ||
batchWriteUntilCompleteOrRunout(input, attempts = 5) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let count = attempts; | ||
let unprocessed; | ||
let writeInput = input; | ||
do { | ||
const result = yield this.db.batchWrite(writeInput).promise(); | ||
writeInput.RequestItems = result.UnprocessedItems; | ||
unprocessed = result.UnprocessedItems; | ||
} while (--count <= 0 && Object.keys(writeInput.RequestItems).length > 0); | ||
return unprocessed; | ||
}); | ||
} | ||
} | ||
exports.DynamoService = DynamoService; | ||
function createPutBatchWriteRequests(objs) { | ||
if (Array.isArray(objs)) { | ||
return objs.map((Item) => { | ||
return { | ||
PutRequest: { | ||
Item | ||
} | ||
}; | ||
}); | ||
} | ||
else { | ||
return [{ | ||
PutRequest: { | ||
Item: objs | ||
} | ||
}]; | ||
} | ||
} | ||
function createDeleteBatchWriteRequests(Keys) { | ||
if (Array.isArray(Keys)) { | ||
return Keys.map((Key) => { | ||
return { | ||
DeleteRequest: { | ||
Key | ||
} | ||
}; | ||
}); | ||
} | ||
else { | ||
return [{ | ||
DeleteRequest: { | ||
Key: Keys | ||
} | ||
}]; | ||
} | ||
} | ||
function addIfExists(original, params, keys = []) { | ||
@@ -97,0 +184,0 @@ const p = params || {}; |
{ | ||
"name": "@xapp/dynamo-service", | ||
"version": "0.0.46", | ||
"version": "0.0.47", | ||
"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
127621
3462