PlanetScale
PlanetScale ↗ is a MySQL-compatible platform that makes databases infinitely scalable, easier and safer to manage.
To set up an integration with PlanetScale:
-
You need to have an existing PlanetScale database to connect to. Create a PlanetScale database ↗ or import an existing database to PlanetScale ↗.
-
From the PlanetScale web console ↗, create a
products
table with the following query:CREATE TABLE products (id int NOT NULL AUTO_INCREMENT PRIMARY KEY,name varchar(255) NOT NULL,image_url varchar(255),category_id INT,KEY category_id_idx (category_id)); -
Insert some data in your newly created table. Run the following command to add a product and category to your table:
INSERT INTO products (name, image_url, category_id)VALUES ('Ballpoint pen', 'https://example.com/500x500', '1'); -
Configure the PlanetScale database credentials in your Worker:
You need to add your PlanetScale database credentials as secrets to your Worker. Get your connection details from the PlanetScale Dashboard ↗ by creating a connection string, then add them as secrets using Wrangler:
Terminal window # Add the database host as a secretnpx wrangler secret put DATABASE_HOST# When prompted, paste your PlanetScale host# Add the database username as a secretnpx wrangler secret put DATABASE_USERNAME# When prompted, paste your PlanetScale username# Add the database password as a secretnpx wrangler secret put DATABASE_PASSWORD# When prompted, paste your PlanetScale password -
In your Worker, install the
@planetscale/database
driver to connect to your PlanetScale database and start manipulating data:Terminal window npm i @planetscale/databaseTerminal window yarn add @planetscale/databaseTerminal window pnpm add @planetscale/database -
The following example shows how to make a query to your PlanetScale database in a Worker. The credentials needed to connect to PlanetScale have been added as secrets to your Worker.
import { connect } from "@planetscale/database";export default {async fetch(request, env) {const config = {host: env.DATABASE_HOST,username: env.DATABASE_USERNAME,password: env.DATABASE_PASSWORD,// see https://github.com/cloudflare/workerd/issues/698fetch: (url, init) => {delete init["cache"];return fetch(url, init);},};const conn = connect(config);const data = await conn.execute("SELECT * FROM products;");return new Response(JSON.stringify(data.rows), {status: 200,headers: {"Content-Type": "application/json",},});},};
To learn more about PlanetScale, refer to PlanetScale's official documentation ↗.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark