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

fix attr name not sanitized

fix bug where special char in attr expression is not sanitized
  • Loading branch information
frankleng committed Dec 25, 2023
1 parent 97d9717 commit 3dbccdc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 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.21",
"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
37 changes: 21 additions & 16 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) {
if (map.hasOwnProperty(key)) {
for (const k in map) {
if (map.hasOwnProperty(k)) {
const key = cleanAttributeName(k);
const v = map[key];
const attribute = `#${key}`;
const anchor = `:${key}`;
ExpressionAttributeNames[cleanAttributeName(attribute)] = key;
ExpressionAttributeNames[attribute] = k;
if (typeof v === "undefined") {
throw Error("Value must not be undefined in an expression.");
}
Expand Down Expand Up @@ -310,7 +312,8 @@ 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 { key: k, op, value, func, logicOp } = condExp;
const key = cleanAttributeName(k);

let cond;
if (op === "IN") {
Expand All @@ -322,24 +325,24 @@ export function getConditionExpression<T extends {}>(row: T, condExps?: Conditio
})
.join(", ");
cond = `(#${key} IN (${valStr}))`;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = k;
} 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}`] = k;
} else if (typeof op === "undefined" && func) {
if (!op) {
cond = `${func}(#${key})`;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = k;
} else {
cond = `${func}(#${key}) ${op} :${key}Xvv`;
expressionAttributeValues[`:${key}Xvv`] = value;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = k;
}
} else {
cond = `#${key} ${op} :${key}Xvv`;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = k;
expressionAttributeValues[`:${key}Xvv`] = value;
}

Expand All @@ -351,18 +354,20 @@ export function getConditionExpression<T extends {}>(row: T, condExps?: Conditio
}

if (includeAll) {
for (const key in row) {
if (row.hasOwnProperty(key)) {
const val = row[key];
for (const k in row) {
if (row.hasOwnProperty(k)) {
const key = cleanAttributeName(k);
const val = row[k];
updateExpressions.push(`#${key} = :${key}`);
expressionAttributeValues[`:${key}`] = val;
ExpressionAttributeNames[cleanAttributeName(`#${key}`)] = key;
ExpressionAttributeNames[`#${key}`] = k;
}
}
} else {
for (const key in row) {
if (ExpressionAttributeNames[cleanAttributeName(`#${key}`)]) {
updateExpressions.push(`#${key} = :${key}`);
for (const k in row) {
const key = cleanAttributeName(k);
if (ExpressionAttributeNames[`#${key}`]) {
updateExpressions.push(`#${key} = :${k}`);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Query syntax", () => {
UpdateExpression: "SET #yo = :yo",
expressionAttributeValues: { ":idXvv": "123", ":yo": "John" },
},
"NONE",
undefined,
);
});

Expand Down

0 comments on commit 3dbccdc

Please sign in to comment.