Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,10 @@ title: Xata

import { Render, TabItem, Tabs } from "~/components";

[Xata](https://xata.io) is a serverless data platform powered by PostgreSQL. Xata uniquely combines multiple types of stores (relational databases, search engines, analytics engines) into a single service, accessible through a consistent REST API.
[Xata](https://xata.io) is a PostgreSQL database platform designed to help developers operate and scale databases with enhanced productivity and performance. Xata provides features like instant copy-on-write database branches, zero-downtime schema changes, data anonymization, AI-powered performance monitoring, and BYOC.

:::note

You can connect to Xata using [Hyperdrive](/hyperdrive) (recommended), or using the Xata client, `@xata.io/client`. Both provide connection pooling and reduce the amount of round trips required to create a secure connection from Workers to your database.

Hyperdrive can provide lower latencies because it performs the database connection setup and connection pooling across Cloudflare's network. Hyperdrive supports native database drivers, libraries, and ORMs, and is included in all [Workers plans](/hyperdrive/platform/pricing/). Learn more about Hyperdrive in [How Hyperdrive Works](/hyperdrive/configuration/how-hyperdrive-works/).

:::

<Tabs>
<TabItem label="Hyperdrive (recommended)">
Read the full [Xata documentation](https://xata.io/documentation).

To connect to Xata using [Hyperdrive](/hyperdrive), follow these steps:

<Render file="xata-partial" product="hyperdrive"/>
</TabItem>
<TabItem label="Xata client">

## Set up an integration with Xata

To set up an integration with Xata:

1. You need to have an existing Xata database to connect to or create a new database from your Xata workspace [Create a Database](https://app.xata.io/workspaces).

2. In your database, you have several options for creating a table: you can start from scratch, use a template filled with sample data, or import data from a CSV file. For this guide, choose **Start with sample data**. This option automatically populates your database with two sample tables: `Posts` and `Users`.

3. Configure the Xata database credentials in your Worker:

You need to add your Xata database credentials as secrets to your Worker. First, get your database details from your [Xata Dashboard](https://app.xata.io), then add them as secrets using Wrangler:

```sh
# Add the Xata API key as a secret
npx wrangler secret put XATA_API_KEY
# When prompted, paste your Xata API key

# Add the Xata branch as a secret
npx wrangler secret put XATA_BRANCH
# When prompted, paste your Xata branch name (usually 'main')

# Add the Xata database URL as a secret
npx wrangler secret put XATA_DATABASE_URL
# When prompted, paste your Xata database URL
```

4. Install the [Xata CLI](https://xata.io/docs/getting-started/installation) and authenticate the CLI by running the following commands:

```sh
npm install -g @xata.io/cli

xata auth login
```

5. Once you have the CLI set up, In your Worker, run the following code in the root directory of your project:

```sh
xata init
```

Accept the default settings during the configuration process. After completion, a `.env` and `.xatarc` file will be generated in your project folder.

6. To enable Cloudflare access the secret values generated when running in development mode, create a `.dev.vars` file in your project's root directory and add the following content, replacing placeholders with the specific values:

```txt
XATA_API_KEY=<YOUR_API_KEY_HERE>
XATA_BRANCH=<YOUR_BRANCH_HERE>
XATA_DATABASE_URL=<YOUR_DATABASE_URL_HERE>
```

7. The following example shows how to make a query to your Xata database in a Worker. The credentials needed to connect to Xata have been added as secrets to your Worker.

```ts
export default {
async fetch(request, env, ctx): Promise<Response> {
const xata = new XataClient({
apiKey: env.XATA_API_KEY,
branch: env.XATA_BRANCH,
databaseURL: env.XATA_DATABASE_URL,
});

const records = await xata.db.Posts.select([
"id",
"title",
"author.name",
"author.email",
"author.bio",
]).getAll();

return Response.json(records);
},
} satisfies ExportedHandler<Env>;
```

To learn more about Xata, refer to [Xata's official documentation](https://xata.io/docs).

</TabItem>
</Tabs>
<Render file="xata-partial" product="hyperdrive" />
9 changes: 5 additions & 4 deletions src/content/partials/hyperdrive/xata-partial.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import { Render } from "~/components";

## 1. Allow Hyperdrive access

You can connect Hyperdrive to any existing Xata database with the default user and password provided by Xata.
You can connect Hyperdrive to any existing Xata PostgreSQL database with the connection string provided by Xata.

### Xata dashboard

To retrieve your connection string from the Xata dashboard:

1. Go to the [**Xata dashboard**](https://app.xata.io/).
1. Go to the [**Xata dashboard**](https://xata.io/).
2. Select the database you want to connect to.
3. Select **Settings**.
4. Copy the connection string from the `PostgreSQL endpoint` section and add your API key.
3. Copy the `PostgreSQL` connection string.

Read the full [Xata documentation](https://xata.io/documentation).

## 2. Create a database configuration

Expand Down