serverless-mysql icon indicating copy to clipboard operation
serverless-mysql copied to clipboard

HELP: How to change database name in every invocation?

Open dashmug opened this issue 5 years ago • 1 comments

I work on a multi-tenant application with each tenant having their own database living on the same database instance.

So, currently in my Lambdas, I connect AND disconnect during the Lambda invocation as I need the request information to determine which database to connect to. The host, port, username, password are all constants. Only the database name changes between invocations. This setup currently works easily with the mysql library. However, it is not optimal as I cannot reuse connections.

Will changeUser help me in this case?

Is it possible to connect to the database instance during cold-start but switch database during invocation?

Does changeUser disconnect from the instance?

Thank you for the help.

dashmug avatar Nov 25 '19 17:11 dashmug

Hi @dashmug,

changeUser has not been implemented in the library as of yet, but if you need to access data on the same instance (and the user has access to both databases), you can preface your queries with the database name, e.g.:

SELECT * FROM myDatabase.myTable WHERE x = 1;

The changeUser command doesn't disconnect the connection, but as I said, it isn't implemented in the library yet.

  • Jeremy

jeremydaly avatar Nov 25 '19 17:11 jeremydaly