I would like to use PlanetScale with my App Platform app, how can I configure it to use the proxy that is required?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

👋

Great question! Since PlanetScale has a neat client that creates a proxy to the database instance, we will have to install and authenticate that tool so that it can inject a DATABASE_URL environment variable into the app.

  1. Setup a service token using this PlanetScale tutorial
  2. The app component that will be connecting to your PlanetScale database will need to have it’s build command altered to install the client like so: export PSCALE_VERSION="$(curl https://github.com/planetscale/cli/releases/latest -s -L -I -o /dev/null -w '%{url_effective}' | awk '{n=split($1,A,"/v"); print A[n]}')" && \ curl -sL https://github.com/planetscale/cli/releases/download/v$PSCALE_VERSION/pscale_${PSCALE_VERSION}_linux_amd64.tar.gz | tar -xz -C ./ && \ chmod +x ./pscale
  3. We will also need to wrap the run command in the PlanetScale command for URL injection like so: ./pscale connect app-platform-test main --execute "node app.js"

Don’t forget to set the required PlanetScale environment variables from the tutorial!

PLANETSCALE_ORG=your-org-name
PLANETSCALE_SERVICE_TOKEN_NAME=your-token-name
PLANETSCALE_SERVICE_TOKEN=your-token-value

And that’s it! PlanetScale connections in App Platform! If you want to support PlanetScale with an a dockerfile based app add the install command above prefixed with RUN to your dockerfile, for example:

export PSCALE_VERSION="$(curl https://github.com/planetscale/cli/releases/latest -s -L -I -o /dev/null -w '%{url_effective}' | awk '{n=split($1,A,"/v"); print A[n]}')" && \
curl -sL https://github.com/planetscale/cli/releases/download/v$PSCALE_VERSION/pscale_${PSCALE_VERSION}_linux_amd64.tar.gz | tar -xz -C ./ && \
chmod +x ./pscale

Don’t forget to ensure your run command is wrapped in the pscale connect... command :)

Here’s an example app spec:

name: pscale-express-example
region: nyc
services:
- build_command: |-
    export PSCALE_VERSION="$(curl https://github.com/planetscale/cli/releases/latest -s -L -I -o /dev/null -w '%{url_effective}' | awk '{n=split($1,A,"/v"); print A[n]}')" && \
    curl -sL https://github.com/planetscale/cli/releases/download/v$PSCALE_VERSION/pscale_${PSCALE_VERSION}_linux_amd64.tar.gz | tar -xz -C ./ && \
    chmod +x ./pscale
  environment_slug: node-js
  envs:
  - key: PLANETSCALE_ORG
    scope: RUN_AND_BUILD_TIME
    value: <org>
  - key: PLANETSCALE_SERVICE_TOKEN_NAME
    scope: RUN_AND_BUILD_TIME
    value: <service_token_name>
  - key: PLANETSCALE_SERVICE_TOKEN
    scope: RUN_AND_BUILD_TIME
    value: <service_token>
  github:
    branch: main
    deploy_on_push: true
    repo: planetscale/express-example
  http_port: 8080
  instance_count: 1
  instance_size_slug: basic-xs
  name: pscale-express-example
  routes:
  - path: /
  run_command: ./pscale connect <database_name> main --execute "node app.js"
  source_dir: /