-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from valentinbeggi/create-toExistInDynamoTable…
…-assertion feat: create toExistInDynamoTable assertion
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { AWSClient } from "../../helpers/general"; | ||
|
||
export default { | ||
async toExistInDynamoTable({ PK, SK }, tableName) { | ||
const docClient = new AWSClient.DynamoDB.DocumentClient(); | ||
|
||
if (!docClient) { | ||
return { | ||
message: () => "expected table to contain document client", | ||
pass: false, | ||
}; | ||
} | ||
|
||
if (!SK) { | ||
const queryParams = { | ||
TableName: tableName, | ||
KeyConditionExpression: "#pk = :pk", | ||
ExpressionAttributeNames: { | ||
"#pk": "PK", | ||
}, | ||
ExpressionAttributeValues: { | ||
":pk": PK, | ||
}, | ||
Limit: 1, | ||
}; | ||
|
||
const result = await docClient.query(queryParams).promise(); | ||
return { | ||
message: () => `expected to find ${PK} in ${tableName}`, | ||
pass: result.Count === 1, | ||
}; | ||
} | ||
|
||
const getParams = { | ||
TableName: tableName, | ||
Key: { | ||
PK, | ||
SK, | ||
}, | ||
}; | ||
const result = await docClient.get(getParams).promise(); | ||
return { | ||
message: () => `expected to find ${PK} in ${tableName}`, | ||
pass: result.Item !== undefined, | ||
}; | ||
}, | ||
}; |