Introduction
Cloudflare Workers database integration is designed to connect your Cloudflare Workers to data sources automatically by generating connection strings and storing them in the worker’s secrets. This article will utilize a sample repository that is a preconfigured Cloudflare Worker you can use to deploy to your Cloudflare account.Prerequisites
- NodeJS installed
- A PlanetScale account
- The PlanetScale CLI
- A Cloudflare account
Set up the database
1
Create a database in your PlanetScale account named
bookings_db
.2
Connect to the
main
branch of the new database.3
Run the following commands to create a table in the database and populate it with some data.
Deploy the Cloudflare Worker
1
Clone the sample repository.
2
Navigate to the
worker
folder of the repository and install the dependencies.3
Deploy the Worker to your Cloudflare account.
Configure the Cloudflare PlanetScale integration
1
Log into the Cloudflare dashboard and navigate to “Workers” > “Overview”. You should see a service in the list named “planetscale-worker”. Select it from the list.

2
Select the “Settings” tab, then “Integrations”, and finally “Add Integration” in the PlanetScale card.

3
Click “Accept” under Review and grant permissions to allow the wizard to write the database connection details to the Worker secrets.

4
Under Connect to PlanetScale, click “Connect” to start the process of connecting your PlanetScale and Cloudflare accounts.

5
A modal will appear allowing you to grant access to your organization, database, and branch. Start by selecting your organization from the list. This demonstration uses an organization named “ps-deved”.

6
Select the “bookings_db” database from the list in the Databases card, and the “main” branch from the list in the Branches card. Finally, click “Authorize access”.

7
Select your organization again from the list and click “Continue”.

8
Select your database and the user role you want the integration to have.

9
Select the “main” branch from the list and click “Continue”.

10
You’ll be given the option to rename the secrets that will be configured on your behalf. These can be left as is. Click “Add Integration” to complete the process.

Test the integration
Back in the overview of the Worker, there is a preview URL that you can use to open a new tab in your browser that runs the Worker and displays the results. Once you’ve located the preview URL, click it to test the Worker.
Test other database operations (optional)
To test other database operations that are mapped to HTTP methods, you may use the providedtests.http
file which is designed to work with the VSCode REST client plugin. The file is preconfigured to work with the local environment, or you can change the @host
variable to match the URL provided in the Cloudflare dashboard that cooresponds with your Worker project.
Method | Operation |
---|---|
GET / | Get a list of all hotels. |
POST / | Create a hotel. |
PUT /:id | Update a hotel. |
DELETE /:id | Delete a hotel. |
What’s next?
Once you’re done with development, it is highly recommended that safe migrations be turned on for yourmain
production branch to protect from accidental schema changes and enable zero-downtime deployments.
When you’re ready to make more schema changes, you’ll create a new branch off of your production branch. Branching your database creates an isolated copy of your production schema so that you can easily test schema changes in development. Once you’re happy with the changes, you’ll open a deploy request. This will generate a diff showing the changes that will be deployed, making it easy for your team to review.
Learn more about how PlanetScale allows you to make non-blocking schema changes to your database tables without locking or causing downtime for production databases.