Database Connections in module

Trying to build a custom scripting module and have the scripting module make connections to one of many different databases already setup inside of Ignition. I also want to try to take advantage of the default project database.

IE if I run system.mycustommodule.customscript(param1) I want it to use the projects default database whereas if I want to run system.mycustommodule.customscript(param1, db1) I want it to use db1 as the database.

So far I understand that the code for this would be pretty different for both gateway and client/designer scopes (vision) which means that I am limited in what I can share between the two scopes codewise (in the common artifact).

I know that I need the following code to run the query but I am missing how I can get the default connections.

        SRConnect con = null;
            con = context.getDatasourceManager().getConnection("customdatasource");
            // data logic 
            } finally {

You can generally expect the ScriptContext threadlocal to have the project's current datasource (if your script is being executed in a context which has a default datasource).

This is broadly true, yes. A common pattern in our scripting function implementations is to have the 'common' code do the argument parsing/handling, to ensure things are consistent and, if you're accepting keyword arguments, to "unpack" them to plain Java values. Then, from common, invoke an protected abstract doSomethingImpl method that can be filled in for each scope.