Skip to content

Commit

Permalink
datastore(transaction): do not squash key-incomplete mutations (#2657)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus authored Oct 10, 2017
1 parent 114bc32 commit 6006fd8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/datastore/src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ var flatten = require('lodash.flatten');
var prop = require('propprop');
var util = require('util');

/**
* @type {module:datastore/entity}
* @private
*/
var entity = require('./entity.js');

/**
* @type {module:datastore/request}
* @private
Expand Down Expand Up @@ -129,10 +135,16 @@ Transaction.prototype.commit = function(callback) {
// key they just asked to be deleted, the delete request will be ignored,
// giving preference to the save operation.
.filter(function(modifiedEntity) {
var key = JSON.stringify(modifiedEntity.entity.key);
var key = modifiedEntity.entity.key;

if (!entity.isKeyComplete(key)) {
return true;
}

var stringifiedKey = JSON.stringify(modifiedEntity.entity.key);

if (!keys[key]) {
keys[key] = true;
if (!keys[stringifiedKey]) {
keys[stringifiedKey] = true;
return true;
}
})
Expand Down
14 changes: 14 additions & 0 deletions packages/datastore/test/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ describe('Transaction', function() {
assert.equal(saveCalled, 1);
});

it('should not squash key-incomplete mutations', function(done) {
transaction.save({ key: key(['Product']), data: '' });
transaction.save({ key: key(['Product']), data: '' });

DatastoreRequestOverride.save = function(entities) {
assert.strictEqual(entities.length, 2);
done();
};

transaction.request_ = util.noop;

transaction.commit();
});

it('should send the built request object', function(done) {
transaction.requests_ = [
{
Expand Down

0 comments on commit 6006fd8

Please sign in to comment.