Skip to content

Commit

Permalink
Merge pull request #533 from dobrynin/return-values
Browse files Browse the repository at this point in the history
defaultReturnValues
  • Loading branch information
fishcharlie committed Jan 12, 2019
2 parents 106e29f + 760a963 commit 830e3b2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/_docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Default `options`:
type: undefined // sets the stream type (NEW_IMAGE | OLD_IMAGE | NEW_AND_OLD_IMAGES | KEYS_ONLY) (https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_StreamSpecification.html#DDB-Type-StreamSpecification-StreamViewType)
},
serverSideEncryption: false // Set SSESpecification.Enabled (server-side encryption) to true or false (default: true)
defaultReturnValues: 'ALL_NEW' // sets ReturnValues for the UpdateItem operation (NONE | ALL_OLD | UPDATED_OLD | ALL_NEW | UPDATED_NEW) (https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html)
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/_docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,10 @@ match schema attribute names.
From [the AWS documentation](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html)
Use ReturnValues if you want to get the item attributes as they appear before or after they are updated. For UpdateItem, the valid values are:

- NONE - If ReturnValues is not specified, or if its value is NONE, then nothing is returned. (This setting is the default for ReturnValues.)
- NONE - If ReturnValues is not specified, or if its value is NONE, then nothing is returned. (This setting is DynamoDB's default.)
- ALL_OLD - Returns all of the attributes of the item, as they appeared before the UpdateItem operation.
- UPDATED_OLD - Returns only the updated attributes, as they appeared before the UpdateItem operation.
- ALL_NEW - Returns all of the attributes of the item, as they appear after the UpdateItem operation.
- ALL_NEW - Returns all of the attributes of the item, as they appear after the UpdateItem operation. (This setting is Dynamoose's default.)
- UPDATED_NEW - Returns only the updated attributes, as they appear after the UpdateItem operation.


Expand Down
4 changes: 2 additions & 2 deletions lib/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,9 @@ Model.update = async function(NewModel, key, update, options, next) {
options.updateTimestamps = true;
}

// default return values to 'NEW_ALL'
// default return values to 'ALL_NEW' unless `defaultReturnValues` is set;
if (typeof options.returnValues === 'undefined') {
options.returnValues = 'ALL_NEW';
options.returnValues = NewModel.$__.options.defaultReturnValues || 'ALL_NEW';
}

// if the key part was emtpy, try the key defaults before giving up...
Expand Down
8 changes: 8 additions & 0 deletions test/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,14 @@ describe('Model', function (){
});
});

it("Update respects global defaultReturnValues option", function () {
return Cats.ReturnValuesNoneCat.create({id: '678', name: 'Oliver'}, {overwrite: true}).then(function(old){
return Cats.ReturnValuesNoneCat.update({id: old.id}, {name: 'Tom'}).then(function(data){
should.not.exist(data);
});
});
});

it("Update returns updated new values using 'UPDATED_NEW'", function () {
return Cats.Cat.create({id: '678', name: 'Oliver'}, {overwrite: true}).then(function(old){
return Cats.Cat.update({id: old.id}, {name: 'Tom'}, {returnValues: 'UPDATED_NEW'}).then(function(data){
Expand Down
10 changes: 9 additions & 1 deletion test/fixtures/Cats.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ module.exports = function(dynamoose){
});
var CatWithMethods = dynamoose.model('CatWithMethods', CatWithMethodsSchema);

const ReturnValuesNoneCat = dynamoose.model('ReturnValuesNoneCat', {
id: Number,
name: String,
},{
defaultReturnValues: 'NONE',
})

return {
Cat: Cat,
Cat1: Cat1,
Expand All @@ -423,6 +430,7 @@ module.exports = function(dynamoose){
ExpiringCatReturnTrue: ExpiringCatReturnTrue,
CatWithGeneratedID: CatWithGeneratedID,
CatWithMethods: CatWithMethods,
CatModel: CatModel
CatModel: CatModel,
ReturnValuesNoneCat,
};
};

0 comments on commit 830e3b2

Please sign in to comment.