We've been connecting to PlanetScale from the pscale
CLI, and while that will work for testing purposes that won't work in a deployed serverless function.
To connect to a PlanetScale database from a deployed serverless function we'll need a connection string that is stable and won't expire. To get that we can create a new username and password to use in a connection string from the PlanetScale dashboard for our database.
MySql connection strings are in the form of mysql://<username>:<password>@<host>:<port>/<db_name>
so we have two options, we can insert all of the separate variable as distinct env vars or we can construct the string ourselves and use that in an environment variable. We'll choose option two since that will match what the pscale
cli gives us and makes it easier to work across environments.
Put the constructed mysql string as DATABASE_URL
in the netlify "Build & Deploy" Environment
.
We can now deploy our function with the new environment variable set.