Skip to content

Commit 45bbb6f

Browse files
committed
spanner: make getTransaction public
1 parent 748816f commit 45bbb6f

4 files changed

Lines changed: 84 additions & 74 deletions

File tree

packages/spanner/src/database.js

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,64 @@ Database.prototype.getSchema = function(callback) {
399399
});
400400
};
401401

402+
/**
403+
* Get a read/write ready Transaction object.
404+
*
405+
* @param {object=} options - Transaction options.
406+
* @param {boolean} options.readOnly - Specifies if the transaction is read
407+
* only.
408+
* @param {function} callback - The callback function.
409+
* @param {?error} callback.err - An error returned while getting the
410+
* transaction object.
411+
* @param {module:spanner/transaction} transaction - The transaction object.
412+
*
413+
* @example
414+
* database.getTransaction(function(err, transaction) {});
415+
*
416+
* //-
417+
* // If the callback is omitted, we'll return a Promise.
418+
* //-
419+
* database.getTransaction().then(function(data) {
420+
* var transaction = data[0];
421+
* });
422+
*/
423+
Database.prototype.getTransaction = function(options, callback) {
424+
var self = this;
425+
426+
if (is.fn(options)) {
427+
callback = options;
428+
options = null;
429+
}
430+
431+
if (!options || !options.readOnly) {
432+
this.pool_.getWriteSession(function(err, session, transaction) {
433+
callback(err, transaction);
434+
});
435+
return;
436+
}
437+
438+
this.pool_.getSession(function(err, session) {
439+
if (err) {
440+
callback(err);
441+
return;
442+
}
443+
444+
options = extend({}, options);
445+
delete options.readOnly;
446+
447+
var transaction = self.pool_.createTransaction_(session, options);
448+
449+
transaction.begin(function(err) {
450+
if (err) {
451+
callback(err);
452+
return;
453+
}
454+
455+
callback(null, transaction);
456+
});
457+
});
458+
};
459+
402460
/**
403461
* Execute a SQL statement on this database.
404462
*
@@ -811,7 +869,7 @@ Database.prototype.runTransaction = function(options, runFn) {
811869

812870
options = extend({}, options);
813871

814-
this.getTransaction_(options, function(err, transaction) {
872+
this.getTransaction(options, function(err, transaction) {
815873
if (err) {
816874
runFn(err);
817875
return;
@@ -1004,66 +1062,6 @@ Database.prototype.getSession_ = function(callback) {
10041062
this.pool_.getSession(callback);
10051063
};
10061064

1007-
/**
1008-
* Get a read/write ready Transaction object.
1009-
*
1010-
* @private
1011-
*
1012-
* @param {object=} options - Transaction options.
1013-
* @param {boolean} options.readOnly - Specifies if the transaction is read
1014-
* only.
1015-
* @param {function} callback - The callback function.
1016-
* @param {?error} callback.err - An error returned while getting the
1017-
* transaction object.
1018-
* @param {module:spanner/transaction} transaction - The transaction object.
1019-
*
1020-
* @example
1021-
* database.getTransaction(function(err, transaction) {});
1022-
*
1023-
* //-
1024-
* // If the callback is omitted, we'll return a Promise.
1025-
* //-
1026-
* database.getTransaction().then(function(data) {
1027-
* var transaction = data[0];
1028-
* });
1029-
*/
1030-
Database.prototype.getTransaction_ = function(options, callback) {
1031-
var self = this;
1032-
1033-
if (is.fn(options)) {
1034-
callback = options;
1035-
options = null;
1036-
}
1037-
1038-
if (!options || !options.readOnly) {
1039-
this.pool_.getWriteSession(function(err, session, transaction) {
1040-
callback(err, transaction);
1041-
});
1042-
return;
1043-
}
1044-
1045-
this.pool_.getSession(function(err, session) {
1046-
if (err) {
1047-
callback(err);
1048-
return;
1049-
}
1050-
1051-
options = extend({}, options);
1052-
delete options.readOnly;
1053-
1054-
var transaction = self.pool_.createTransaction_(session, options);
1055-
1056-
transaction.begin(function(err) {
1057-
if (err) {
1058-
callback(err);
1059-
return;
1060-
}
1061-
1062-
callback(null, transaction);
1063-
});
1064-
});
1065-
};
1066-
10671065
/**
10681066
* Create a Session object.
10691067
*

packages/spanner/src/transaction.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Transaction.prototype.request = function(config, callback) {
325325
delete reqOpts.gaxOptions;
326326

327327
config.method(reqOpts, gaxOptions, function(err, resp) {
328-
if (!err || err.code !== ABORTED) {
328+
if (!self.runFn_ || !err || err.code !== ABORTED) {
329329
callback(err, resp);
330330
return;
331331
}
@@ -357,7 +357,6 @@ Transaction.prototype.requestStream = function(config) {
357357
var gaxOptions = reqOpts.gaxOptions;
358358
delete reqOpts.gaxOptions;
359359

360-
// return config.method(reqOpts, gaxOptions);
361360
var requestStream = config.method(reqOpts, gaxOptions);
362361

363362
if (!is.fn(self.runFn_)) {

packages/spanner/test/database.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ describe('Database', function() {
798798

799799
describe('runTransaction', function() {
800800
it('should get a Transaction object', function(done) {
801-
database.getTransaction_ = function() {
801+
database.getTransaction = function() {
802802
done();
803803
};
804804

@@ -808,7 +808,7 @@ describe('Database', function() {
808808
it('should execute callback with error', function(done) {
809809
var error = new Error('Error.');
810810

811-
database.getTransaction_ = function(options, callback) {
811+
database.getTransaction = function(options, callback) {
812812
callback(error);
813813
};
814814

@@ -831,7 +831,7 @@ describe('Database', function() {
831831
return fakeDate;
832832
};
833833

834-
database.getTransaction_ = function(options, callback) {
834+
database.getTransaction = function(options, callback) {
835835
assert.deepEqual(options, OPTIONS);
836836
callback(null, TRANSACTION);
837837
};
@@ -855,7 +855,7 @@ describe('Database', function() {
855855
timeout: 1000
856856
};
857857

858-
database.getTransaction_ = function(options, callback) {
858+
database.getTransaction = function(options, callback) {
859859
callback(null, TRANSACTION);
860860
};
861861

@@ -1045,7 +1045,7 @@ describe('Database', function() {
10451045
});
10461046
});
10471047

1048-
describe('getTransaction_', function() {
1048+
describe('getTransaction', function() {
10491049
describe('write mode', function() {
10501050
it('should get a session from the pool', function(done) {
10511051
var error = new Error('Error.');
@@ -1058,7 +1058,7 @@ describe('Database', function() {
10581058
}
10591059
};
10601060

1061-
database.getTransaction_(function(err, transaction_) {
1061+
database.getTransaction(function(err, transaction_) {
10621062
assert.strictEqual(err, error);
10631063
assert.strictEqual(transaction_, transaction);
10641064
done();
@@ -1079,7 +1079,7 @@ describe('Database', function() {
10791079
}
10801080
};
10811081

1082-
database.getTransaction_(OPTIONS, assert.ifError);
1082+
database.getTransaction(OPTIONS, assert.ifError);
10831083
});
10841084

10851085
it('should return an error if could not get session', function(done) {
@@ -1091,7 +1091,7 @@ describe('Database', function() {
10911091
}
10921092
};
10931093

1094-
database.getTransaction_(OPTIONS, function(err) {
1094+
database.getTransaction(OPTIONS, function(err) {
10951095
assert.strictEqual(err, error);
10961096
done();
10971097
});
@@ -1121,7 +1121,7 @@ describe('Database', function() {
11211121
}
11221122
};
11231123

1124-
database.getTransaction_(OPTIONS, assert.ifError);
1124+
database.getTransaction(OPTIONS, assert.ifError);
11251125
});
11261126

11271127
it('should begin a transaction', function(done) {
@@ -1141,7 +1141,7 @@ describe('Database', function() {
11411141
}
11421142
};
11431143

1144-
database.getTransaction_(OPTIONS, function(err, transaction) {
1144+
database.getTransaction(OPTIONS, function(err, transaction) {
11451145
assert.ifError(err);
11461146
assert.strictEqual(transaction, TRANSACTION);
11471147
done();
@@ -1167,7 +1167,7 @@ describe('Database', function() {
11671167
}
11681168
};
11691169

1170-
database.getTransaction_(OPTIONS, function(err) {
1170+
database.getTransaction(OPTIONS, function(err) {
11711171
assert.strictEqual(err, error);
11721172
done();
11731173
});

packages/spanner/test/transaction.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ describe('Transaction', function() {
485485
return true;
486486
};
487487

488+
transaction.runFn_ = function() {
489+
done(new Error('Should not have been called.'));
490+
};
491+
488492
transaction.request(config, function() {
489493
done(new Error('Should not have been called.'));
490494
});
@@ -519,6 +523,15 @@ describe('Transaction', function() {
519523
done(new Error('Should not have been called.'));
520524
});
521525
});
526+
527+
it('should return the aborted error if no runFn', function(done) {
528+
transaction.runFn_ = null;
529+
530+
transaction.request(config, function(err) {
531+
assert.strictEqual(err, abortedError);
532+
done();
533+
});
534+
});
522535
});
523536
});
524537

0 commit comments

Comments
 (0)