Skip to content

Commit 25ce9bf

Browse files
callmehiphopstephenplusplus
authored andcommitted
spanner: add txn id to reads and remove promise support in runTransaction (#2148)
1 parent 9e7c332 commit 25ce9bf

File tree

6 files changed

+61
-244
lines changed

6 files changed

+61
-244
lines changed

src/database.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -714,30 +714,6 @@ Database.prototype.runStream = function(query, options) {
714714
* transaction.end();
715715
* });
716716
* });
717-
*
718-
* //-
719-
* // If the callback is omitted, we'll return a Promise.
720-
* //-
721-
* database.runTransaction()
722-
* .then(function(data) {
723-
* var transaction = data[0];
724-
*
725-
* // Run a transactional query.
726-
* return transaction.run('SELECT * FROM Singers')
727-
* .then(function() {
728-
* // Queue a mutation (note there is no callback passed to `insert`).
729-
* transaction.insert('Singers', {
730-
* SingerId: 'Id3b',
731-
* Name: 'Joe West'
732-
* });
733-
*
734-
* // Commit the transaction.
735-
* return transaction.commit();
736-
* });
737-
* })
738-
* .then(function() {
739-
* // Transaction committed successfully.
740-
* });
741717
*/
742718
Database.prototype.runTransaction = function(options, runFn) {
743719
if (is.fn(options)) {
@@ -1020,6 +996,7 @@ common.util.promisifyAll(Database, {
1020996
exclude: [
1021997
'delete',
1022998
'getMetadata',
999+
'runTransaction',
10231000
'table',
10241001
'updateSchema',
10251002
'session_'

src/transaction-request.js

Lines changed: 19 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,19 @@ TransactionRequest.formatTimestampOptions_ = function(options) {
192192
* // If you anticipate many results, you can end a stream early to prevent
193193
* // unnecessary processing and API requests.
194194
* //-
195-
* database.runTransaction()
196-
* .then(function(data) {
197-
* var transaction = data[0];
198-
*
199-
* transaction.createReadStream('Singers', {
200-
* keys: ['1'],
201-
* columns: ['SingerId', 'name']
202-
* })
203-
* .on('data', function(row) {
204-
* this.end();
205-
* });
206-
* });
195+
* database.runTransaction(function(err, transaction) {
196+
* if (err) {
197+
* // Error handling omitted.
198+
* }
199+
*
200+
* transaction.createReadStream('Singers', {
201+
* keys: ['1'],
202+
* columns: ['SingerId', 'name']
203+
* })
204+
* .on('data', function(row) {
205+
* this.end();
206+
* });
207+
* });
207208
*/
208209
TransactionRequest.prototype.createReadStream = function(table, query) {
209210
var self = this;
@@ -218,6 +219,12 @@ TransactionRequest.prototype.createReadStream = function(table, query) {
218219
table: table
219220
}, query);
220221

222+
if (this.transaction && this.id) {
223+
reqOpts.transaction = {
224+
id: this.id
225+
};
226+
}
227+
221228
if (query.keys) {
222229
reqOpts.keySet = {
223230
keys: arrify(query.keys).map(function(key) {
@@ -284,26 +291,6 @@ TransactionRequest.prototype.createReadStream = function(table, query) {
284291
* 'Name2'
285292
* ]
286293
* ];
287-
*
288-
* //-
289-
* // If you are using a Promise to retrieve the transaction.
290-
* //-
291-
* database.runTransaction()
292-
* .then(function(data) {
293-
* var transaction = data[0];
294-
*
295-
* // Queue this mutation until later calling `commit`.
296-
* // Note that a callback is not passed to `deleteRows`.
297-
* transaction.deleteRows('Singers', keys);
298-
*
299-
* // Commit the transaction.
300-
* return transaction.commit();
301-
* })
302-
* .then(function(data) {
303-
* var apiResponse = data[0];
304-
*
305-
* // The rows were deleted successfully.
306-
* });
307294
*/
308295
TransactionRequest.prototype.deleteRows = function(table, keys, callback) {
309296
var mutation = {};
@@ -396,26 +383,6 @@ TransactionRequest.prototype.deleteRows = function(table, keys, callback) {
396383
* }
397384
* });
398385
* });
399-
*
400-
* //-
401-
* // If you are using a Promise to retrieve the transaction.
402-
* //-
403-
* database.runTransaction()
404-
* .then(function(data) {
405-
* var transaction = data[0];
406-
*
407-
* // Queue this mutation until later calling `commit`.
408-
* // Note that a callback is not passed to `insert`.
409-
* transaction.insert('Singers', row);
410-
*
411-
* // Commit the transaction.
412-
* return transaction.commit();
413-
* })
414-
* .then(function(data) {
415-
* var apiResponse = data[0];
416-
*
417-
* // The row was inserted successfully.
418-
* });
419386
*/
420387
TransactionRequest.prototype.insert = function(table, keyVals, callback) {
421388
return this.mutate_('insert', table, keyVals, callback);
@@ -516,22 +483,6 @@ TransactionRequest.prototype.insert = function(table, keyVals, callback) {
516483
* transaction.end();
517484
* });
518485
* });
519-
*
520-
* //-
521-
* // If the callback is omitted, we'll return a Promise.
522-
* //-
523-
* database.runTransaction()
524-
* .then(function(data) {
525-
* var transaction = data[0];
526-
*
527-
* return transaction.read('Singers', query)
528-
* .then(function(data) {
529-
* var rows = data[0];
530-
*
531-
* // End the transaction. Note that no callback is provided.
532-
* transaction.end();
533-
* });
534-
* });
535486
*/
536487
TransactionRequest.prototype.read = function(table, keyVals, callback) {
537488
var rows = [];
@@ -581,26 +532,6 @@ TransactionRequest.prototype.read = function(table, keyVals, callback) {
581532
* }
582533
* });
583534
* });
584-
*
585-
* //-
586-
* // If you are using a Promise to retrieve the transaction.
587-
* //-
588-
* database.runTransaction()
589-
* .then(function(data) {
590-
* var transaction = data[0];
591-
*
592-
* // Queue this mutation until later calling `commit`.
593-
* // Note that a callback is not passed to `replace`.
594-
* transaction.replace('Singers', row);
595-
*
596-
* // Commit the transaction.
597-
* return transaction.commit();
598-
* })
599-
* .then(function(data) {
600-
* var apiResponse = data[0];
601-
*
602-
* // The row was replaced successfully.
603-
* });
604535
*/
605536
TransactionRequest.prototype.replace = function(table, keyVals, callback) {
606537
return this.mutate_('replace', table, keyVals, callback);
@@ -659,26 +590,6 @@ TransactionRequest.prototype.requestStream = function() {};
659590
* }
660591
* });
661592
* });
662-
*
663-
* //-
664-
* // If you are using a Promise to retrieve the transaction.
665-
* //-
666-
* database.runTransaction()
667-
* .then(function(data) {
668-
* var transaction = data[0];
669-
*
670-
* // Queue this mutation until later calling `commit`.
671-
* // Note that a callback is not passed to `update`.
672-
* transaction.update('Singers', row);
673-
*
674-
* // Commit the transaction.
675-
* return transaction.commit();
676-
* })
677-
* .then(function(data) {
678-
* var apiResponse = data[0];
679-
*
680-
* // The row was updated successfully.
681-
* });
682593
*/
683594
TransactionRequest.prototype.update = function(table, keyVals, callback) {
684595
return this.mutate_('update', table, keyVals, callback);
@@ -719,26 +630,6 @@ TransactionRequest.prototype.update = function(table, keyVals, callback) {
719630
* }
720631
* });
721632
* });
722-
*
723-
* //-
724-
* // If you are using a Promise to retrieve the transaction.
725-
* //-
726-
* database.runTransaction()
727-
* .then(function(data) {
728-
* var transaction = data[0];
729-
*
730-
* // Queue this mutation until later calling `commit`.
731-
* // Note that a callback is not passed to `upsert`.
732-
* transaction.upsert('Singers', row);
733-
*
734-
* // Commit the transaction.
735-
* return transaction.commit();
736-
* })
737-
* .then(function(data) {
738-
* var apiResponse = data[0];
739-
*
740-
* // The row was updated or inserted successfully.
741-
* });
742633
*/
743634
TransactionRequest.prototype.upsert = function(table, keyVals, callback) {
744635
return this.mutate_('insertOrUpdate', table, keyVals, callback);

src/transaction.js

Lines changed: 11 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -221,27 +221,6 @@ Transaction.prototype.begin = function(callback) {
221221
* }
222222
* });
223223
* });
224-
*
225-
* //-
226-
* // If the callback is omitted, we'll return a Promise.
227-
* //-
228-
* database.runTransaction()
229-
* .then(function(data) {
230-
* var transaction = data[0];
231-
*
232-
* // Queue a mutation (note that there is no callback passed to `insert`).
233-
* transaction.insert('Singers', {
234-
* SingerId: 'Id3b',
235-
* Name: 'Joe West'
236-
* });
237-
*
238-
* return transaction.commit();
239-
* })
240-
* .then(function(data) {
241-
* var apiResponse = data[0];
242-
*
243-
* // Mutations were committed successfully.
244-
* });
245224
*/
246225
Transaction.prototype.commit = function(callback) {
247226
var self = this;
@@ -342,19 +321,6 @@ Transaction.prototype.requestStream = function(config) {
342321
* }
343322
* });
344323
* });
345-
*
346-
* //-
347-
* // If the callback is omitted, we'll return a Promise.
348-
* //-
349-
* database.runTransaction()
350-
* .then(function(data) {
351-
* var transaction = data[0];
352-
*
353-
* return transaction.rollback();
354-
* })
355-
* .then(function() {
356-
* // Transaction rolled back successfully.
357-
* });
358324
*/
359325
Transaction.prototype.rollback = function(callback) {
360326
var self = this;
@@ -440,19 +406,6 @@ Transaction.prototype.rollback = function(callback) {
440406
*
441407
* transaction.run(query, function(err, rows) {});
442408
* });
443-
*
444-
* //-
445-
* // If the callback is omitted, we'll return a Promise.
446-
* //-
447-
* database.runTransaction()
448-
* .then(function(data) {
449-
* var transaction = data[0];
450-
*
451-
* transaction.run(query)
452-
* .then(function(data) {
453-
* var rows = data[0];
454-
* });
455-
* });
456409
*/
457410
Transaction.prototype.run = function(query, callback) {
458411
var rows = [];
@@ -482,7 +435,7 @@ Transaction.prototype.run = function(query, callback) {
482435
* @example
483436
* var query = 'SELECT * FROM Singers';
484437
*
485-
* database.runTransaction(function(err) {
438+
* database.runTransaction(function(err, transaction) {
486439
* if (err) {
487440
* // Error handling omitted.
488441
* }
@@ -504,7 +457,7 @@ Transaction.prototype.run = function(query, callback) {
504457
* // The SQL query string can contain parameter placeholders. A parameter
505458
* // placeholder consists of '@' followed by the parameter name.
506459
* //-
507-
* database.runTransaction(function(err) {
460+
* database.runTransaction(function(err, transaction) {
508461
* if (err) {
509462
* // Error handling omitted.
510463
* }
@@ -526,15 +479,16 @@ Transaction.prototype.run = function(query, callback) {
526479
* // If you anticipate many results, you can end a stream early to prevent
527480
* // unnecessary processing and API requests.
528481
* //-
529-
* database.runTransaction()
530-
* .then(function(data) {
531-
* var transaction = data[0];
482+
* database.runTransaction(function(err, transaction) {
483+
* if (err) {
484+
* // Error handling omitted.
485+
* }
532486
*
533-
* transaction.runStream(query)
534-
* .on('data', function(row) {})
535-
* .on('end', function() {
536-
* // All results retrieved.
537-
* });
487+
* transaction.runStream(query)
488+
* .on('data', function(row) {
489+
* this.end();
490+
* })
491+
* .on('end', function() {});
538492
* });
539493
*/
540494
Transaction.prototype.runStream = function(query) {

0 commit comments

Comments
 (0)