@@ -106,14 +106,10 @@ DatastoreRequest.prototype.get = function(keys, callback) {
106106 var isMultipleRequest = Array . isArray ( keys ) ;
107107 keys = isMultipleRequest ? keys : [ keys ] ;
108108 callback = callback || util . noop ;
109- var req = new pb . LookupRequest ( {
109+ var req = {
110110 key : keys . map ( entity . keyToKeyProto )
111- } ) ;
112- var res = pb . LookupResponse ;
113- if ( this . id ) {
114- req . transaction = this . id ;
115- }
116- this . createRequest_ ( 'lookup' , req , res , function ( err , resp ) {
111+ } ;
112+ this . createRequest_ ( 'lookup' , req , function ( err , resp ) {
117113 if ( err ) {
118114 callback ( err ) ;
119115 return ;
@@ -217,7 +213,6 @@ DatastoreRequest.prototype.save = function(entities, callback) {
217213 return entityObject . key ;
218214 } ) ;
219215 var req = {
220- mode : MODE_NON_TRANSACTIONAL ,
221216 mutation : entities . reduce ( function ( acc , entityObject , index ) {
222217 var ent = { } ;
223218 if ( Array . isArray ( entityObject . data ) ) {
@@ -242,13 +237,7 @@ DatastoreRequest.prototype.save = function(entities, callback) {
242237 return acc ;
243238 } . bind ( this ) , { upsert : [ ] , insert_auto_id : [ ] } )
244239 } ;
245- if ( this . id ) {
246- req . transaction = this . id ;
247- req . mode = MODE_TRANSACTIONAL ;
248- }
249- req = new pb . CommitRequest ( req ) ;
250- var res = pb . CommitResponse ;
251- this . createRequest_ ( 'commit' , req , res , function ( err , resp ) {
240+ this . createRequest_ ( 'commit' , req , function ( err , resp ) {
252241 if ( err || ! resp ) {
253242 callback ( err ) ;
254243 return ;
@@ -290,20 +279,12 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
290279 var isMultipleRequest = Array . isArray ( keys ) ;
291280 keys = isMultipleRequest ? keys : [ keys ] ;
292281 callback = callback || util . noop ;
293-
294282 var req = {
295- mode : MODE_NON_TRANSACTIONAL ,
296283 mutation : {
297284 delete : keys . map ( entity . keyToKeyProto )
298285 }
299286 } ;
300- if ( this . id ) {
301- req . transaction = this . id ;
302- req . mode = MODE_TRANSACTIONAL ;
303- }
304- req = new pb . CommitRequest ( req ) ;
305- var res = pb . CommitResponse ;
306- this . createRequest_ ( 'commit' , req , res , function ( err ) {
287+ this . createRequest_ ( 'commit' , req , function ( err ) {
307288 if ( ! err && this . id ) {
308289 this . isFinalized = true ;
309290 }
@@ -334,28 +315,20 @@ DatastoreRequest.prototype.delete = function(keys, callback) {
334315 * }
335316 * });
336317 */
337-
338318DatastoreRequest . prototype . runQuery = function ( q , callback ) {
339319 callback = callback || util . noop ;
340320 var req = {
341321 read_options : { } ,
342322 query : entity . queryToQueryProto ( q )
343323 } ;
344324
345- if ( this . id ) {
346- req . read_options . transaction = this . id ;
347- }
348-
349325 if ( q . namespace ) {
350326 req . partition_id = {
351327 namespace : q . namespace
352328 } ;
353329 }
354330
355- req = new pb . RunQueryRequest ( req ) ;
356- var res = pb . RunQueryResponse ;
357-
358- this . createRequest_ ( 'runQuery' , req , res , function ( err , resp ) {
331+ this . createRequest_ ( 'runQuery' , req , function ( err , resp ) {
359332 if ( err || ! resp . batch || ! resp . batch . entity_result ) {
360333 callback ( err ) ;
361334 return ;
@@ -406,11 +379,10 @@ DatastoreRequest.prototype.allocateIds = function(incompleteKey, n, callback) {
406379 for ( var i = 0 ; i < n ; i ++ ) {
407380 incompleteKeys . push ( entity . keyToKeyProto ( incompleteKey ) ) ;
408381 }
409-
410- this . createRequest_ (
411- 'allocateIds' ,
412- new pb . AllocateIdsRequest ( { key : incompleteKeys } ) ,
413- pb . AllocateIdsResponse , function ( err , resp ) {
382+ var req = {
383+ key : incompleteKeys
384+ } ;
385+ this . createRequest_ ( 'allocateIds' , req , function ( err , resp ) {
414386 if ( err ) {
415387 callback ( err ) ;
416388 return ;
@@ -424,28 +396,50 @@ DatastoreRequest.prototype.allocateIds = function(incompleteKey, n, callback) {
424396} ;
425397
426398/**
427- * Make a request to the API endpoint.
399+ * Make a request to the API endpoint. Properties to indicate a transactional or
400+ * non-transactional operation are added automatically.
428401 *
429- * @param {string } method - Transaction action (allocateIds, commit, etc.).
430- * @param {object } req - Request configuration object.
431- * @param {object } respType - Response type.
432- * @param {function } cb - The callback function.
402+ * @param {string } method - Datastore action (allocateIds, commit, etc.).
403+ * @param {object= } req - Request configuration object.
404+ * @param {function } callback - The callback function.
433405 *
434406 * @private
435407 *
436408 * @example
437409 * var deleteRequest = {
438- * MODE: 'NON_TRANSACTIONAL',
439410 * mutation: {
440411 * delete: [] // datastore key objects.
441412 * }
442413 * };
443414 * transaction.makeReq('commit', deleteRequest, function(err) {});
444415 */
445- DatastoreRequest . prototype . createRequest_ =
446- function ( method , req , respType , cb ) {
416+ DatastoreRequest . prototype . createRequest_ = function ( method , req , callback ) {
447417 // TODO: Handle non-HTTP 200 cases.
448- cb = cb || util . noop ;
418+ if ( ! callback ) {
419+ callback = req ;
420+ req = { } ;
421+ }
422+ callback = callback || util . noop ;
423+
424+ // Set properties to indicate if we're in a transaction or not.
425+ if ( method === 'commit' ) {
426+ if ( this . id ) {
427+ req . mode = MODE_TRANSACTIONAL ;
428+ req . transaction = this . id ;
429+ } else {
430+ req . mode = MODE_NON_TRANSACTIONAL ;
431+ }
432+ }
433+
434+ if ( method === 'lookup' && this . id ) {
435+ req . read_options = req . read_options || { } ;
436+ req . read_options . transaction = this . id ;
437+ }
438+
439+ var pbKey = method [ 0 ] . toUpperCase ( ) + method . substr ( 1 ) ;
440+ var pbRequest = new pb [ pbKey + 'Request' ] ( req ) . toBuffer ( ) ;
441+ var pbResponse = pb [ pbKey + 'Response' ] ;
442+
449443 this . connection . createAuthorizedReq ( {
450444 method : 'POST' ,
451445 host : GOOGLE_APIS_HOST ,
@@ -455,7 +449,7 @@ DatastoreRequest.prototype.createRequest_ =
455449 }
456450 } , function ( err , request ) {
457451 if ( err ) {
458- cb ( err ) ;
452+ callback ( err ) ;
459453 return ;
460454 }
461455 var remoteStream = https . request ( request , function ( resp ) {
@@ -466,15 +460,15 @@ DatastoreRequest.prototype.createRequest_ =
466460 resp . on ( 'end' , function ( ) {
467461 util . handleResp ( null , resp , buffer . toString ( ) , function ( err ) {
468462 if ( err ) {
469- cb ( err ) ;
463+ callback ( err ) ;
470464 return ;
471465 }
472- cb ( null , respType . decode ( buffer ) ) ;
466+ callback ( null , pbResponse . decode ( buffer ) ) ;
473467 } ) ;
474468 } ) ;
475469 } ) ;
476- remoteStream . on ( 'error' , cb ) ;
477- remoteStream . write ( req . toBuffer ( ) ) ;
470+ remoteStream . on ( 'error' , callback ) ;
471+ remoteStream . write ( pbRequest ) ;
478472 remoteStream . end ( ) ;
479473 } ) ;
480474} ;
0 commit comments