diff --git a/packages/datastore/src/transaction.js b/packages/datastore/src/transaction.js index c28bbd0f56c..3a90c1c7f11 100644 --- a/packages/datastore/src/transaction.js +++ b/packages/datastore/src/transaction.js @@ -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 @@ -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; } }) diff --git a/packages/datastore/test/transaction.js b/packages/datastore/test/transaction.js index 263d54132f7..123ee58fd8e 100644 --- a/packages/datastore/test/transaction.js +++ b/packages/datastore/test/transaction.js @@ -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_ = [ {