@@ -29,7 +29,7 @@ export class FlightSqlClient {
2929 this . flight = flight ;
3030 }
3131
32- private async login ( username : string , password : string ) : Promise < void > {
32+ private async login ( username : string , password : string , defaultDatabase ?: string ) : Promise < void > {
3333 // Most servers seem to use Basic auth for the handshake to get a token.
3434 // The GRPC headers (and not the handshake request / response) are used to
3535 // transmit the username, password, and token.
@@ -50,23 +50,26 @@ export class FlightSqlClient {
5050 // The logic here is maybe not ideal. If a server someone sends an empty metadata message and then
5151 // a populated data payload message, then we will see the empty payload message first and fail to
5252 // wait for the next message. We can optimize this later if we see it in the wild.
53- const hello_rsp = await firstValueFrom ( call . responses ) ;
53+ const helloRsp = await firstValueFrom ( call . responses ) ;
5454
55- if ( hello_rsp ?. data ?. payload ) {
55+ const defaultMetadata = { } ;
56+ if ( defaultDatabase ) {
57+ defaultMetadata [ "database" ] = defaultDatabase ;
58+ }
59+
60+ if ( helloRsp ?. data ?. payload ) {
5661 // If we get a payload prefer that as a token
57- const payload = new TextDecoder ( ) . decode ( hello_rsp . data . payload ) ;
58- this . flight . set_default_metadata ( {
59- authorization : "Bearer " + payload ,
60- } ) ;
61- } else if ( hello_rsp ?. metadata ) {
62+ const payload = new TextDecoder ( ) . decode ( helloRsp . data . payload ) ;
63+ defaultMetadata [ "authorization" ] = "Bearer " + payload ;
64+ this . flight . set_default_metadata ( defaultMetadata ) ;
65+ } else if ( helloRsp ?. metadata ) {
6266 // Otherwise if we get metadata then use that as the token
63- const authorization = hello_rsp . metadata . get_first_string ( "authorization" ) ;
67+ const authorization = helloRsp . metadata . get_first_string ( "authorization" ) ;
6468 if ( ! authorization ) {
6569 throw new Error ( "Handshake failed, metadata received but no authorization header present" ) ;
6670 }
67- this . flight . set_default_metadata ( {
68- authorization,
69- } ) ;
71+ defaultMetadata [ "authorization" ] = authorization ;
72+ this . flight . set_default_metadata ( defaultMetadata ) ;
7073 } else {
7174 throw new Error ( "Handshake failed, no metadata or data received from server before call completed" ) ;
7275 }
@@ -91,10 +94,15 @@ export class FlightSqlClient {
9194 * @param password The password to use for the handshake
9295 * @returns A client that can be used to execute queries
9396 */
94- public static async connect ( host : string , username : string , password : string ) : Promise < FlightSqlClient > {
97+ public static async connect (
98+ host : string ,
99+ username : string ,
100+ password : string ,
101+ defaultDatabase ?: string ,
102+ ) : Promise < FlightSqlClient > {
95103 const sql = new FlightClient ( host ) ;
96104 const client = new FlightSqlClient ( sql ) ;
97- await client . login ( username , password ) ;
105+ await client . login ( username , password , defaultDatabase ) ;
98106 return client ;
99107 }
100108
0 commit comments