Skip to content

Commit 3b538b2

Browse files
datastore: throw if an unrecognized commit method is given
1 parent fbbe1ab commit 3b538b2

File tree

2 files changed

+62
-57
lines changed

2 files changed

+62
-57
lines changed

lib/datastore/request.js

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ DatastoreRequest.prototype.insert = function(entities, callback) {
230230
* @param {object|object[]} entities - Datastore key object(s).
231231
* @param {Key} entities.key - Datastore key object.
232232
* @param {string=} entities.method - Optional method to explicity use for save.
233-
* The choices include 'insert', 'update', 'upsert' and 'auto_insert_id'.
233+
* The choices include 'insert', 'update', 'upsert' and 'insert_auto_id'.
234234
* @param {object|object[]} entities.data - Data to save with the provided key.
235235
* If you provide an array of objects, you must use the explicit syntax:
236236
* `name` for the name of the property and `value` for its value. You may
@@ -382,75 +382,68 @@ DatastoreRequest.prototype.save = function(entities, callback) {
382382

383383
var insertIndexes = [];
384384

385-
var req = {
386-
mutation: entities.reduce(function(acc, entityObject, index) {
387-
entityObject = extend(true, {}, entityObject);
385+
var mutation = {
386+
insert: [],
387+
update: [],
388+
upsert: [],
389+
insert_auto_id: []
390+
};
388391

389-
var ent = {};
390-
var method = entityObject.method;
391-
delete entityObject.method;
392+
// Iterate over the entity objects, build a proto from all keys and values,
393+
// then place in the correct mutation array (insert, update, etc).
394+
entities.forEach(function(entityObject, index) {
395+
entityObject = extend(true, {}, entityObject);
392396

393-
if (Array.isArray(entityObject.data)) {
394-
ent.property = entityObject.data.map(function(data) {
395-
data.value = entity.valueToProperty(data.value);
397+
var entityProto = {};
398+
var method = entityObject.method;
396399

397-
if (is.boolean(data.excludeFromIndexes)) {
398-
var indexed = !data.excludeFromIndexes;
400+
if (is.array(entityObject.data)) {
401+
entityProto.property = entityObject.data.map(function(data) {
402+
data.value = entity.valueToProperty(data.value);
399403

400-
if (is.array(data.value.list_value)) {
401-
data.value.list_value =
402-
data.value.list_value.map(propAssign('indexed', indexed));
403-
} else {
404-
data.value.indexed = indexed;
405-
}
404+
if (is.boolean(data.excludeFromIndexes)) {
405+
var indexed = !data.excludeFromIndexes;
406406

407-
delete data.excludeFromIndexes;
407+
if (is.array(data.value.list_value)) {
408+
data.value.list_value =
409+
data.value.list_value.map(propAssign('indexed', indexed));
410+
} else {
411+
data.value.indexed = indexed;
408412
}
409413

410-
return data;
411-
});
412-
} else {
413-
ent = entity.entityToEntityProto(entityObject.data);
414-
}
414+
delete data.excludeFromIndexes;
415+
}
415416

416-
ent.key = entity.keyToKeyProto(entityObject.key);
417+
return data;
418+
});
419+
} else {
420+
entityProto = entity.entityToEntityProto(entityObject.data);
421+
}
417422

418-
if (method) {
419-
switch (method) {
420-
case 'insert': {
421-
acc.insert.push(ent);
422-
break;
423-
}
424-
case 'update': {
425-
acc.update.push(ent);
426-
break;
427-
}
428-
case 'upsert': {
429-
acc.upsert.push(ent);
430-
break;
431-
}
432-
case 'insert_auto_id': {
433-
insertIndexes.push(index);
434-
acc.insert_auto_id.push(ent);
435-
break;
436-
}
437-
}
438-
} else {
439-
if (entity.isKeyComplete(entityObject.key)) {
440-
acc.upsert.push(ent);
441-
} else {
423+
entityProto.key = entity.keyToKeyProto(entityObject.key);
424+
425+
if (method) {
426+
if (mutation[method]) {
427+
mutation[method].push(entityProto);
428+
429+
if (method === 'insert_auto_id') {
442430
insertIndexes.push(index);
443-
acc.insert_auto_id.push(ent);
444431
}
432+
} else {
433+
throw new Error('Method ' + method + ' not recognized.');
445434
}
435+
} else {
436+
if (entity.isKeyComplete(entityObject.key)) {
437+
mutation.upsert.push(entityProto);
438+
} else {
439+
insertIndexes.push(index);
440+
mutation.insert_auto_id.push(entityProto);
441+
}
442+
}
443+
});
446444

447-
return acc;
448-
}, {
449-
insert: [],
450-
update: [],
451-
upsert: [],
452-
insert_auto_id: []
453-
})
445+
var req = {
446+
mutation: mutation
454447
};
455448

456449
if (this.id) {

test/datastore/request.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,18 @@ describe('Request', function() {
455455
], done);
456456
});
457457

458+
it('should throw if a given method is not recognized', function() {
459+
assert.throws(function() {
460+
request.save({
461+
key: key,
462+
method: 'auto_insert_id',
463+
data: {
464+
k: 'v'
465+
}
466+
}, assert.ifError);
467+
}, /Method auto_insert_id not recognized/);
468+
});
469+
458470
it('should not alter the provided data object', function(done) {
459471
var entities = [
460472
{

0 commit comments

Comments
 (0)