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