Skip to content

Commit d262968

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

File tree

2 files changed

+64
-57
lines changed

2 files changed

+64
-57
lines changed

lib/datastore/request.js

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,12 @@ DatastoreRequest.prototype.insert = function(entities, callback) {
227227
*
228228
* @borrows {module:datastore/transaction#save} as save
229229
*
230+
* @throws {Error} If an unrecognized method is provided.
231+
*
230232
* @param {object|object[]} entities - Datastore key object(s).
231233
* @param {Key} entities.key - Datastore key object.
232234
* @param {string=} entities.method - Optional method to explicity use for save.
233-
* The choices include 'insert', 'update', 'upsert' and 'auto_insert_id'.
235+
* The choices include 'insert', 'update', 'upsert' and 'insert_auto_id'.
234236
* @param {object|object[]} entities.data - Data to save with the provided key.
235237
* If you provide an array of objects, you must use the explicit syntax:
236238
* `name` for the name of the property and `value` for its value. You may
@@ -382,75 +384,68 @@ DatastoreRequest.prototype.save = function(entities, callback) {
382384

383385
var insertIndexes = [];
384386

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

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

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

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

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-
}
406+
if (is.boolean(data.excludeFromIndexes)) {
407+
var indexed = !data.excludeFromIndexes;
406408

407-
delete data.excludeFromIndexes;
409+
if (is.array(data.value.list_value)) {
410+
data.value.list_value =
411+
data.value.list_value.map(propAssign('indexed', indexed));
412+
} else {
413+
data.value.indexed = indexed;
408414
}
409415

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

416-
ent.key = entity.keyToKeyProto(entityObject.key);
419+
return data;
420+
});
421+
} else {
422+
entityProto = entity.entityToEntityProto(entityObject.data);
423+
}
417424

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 {
425+
entityProto.key = entity.keyToKeyProto(entityObject.key);
426+
427+
if (method) {
428+
if (mutation[method]) {
429+
mutation[method].push(entityProto);
430+
431+
if (method === 'insert_auto_id') {
442432
insertIndexes.push(index);
443-
acc.insert_auto_id.push(ent);
444433
}
434+
} else {
435+
throw new Error('Method ' + method + ' not recognized.');
445436
}
437+
} else {
438+
if (entity.isKeyComplete(entityObject.key)) {
439+
mutation.upsert.push(entityProto);
440+
} else {
441+
insertIndexes.push(index);
442+
mutation.insert_auto_id.push(entityProto);
443+
}
444+
}
445+
});
446446

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

456451
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)