Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions src/content/docs/workers/databases/connecting-to-databases.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,16 @@ Serverless databases provide HTTP-based proxies and drivers, also known as serve

By providing a way to query your database with HTTP, these serverless databases and drivers eliminate several roundtrips needed to establish a secure connection.

| Database | Integration | Library or Driver | Connection Method |
| --------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | ---------------------------------------------------------------------------------- | ----------------------- |
| [PlanetScale](https://planetscale.com/blog/introducing-the-planetscale-serverless-driver-for-javascript) | [Yes](/workers/databases/native-integrations/planetscale/) | [@planetscale/database](https://github.com/planetscale/database-js) | API via client library |
| [Supabase](https://github.com/supabase/supabase/tree/master/examples/with-cloudflare-workers) | [Yes](/workers/databases/native-integrations/supabase/) | [@supabase/supabase-js](https://github.com/supabase/supabase-js) | API via client library |
| [Prisma](https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-cloudflare-workers) | No | [prisma](https://github.com/prisma/prisma) | API via client library |
| [Neon](https://blog.cloudflare.com/neon-postgres-database-from-workers/) | [Yes](/workers/databases/native-integrations/neon/) | [@neondatabase/serverless](https://neon.tech/blog/serverless-driver-for-postgres/) | API via client library |
| [Hasura](https://hasura.io/blog/building-applications-with-cloudflare-workers-and-hasura-graphql-engine/) | No | API | GraphQL API via fetch() |
| [Upstash Redis](https://blog.cloudflare.com/cloudflare-workers-database-integration-with-upstash/) | [Yes](/workers/databases/native-integrations/upstash/) | [@upstash/redis](https://github.com/upstash/upstash-redis) | API via client library |
| [TiDB Cloud](https://docs.pingcap.com/tidbcloud/integrate-tidbcloud-with-cloudflare) | No | [@tidbcloud/serverless](https://github.com/tidbcloud/serverless-js) | API via client library |
| Database | Library or Driver | Connection Method |
| --------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ----------------------- |
| [PlanetScale](https://planetscale.com/blog/introducing-the-planetscale-serverless-driver-for-javascript) | [@planetscale/database](https://github.com/planetscale/database-js) | API via client library |
| [Supabase](https://github.com/supabase/supabase/tree/master/examples/with-cloudflare-workers) | [@supabase/supabase-js](https://github.com/supabase/supabase-js) | API via client library |
| [Prisma](https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-cloudflare-workers) | [prisma](https://github.com/prisma/prisma) | API via client library |
| [Neon](https://blog.cloudflare.com/neon-postgres-database-from-workers/) | [@neondatabase/serverless](https://neon.tech/blog/serverless-driver-for-postgres/) | API via client library |
| [Hasura](https://hasura.io/blog/building-applications-with-cloudflare-workers-and-hasura-graphql-engine/) | API | GraphQL API via fetch() |
| [Upstash Redis](https://blog.cloudflare.com/cloudflare-workers-database-integration-with-upstash/) | [@upstash/redis](https://github.com/upstash/upstash-redis) | API via client library |
| [TiDB Cloud](https://docs.pingcap.com/tidbcloud/integrate-tidbcloud-with-cloudflare) | [@tidbcloud/serverless](https://github.com/tidbcloud/serverless-js) | API via client library |

:::note[Easier setup with database integrations]

[Database Integrations](/workers/databases/native-integrations/) simplify the authentication for serverless database drivers by managing credentials on your behalf and includes support for PlanetScale, Neon and Supabase.

If you do not see an integration listed or have an integration to add, complete and submit the [Cloudflare Developer Platform Integration form](https://forms.gle/iaUqLWE8aezSEhgd6).

:::

Once you have installed the necessary packages, use the APIs provided by these packages to connect to your database and perform operations on it. Refer to detailed links for service-specific instructions.

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
pcx_content_type: navigation
title: 3rd Party Integrations
head: []
description: Connect to third-party databases such as Supabase,
Turso and PlanetScale)
Comment on lines +5 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a syntax error in the description field. The closing parenthesis at the end doesn't have a matching opening parenthesis.

Suggested change
description: Connect to third-party databases such as Supabase,
Turso and PlanetScale)
description: Connect to third-party databases such as Supabase,
Turso and PlanetScale


---

import { DirectoryListing } from "~/components"

## Background

Connect to databases by configuring connection strings and credentials as [secrets](/workers/configuration/secrets/) in your Worker.

:::note[Making multiple round trip calls to a centralized database from a Worker?]


If your Worker is making multiple round trip calls to a centralized database, your Worker may be a good fit for Smart Placement. Smart Placement speeds up applications by automatically running your Worker closer to your back-end infrastructure rather than the end user. Learn more about [how Smart Placement works](/workers/configuration/smart-placement/).
:::

## Database credentials

When you rotate or update database credentials, you must update the corresponding [secrets](/workers/configuration/secrets/) in your Worker. Use the [`wrangler secret put`](/workers/wrangler/commands/#secret) command to update secrets securely or update the secret directly in the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/services/view/:worker/production/settings).

## Database limits

You can connect to multiple databases by configuring separate sets of secrets for each database connection. Use descriptive secret names to distinguish between different database connections (for example, `DATABASE_URL_PROD` and `DATABASE_URL_STAGING`).

## Popular providers

<DirectoryListing />
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Render, PackageManagers } from "~/components";

[Neon](https://neon.tech/) is a fully managed serverless PostgreSQL. It separates storage and compute to offer modern developer features, such as serverless, branching, and bottomless storage.

<Render file="database_integrations_definition" />

## Set up an integration with Neon

To set up an integration with Neon:
Expand Down Expand Up @@ -43,21 +41,23 @@ To set up an integration with Neon:
(10, 'Neon', 10, 'Ne');
```

4. Add the Neon database integration to your Worker:
4. Configure the Neon database credentials in your Worker:

You need to add your Neon database connection string as a secret to your Worker. Get your connection string from the [Neon Console](https://console.neon.tech) under **Connection Details**, then add it as a secret using Wrangler:

1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
2. In **Account Home**, select **Workers & Pages**.
3. In **Overview**, select your Worker.
4. Select **Integrations** > **Neon**.
5. Follow the setup flow, selecting the database created in step 1.
```sh
# Add the database connection string as a secret
npx wrangler secret put DATABASE_URL
# When prompted, paste your Neon database connection string
```

5. In your Worker, install the `@neondatabase/serverless` driver to connect to your database and start manipulating data:

<PackageManagers pkg="@neondatabase/serverless" />

6. The following example shows how to make a query to your Neon database in a Worker. The credentials needed to connect to Neon have been automatically added as secrets to your Worker through the integration.
6. The following example shows how to make a query to your Neon database in a Worker. The credentials needed to connect to Neon have been added as secrets to your Worker.

```sql
```js
import { Client } from '@neondatabase/serverless';

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Render, PackageManagers } from "~/components";

[PlanetScale](https://planetscale.com/) is a MySQL-compatible platform that makes databases infinitely scalable, easier and safer to manage.

<Render file="database_integrations_definition" />

## Set up an integration with PlanetScale

To set up an integration with PlanetScale:
Expand All @@ -34,19 +32,29 @@ To set up an integration with PlanetScale:
VALUES ('Ballpoint pen', 'https://example.com/500x500', '1');
```

4. Add the PlanetScale integration to your Worker:

1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
2. In **Account Home**, select **Workers & Pages**.
3. In **Overview**, select your Worker.
4. Select **Integrations** > **PlanetScale**.
5. Follow the setup flow, selecting the database created in step 1.
4. 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](https://app.planetscale.com) by creating a connection string, then add them as secrets using Wrangler:

```sh
# Add the database host as a secret
npx wrangler secret put DATABASE_HOST
# When prompted, paste your PlanetScale host

# Add the database username as a secret
npx wrangler secret put DATABASE_USERNAME
# When prompted, paste your PlanetScale username

# Add the database password as a secret
npx wrangler secret put DATABASE_PASSWORD
# When prompted, paste your PlanetScale password
```

5. In your Worker, install the `@planetscale/database` driver to connect to your PlanetScale database and start manipulating data:

<PackageManagers pkg="@planetscale/database" />

6. 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 automatically added as secrets to your Worker through the integration.
6. 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.

```js
import { connect } from "@planetscale/database";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Render, PackageManagers } from "~/components";

[Supabase](https://supabase.com/) is an open source Firebase alternative and a PostgreSQL database service that offers real-time functionality, database backups, and extensions. With Supabase, developers can quickly set up a PostgreSQL database and build applications.

<Render file="database_integrations_definition" />

## Set up an integration with Supabase

To set up an integration with Supabase:
Expand All @@ -35,21 +33,27 @@ To set up an integration with Supabase:
INSERT INTO countries (name) VALUES ('The Netherlands');
```

4. Add the Supabase database integration to your Worker:
4. Configure the Supabase database credentials in your Worker:

You need to add your Supabase URL and anon key as secrets to your Worker. Get these from your [Supabase Dashboard](https://supabase.com/dashboard) under **Settings** > **API**, then add them as secrets using Wrangler:

1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
2. In **Account Home**, select **Workers & Pages**.
3. In **Overview**, select your Worker.
4. Select **Integrations** > **Supabase**.
5. Follow the setup flow, selecting the database created in step 1.
```sh
# Add the Supabase URL as a secret
npx wrangler secret put SUPABASE_URL
# When prompted, paste your Supabase project URL

# Add the Supabase anon key as a secret
npx wrangler secret put SUPABASE_KEY
# When prompted, paste your Supabase anon/public key
```

5. In your Worker, install the `@supabase/supabase-js` driver to connect to your database and start manipulating data:

<PackageManagers pkg="@supabase/supabase-js" />

6. The following example shows how to make a query to your Supabase database in a Worker. The credentials needed to connect to Supabase have been automatically added as secrets to your Worker through the integration.
6. The following example shows how to make a query to your Supabase database in a Worker. The credentials needed to connect to Supabase have been added as secrets to your Worker.

```sql
```js
import { createClient } from '@supabase/supabase-js';

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { Render, PackageManagers } from "~/components";

[Turso](https://turso.tech/) is an edge-hosted, distributed database based on [libSQL](https://libsql.org/), an open-source fork of SQLite. Turso was designed to minimize query latency for applications where queries comes from anywhere in the world.

<Render file="database_integrations_definition" />

## Set up an integration with Turso

To set up an integration with Turso:
Expand Down Expand Up @@ -84,19 +82,35 @@ To set up an integration with Turso:
(10, 'Neon', 10, 'Ne');
```

4. Add the Turso database integration to your Worker:
4. Configure the Turso database credentials in your Worker:

You need to add your Turso database URL and authentication token as secrets to your Worker. First, get your database URL and create an authentication token:

```sh
# Get your database URL
turso db show my-db --url

# Create an authentication token
turso db tokens create my-db
```

Then add these as secrets to your Worker using Wrangler:

1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com) and select your account.
2. In **Account Home**, select **Workers & Pages**.
3. In **Overview**, select your Worker.
4. Select **Integrations** > **Turso**.
5. Follow the setup flow, selecting the database created in step 1.
```sh
# Add the database URL as a secret
npx wrangler secret put TURSO_URL
# When prompted, paste your database URL

# Add the authentication token as a secret
npx wrangler secret put TURSO_AUTH_TOKEN
# When prompted, paste your authentication token
```

5. In your Worker, install the Turso client library:

<PackageManagers pkg="@libsql/client" />

6. The following example shows how to make a query to your Turso database in a Worker. The credentials needed to connect to Turso have been automatically added as [secrets](/workers/configuration/secrets/) to your Worker through the integration.
6. The following example shows how to make a query to your Turso database in a Worker. The credentials needed to connect to Turso have been added as [secrets](/workers/configuration/secrets/) to your Worker.

```ts
import { Client as LibsqlClient, createClient } from "@libsql/client/web";
Expand Down
Loading