What is Xata?
Prisma

Prisma with Xata

Edit on GitHub

Prisma ORM is a TypeScript ORM that is focused on type safety and developer experience.

#

Installation

Prisma currently only works with Xata databases that are set up with the ability to make direct Postgres connections. Follow the instructions below to learn how to configure Prisma to work with Xata in this method.

Enable Postgres connections in Xata

Beta

Xata now allows direct connections to Postgres. To enable it follow these steps:


1

Create a brand new database with Postgres enabled

2

Use the @next version of our APIs within your project.

npm install @xata.io/client@next

Check the Postgres documentation for details and troubleshooting.

You can use the following package managers:

npm install prisma

To use Xata with Prisma define a postgresql datasource in the schema.prisma file. Here is an example:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}

You can manually define the schema or use the introspection feature to generate the schema from the database.

To introspect the database, you can use the following command:

prisma db pull

Introspection is necessary after Adapting an existing table in the UI. This action adds columns xata_id,xata_createdat,xata_updatedat to the table. prisma db pull updates the Prisma schema to include these.

To generate the client, you can use the following command:

prisma generate

After generating the client, you can use it in your code. Here is an example of using the client:

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({});

const result = await prisma.users.findMany({
  where: {
    name: 'Alice'
  }
});

Migrations with Prisma require a Shadow Database, which is an extra database that stores migration information and state.

Create an additional database in Xata (we recommend naming it databasename-migrations), configure this database's Postgres connection string as the shadowDatabaseUrl in the Prisma datasource and store the connection string in your .env file as SHADOW_DATABASE_URL. Refer to Prisma's documentation for more details.

  • Xata File attachments columns are read-only and only show file metadata. You will need to use the SDK to upload and download files.

On this page

InstallationInstall PrismaUsageShadow Database for MigrationsLimitations