@@ -15,7 +15,9 @@ var parse = require('./url_parser'),
15
15
f = require ( 'util' ) . format ,
16
16
assign = require ( './utils' ) . assign ,
17
17
shallowClone = require ( './utils' ) . shallowClone ,
18
- authenticate = require ( './authenticate' ) ;
18
+ authenticate = require ( './authenticate' ) ,
19
+ ServerSessionPool = require ( 'mongodb-core' ) . Sessions . ServerSessionPool ,
20
+ ClientSession = require ( 'mongodb-core' ) . Sessions . ClientSession ;
19
21
20
22
/**
21
23
* @fileOverview The **MongoClient** class is a class that allows for making Connections to MongoDB.
@@ -180,7 +182,8 @@ function MongoClient(url, options) {
180
182
url : url ,
181
183
options : options || { } ,
182
184
promiseLibrary : null ,
183
- dbCache : { }
185
+ dbCache : { } ,
186
+ sessionPool : null
184
187
} ;
185
188
186
189
// Get the promiseLibrary
@@ -228,9 +231,8 @@ MongoClient.prototype.connect = function(callback) {
228
231
// Did we have a validation error
229
232
if ( err ) return reject ( err ) ;
230
233
// Attempt to connect
231
- connect ( self , self . s . url , self . s . options , function ( err , topology ) {
234
+ connect ( self , self . s . url , self . s . options , function ( err ) {
232
235
if ( err ) return reject ( err ) ;
233
- self . topology = topology ;
234
236
resolve ( self ) ;
235
237
} ) ;
236
238
} ) ;
@@ -239,9 +241,8 @@ MongoClient.prototype.connect = function(callback) {
239
241
// Did we have a validation error
240
242
if ( err ) return callback ( err ) ;
241
243
// Fallback to callback based connect
242
- connect ( self , self . s . url , self . s . options , function ( err , topology ) {
244
+ connect ( self , self . s . url , self . s . options , function ( err ) {
243
245
if ( err ) return callback ( err ) ;
244
- self . topology = topology ;
245
246
callback ( null , self ) ;
246
247
} ) ;
247
248
} ;
@@ -454,6 +455,28 @@ MongoClient.connect = function(url, options, callback) {
454
455
455
456
define . staticMethod ( 'connect' , { callback : true , promise : true } ) ;
456
457
458
+ /**
459
+ * Starts a new session on the server
460
+ *
461
+ * @param {object } [options] optional settings for a driver session
462
+ * @param {MongoClient~sessionCallback } [callback] The callback called with a newly establish session, or an error if one occurred
463
+ * @return {Promise } if no callback is specified, a promise will be returned for the newly established session
464
+ */
465
+ MongoClient . prototype . startSession = function ( options ) {
466
+ options = options || { } ;
467
+ if ( ! this . topology ) {
468
+ throw new MongoError ( 'Must connect to a server before calling this method' ) ;
469
+ }
470
+
471
+ const capabilities = this . topology . capabilities ( ) ;
472
+ if ( capabilities && ! capabilities . hasSessionSupport ) {
473
+ throw new MongoError ( 'Current topology does not support sessions' ) ;
474
+ }
475
+
476
+ const session = new ClientSession ( this . topology . s . coreTopology , this . s . sessionPool , options ) ;
477
+ return session ;
478
+ } ;
479
+
457
480
var mergeOptions = function ( target , source , flatten ) {
458
481
for ( var name in source ) {
459
482
if ( source [ name ] && typeof source [ name ] === 'object' && flatten ) {
@@ -611,6 +634,11 @@ function relayEvents(self, topology) {
611
634
} ) ;
612
635
}
613
636
637
+ function assignTopology ( client , topology ) {
638
+ client . topology = topology ;
639
+ client . s . sessionPool = new ServerSessionPool ( topology . s . coreTopology ) ;
640
+ }
641
+
614
642
function createServer ( self , options , callback ) {
615
643
// Set default options
616
644
var servers = translateOptions ( options ) ;
@@ -629,8 +657,9 @@ function createServer(self, options, callback) {
629
657
addListeners ( self , servers [ 0 ] ) ;
630
658
// Check if we are really speaking to a mongos
631
659
var ismaster = topology . lastIsMaster ( ) ;
660
+
632
661
// Set the topology
633
- self . topology = topology ;
662
+ assignTopology ( self , topology ) ;
634
663
635
664
// Do we actually have a mongos
636
665
if ( ismaster && ismaster . msg === 'isdbgrid' ) {
@@ -659,7 +688,8 @@ function createReplicaset(self, options, callback) {
659
688
// Open the connection
660
689
topology . connect ( options , function ( err , topology ) {
661
690
if ( err ) return callback ( err ) ;
662
- self . topology = topology ;
691
+
692
+ assignTopology ( self , topology ) ;
663
693
callback ( null , topology ) ;
664
694
} ) ;
665
695
}
@@ -676,7 +706,8 @@ function createMongos(self, options, callback) {
676
706
// Open the connection
677
707
topology . connect ( options , function ( err , topology ) {
678
708
if ( err ) return callback ( err ) ;
679
- self . topology = topology ;
709
+
710
+ assignTopology ( self , topology ) ;
680
711
callback ( null , topology ) ;
681
712
} ) ;
682
713
}
@@ -772,7 +803,8 @@ var connect = function(self, url, options, callback) {
772
803
// Did we pass in a Server/ReplSet/Mongos
773
804
if ( url instanceof Server || url instanceof ReplSet || url instanceof Mongos ) {
774
805
// Set the topology
775
- self . topology = url ;
806
+ assignTopology ( self , url ) ;
807
+
776
808
// Add listeners
777
809
addListeners ( self , url ) ;
778
810
// Connect
0 commit comments