Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.transaction.condition now accepts condition instance #830

Merged
merged 4 commits into from Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/docs/api/Model.md
Expand Up @@ -704,11 +704,15 @@ You can pass in the same parameters into each method that you do for the normal

These methods are only meant to only be called to instantiate the `dynamoose.transaction` array.

### Model.transaction.condition(key, additionalParameters)
### Model.transaction.condition(key, condition)

This method allows you to run a conditionCheck when running a DynamoDB transaction.
This method allows you to run a `conditionCheck` when running a DynamoDB transaction.

The `additionalParameters` property will be appended to the result to allow you to set custom conditions.
The `condition` parameter is a `dynamoose.Condition` instance that represents the conditional you want to run.

```js
User.transaction.condition(1, new dynamoose.Condition("age").gt(13));
```

## Model.methods.set(name, function)

Expand Down
6 changes: 3 additions & 3 deletions lib/Model/index.js
Expand Up @@ -95,10 +95,10 @@ class Model {
delete response.ReturnValues;
return response;
}},
{"key": "condition", "settingsIndex": -1, "dynamoKey": "ConditionCheck", "function": (key, options) => ({
...options,
{"key": "condition", "settingsIndex": -1, "dynamoKey": "ConditionCheck", "function": (key, condition) => ({
"Key": this.Document.toDynamo(convertObjectToKey.bind(this)(key)),
"TableName": this.name
"TableName": this.name,
...(condition ? condition.requestObject() : {})
})}
].reduce((accumulator, currentValue) => {
const {key, modifier} = currentValue;
Expand Down
24 changes: 20 additions & 4 deletions test/Model.js
Expand Up @@ -3184,12 +3184,28 @@ describe("Model", () => {
});
});

it("Should return correct result with options", async () => {
expect(await User.transaction.condition(1, {"hello": "world"})).to.eql({
it("Should return correct result for object based key", async () => {
User = dynamoose.model("User", {"id": Number, "name": {"type": String, "rangeKey": true}});
expect(await User.transaction.condition({"id": 1, "name": "Bob"})).to.eql({
"ConditionCheck": {
"Key": {"id": {"N": "1"}, "name": {"S": "Bob"}},
"TableName": "User"
}
});
});

it("Should return correct result with conditional", async () => {
expect(await User.transaction.condition(1, new dynamoose.Condition("age").gt(13))).to.eql({
"ConditionCheck": {
"ConditionExpression": "#a0 > :v0",
"ExpressionAttributeNames": {
"#a0": "age"
},
"ExpressionAttributeValues": {
":v0": {"N": "13"}
},
"Key": {"id": {"N": "1"}},
"TableName": "User",
"hello": "world"
"TableName": "User"
}
});
});
Expand Down