Skip to content

Commit

Permalink
update default max retry, retrymode default to adaptive, fix sanitizi…
Browse files Browse the repository at this point in the history
…ng attribute special char in attr value placeholder
  • Loading branch information
frankleng committed Dec 25, 2023
1 parent 97d9717 commit 5ee40d6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dynadash",
"version": "1.9.18",
"version": "1.9.19",
"description": "DynamoDb helpers",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ export let ddbClientInstance: DynamoDBClient | null = null;

export function initDdbClient(
credentialDefaultProvider?: DynamoDBClientConfig["credentialDefaultProvider"],
maxAttempts = 5,
maxAttempts = 10,
retryMode: DynamoDBClientConfig["retryMode"] = "adaptive",
): DynamoDBClient {
if (!ddbClientInstance) {
const config: DynamoDBClientConfig = {
maxAttempts,
retryMode,
};
if (credentialDefaultProvider) {
config.credentialDefaultProvider = credentialDefaultProvider;
Expand Down
24 changes: 14 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BatchWriteItemInput,
Capacity,
ConsumedCapacity,
DynamoDBServiceException,
QueryCommandInput,
QueryCommandOutput,
WriteRequest,
Expand Down Expand Up @@ -41,12 +42,13 @@ export function getExpressionFromMap(type: "FilterExpression" | "KeyConditionExp
const ExpressionAttributeValues: { [key: string]: string | number } = {};
const ExpressionAttributeNames: { [key: string]: string } = {};
try {
for (const key in map) {
for (const k in map) {
const key = cleanAttributeName(k);
if (map.hasOwnProperty(key)) {
const v = map[key];
const attribute = `#${key}`;
const anchor = `:${key}`;
ExpressionAttributeNames[cleanAttributeName(attribute)] = key;
ExpressionAttributeNames[attribute] = key;
if (typeof v === "undefined") {
throw Error("Value must not be undefined in an expression.");
}
Expand Down Expand Up @@ -310,7 +312,9 @@ export function getConditionExpression<T extends {}>(row: T, condExps?: Conditio

if (Array.isArray(condExps)) {
for (const condExp of condExps) {
const { key, op, value, func, logicOp } = condExp;
const { op, value, func, logicOp } = condExp;
let { key } = condExp;
key = cleanAttributeName(key);

let cond;
if (op === "IN") {
Expand All @@ -322,24 +326,24 @@ export function getConditionExpression<T extends {}>(row: T, condExps?: Conditio
})
.join(", ");
cond = `(#${key} IN (${valStr}))`;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = key;
} else if (op === "BETWEEN") {
cond = `(#${key} between :${key}Xaa and :${key}Xbb)`;
expressionAttributeValues[`:${key}Xaa`] = value[0];
expressionAttributeValues[`:${key}Xbb`] = value[1];
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = key;
} else if (typeof op === "undefined" && func) {
if (!op) {
cond = `${func}(#${key})`;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = key;
} else {
cond = `${func}(#${key}) ${op} :${key}Xvv`;
expressionAttributeValues[`:${key}Xvv`] = value;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = key;
}
} else {
cond = `#${key} ${op} :${key}Xvv`;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = key;
expressionAttributeValues[`:${key}Xvv`] = value;
}

Expand All @@ -356,12 +360,12 @@ export function getConditionExpression<T extends {}>(row: T, condExps?: Conditio
const val = row[key];
updateExpressions.push(`#${key} = :${key}`);
expressionAttributeValues[`:${key}`] = val;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = key;
}
}
} else {
for (const key in row) {
if (ExpressionAttributeNames[cleanAttributeName(`#${key}`)]) {
if (ExpressionAttributeNames[`#${key}`]) {
updateExpressions.push(`#${key} = :${key}`);
}
}
Expand Down

0 comments on commit 5ee40d6

Please sign in to comment.