@@ -59,23 +59,30 @@ func Connect(arg any) mongo.Client {
5959 }
6060
6161 opts .Alias = lo .CoalesceOrEmpty (opts .Alias , "default" )
62+
6263 clientOpts := lo .CoalesceOrEmpty (opts .ClientOptions , options .Client ()).
6364 SetServerAPIOptions (options .ServerAPI (options .ServerAPIVersion1 )).
64- SetPoolMonitor (lo .CoalesceOrEmpty (opts .PoolMonitor , defaultPoolMonitor (opts .Alias )))
65- if clientOpts .GetURI () == "" {
66- if opts .URI == "" {
67- panic (ErrURIRequired )
68- }
69- clientOpts = clientOpts .ApplyURI (opts .URI )
65+ SetPoolMonitor (lo .CoalesceOrEmpty (opts .PoolMonitor , defaultPoolMonitor (opts .Alias ))) // Set default client options if not provided
66+
67+ if opts .URI = lo .CoalesceOrEmpty (clientOpts .GetURI (), opts .URI ); opts .URI == "" { // We prioritize the URI set in the client options if both are provided
68+ panic (ErrURIRequired )
7069 }
71- cs := lo .Must (connstring .ParseAndValidate (clientOpts .GetURI ()))
70+
71+ lo .Must0 (clientOpts .ApplyURI (opts .URI ).Validate ())
72+
73+ cs , _ := connstring .Parse (opts .URI ) // We can ignore the error here since we already validated the URI above
7274 defaultDatabases [opts .Alias ] = cs .Database
75+
7376 ctx , cancel := context .WithTimeout (context .Background (), * lo .CoalesceOrEmpty (clientOpts .ConnectTimeout , lo .ToPtr (ConnectionTimeout )))
7477 defer cancel ()
78+
7579 client := lo .Must (mongo .Connect (ctx , clientOpts ))
76- lo .Must0 (client .Ping (ctx , readpref .Primary ()))
80+ lo .Must0 (client .Ping (ctx , readpref .Primary ())) // Verify connection
81+
7782 clients [opts .Alias ] = client
83+
7884 triggerEventIfRegistered (opts .Alias , EventDeploymentDiscovered )
85+
7986 return * client
8087}
8188
0 commit comments