Skip to content

Commit 6006fd8

Browse files
datastore(transaction): do not squash key-incomplete mutations (#2657)
1 parent 114bc32 commit 6006fd8

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

packages/datastore/src/transaction.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ var flatten = require('lodash.flatten');
2626
var prop = require('propprop');
2727
var util = require('util');
2828

29+
/**
30+
* @type {module:datastore/entity}
31+
* @private
32+
*/
33+
var entity = require('./entity.js');
34+
2935
/**
3036
* @type {module:datastore/request}
3137
* @private
@@ -129,10 +135,16 @@ Transaction.prototype.commit = function(callback) {
129135
// key they just asked to be deleted, the delete request will be ignored,
130136
// giving preference to the save operation.
131137
.filter(function(modifiedEntity) {
132-
var key = JSON.stringify(modifiedEntity.entity.key);
138+
var key = modifiedEntity.entity.key;
139+
140+
if (!entity.isKeyComplete(key)) {
141+
return true;
142+
}
143+
144+
var stringifiedKey = JSON.stringify(modifiedEntity.entity.key);
133145

134-
if (!keys[key]) {
135-
keys[key] = true;
146+
if (!keys[stringifiedKey]) {
147+
keys[stringifiedKey] = true;
136148
return true;
137149
}
138150
})

packages/datastore/test/transaction.js

+14
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,20 @@ describe('Transaction', function() {
261261
assert.equal(saveCalled, 1);
262262
});
263263

264+
it('should not squash key-incomplete mutations', function(done) {
265+
transaction.save({ key: key(['Product']), data: '' });
266+
transaction.save({ key: key(['Product']), data: '' });
267+
268+
DatastoreRequestOverride.save = function(entities) {
269+
assert.strictEqual(entities.length, 2);
270+
done();
271+
};
272+
273+
transaction.request_ = util.noop;
274+
275+
transaction.commit();
276+
});
277+
264278
it('should send the built request object', function(done) {
265279
transaction.requests_ = [
266280
{

0 commit comments

Comments
 (0)