@@ -230,7 +230,7 @@ DatastoreRequest.prototype.insert = function(entities, callback) {
230
230
* @param {object|object[] } entities - Datastore key object(s).
231
231
* @param {Key } entities.key - Datastore key object.
232
232
* @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 '.
234
234
* @param {object|object[] } entities.data - Data to save with the provided key.
235
235
* If you provide an array of objects, you must use the explicit syntax:
236
236
* `name` for the name of the property and `value` for its value. You may
@@ -382,75 +382,68 @@ DatastoreRequest.prototype.save = function(entities, callback) {
382
382
383
383
var insertIndexes = [ ] ;
384
384
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
+ } ;
388
391
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 ) ;
392
396
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 ;
396
399
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 ) ;
399
403
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 ;
406
406
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 ;
408
412
}
409
413
410
- return data ;
411
- } ) ;
412
- } else {
413
- ent = entity . entityToEntityProto ( entityObject . data ) ;
414
- }
414
+ delete data . excludeFromIndexes ;
415
+ }
415
416
416
- ent . key = entity . keyToKeyProto ( entityObject . key ) ;
417
+ return data ;
418
+ } ) ;
419
+ } else {
420
+ entityProto = entity . entityToEntityProto ( entityObject . data ) ;
421
+ }
417
422
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' ) {
442
430
insertIndexes . push ( index ) ;
443
- acc . insert_auto_id . push ( ent ) ;
444
431
}
432
+ } else {
433
+ throw new Error ( 'Method ' + method + ' not recognized.' ) ;
445
434
}
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
+ } ) ;
446
444
447
- return acc ;
448
- } , {
449
- insert : [ ] ,
450
- update : [ ] ,
451
- upsert : [ ] ,
452
- insert_auto_id : [ ]
453
- } )
445
+ var req = {
446
+ mutation : mutation
454
447
} ;
455
448
456
449
if ( this . id ) {
0 commit comments