@@ -78,6 +78,17 @@ type CreateConnectionProvider = (
78
78
hostNameResolver : ConfiguredCustomResolver
79
79
) => ConnectionProvider
80
80
81
+ type CreateSession = ( args : {
82
+ mode : SessionMode
83
+ connectionProvider : ConnectionProvider
84
+ bookmark ?: Bookmark
85
+ database : string
86
+ config : any
87
+ reactive : boolean
88
+ fetchSize : number ,
89
+ impersonatedUser ?: string
90
+ } ) => Session
91
+
81
92
interface DriverConfig {
82
93
encrypted ?: EncryptionLevel | boolean
83
94
trust ?: TrustStrategy
@@ -102,6 +113,7 @@ class Driver {
102
113
private readonly _log : Logger
103
114
private readonly _createConnectionProvider : CreateConnectionProvider
104
115
private _connectionProvider : ConnectionProvider | null
116
+ private readonly _createSession : CreateSession
105
117
106
118
/**
107
119
* You should not be calling this directly, instead use {@link driver}.
@@ -110,11 +122,13 @@ class Driver {
110
122
* @param {Object } meta Metainformation about the driver
111
123
* @param {Object } config
112
124
* @param {function(id: number, config:Object, log:Logger, hostNameResolver: ConfiguredCustomResolver): ConnectionProvider } createConnectonProvider Creates the connection provider
113
- */
125
+ * @param {function(args): Session } createSession Creates the a session
126
+ */
114
127
constructor (
115
128
meta : MetaInfo ,
116
129
config : DriverConfig = { } ,
117
- createConnectonProvider : CreateConnectionProvider
130
+ createConnectonProvider : CreateConnectionProvider ,
131
+ createSession : CreateSession = args => new Session ( args )
118
132
) {
119
133
sanitizeConfig ( config )
120
134
validateConfig ( config )
@@ -124,6 +138,7 @@ class Driver {
124
138
this . _config = config
125
139
this . _log = Logger . create ( config )
126
140
this . _createConnectionProvider = createConnectonProvider
141
+ this . _createSession = createSession
127
142
128
143
/**
129
144
* Reference to the connection provider. Initialized lazily by {@link _getOrCreateConnectionProvider}.
@@ -177,6 +192,19 @@ class Driver {
177
192
return connectionProvider . supportsTransactionConfig ( )
178
193
}
179
194
195
+ /**
196
+ * Returns whether the server supports user impersonation capabilities based on the protocol
197
+ * version negotiated via handshake.
198
+ *
199
+ * Note that this function call _always_ causes a round-trip to the server.
200
+ *
201
+ * @returns {Promise<boolean> } promise resolved with a boolean or rejected with error.
202
+ */
203
+ supportsUserImpersonation ( ) : Promise < boolean > {
204
+ const connectionProvider = this . _getOrCreateConnectionProvider ( )
205
+ return connectionProvider . supportsUserImpersonation ( )
206
+ }
207
+
180
208
/**
181
209
* @protected
182
210
* @returns {boolean }
@@ -224,24 +252,28 @@ class Driver {
224
252
* @param {number } param.fetchSize - The record fetch size of each batch of this session.
225
253
* Use {@link FETCH_ALL} to always pull all records in one batch. This will override the config value set on driver config.
226
254
* @param {string } param.database - The database this session will operate on.
255
+ * @param {string } param.impersonatedUser - The username which the user wants to impersonate for the duration of the session.
227
256
* @return {Session } new session.
228
257
*/
229
258
session ( {
230
259
defaultAccessMode = WRITE ,
231
260
bookmarks : bookmarkOrBookmarks ,
232
261
database = '' ,
262
+ impersonatedUser,
233
263
fetchSize
234
264
} : {
235
265
defaultAccessMode ?: SessionMode
236
266
bookmarks ?: string | string [ ]
237
267
database ?: string
268
+ impersonatedUser ?: string
238
269
fetchSize ?: number
239
270
} = { } ) : Session {
240
271
return this . _newSession ( {
241
272
defaultAccessMode,
242
273
bookmarkOrBookmarks,
243
274
database,
244
275
reactive : false ,
276
+ impersonatedUser,
245
277
fetchSize : validateFetchSizeValue ( fetchSize , this . _config . fetchSize ! ! )
246
278
} )
247
279
}
@@ -277,26 +309,29 @@ class Driver {
277
309
bookmarkOrBookmarks,
278
310
database,
279
311
reactive,
312
+ impersonatedUser,
280
313
fetchSize
281
314
} : {
282
315
defaultAccessMode : SessionMode
283
316
bookmarkOrBookmarks ?: string | string [ ]
284
317
database : string
285
318
reactive : boolean
319
+ impersonatedUser ?: string
286
320
fetchSize : number
287
321
} ) {
288
322
const sessionMode = Session . _validateSessionMode ( defaultAccessMode )
289
323
const connectionProvider = this . _getOrCreateConnectionProvider ( )
290
324
const bookmark = bookmarkOrBookmarks
291
325
? new Bookmark ( bookmarkOrBookmarks )
292
326
: Bookmark . empty ( )
293
- return new Session ( {
327
+ return this . _createSession ( {
294
328
mode : sessionMode ,
295
329
database : database || '' ,
296
330
connectionProvider,
297
331
bookmark,
298
332
config : this . _config ,
299
333
reactive,
334
+ impersonatedUser,
300
335
fetchSize
301
336
} )
302
337
}
0 commit comments