Skip to content

Commit

Permalink
update content
Browse files Browse the repository at this point in the history
  • Loading branch information
ruheni committed Nov 1, 2023
1 parent 39315b3 commit 37170ba
Showing 1 changed file with 68 additions and 49 deletions.
117 changes: 68 additions & 49 deletions content/300-guides/050-database/900-turso.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Prisma support for Turso is currently in [Early Access](/about/prisma/releases#e

[Turso](https://turso.tech/) is an edge-hosted, distributed database that's based on [libSQL](https://turso.tech/libsql), an open-source and open-contribution fork of [SQLite](https://sqlite.org/), enabling you to bring data closer to your application and minimize query latency. Turso can also be hosted on a remote server.

<Admonition type="warning">

Support for Turso is available in [Early Access](/about/prisma/releases#early-access) from Prisma versions 5.4.2 and later.

</Admonition>

## Commonalities with other database providers

libSQL is 100% compatible with SQLite. libSQL extends SQLite and adds the following features and capabilities:
Expand Down Expand Up @@ -50,72 +56,85 @@ There are a number of differences between Turso and SQLite to consider. You shou

## Connect and query your primary database

1. Create a database on Turso if you don't have one already:
<br />

```sh
turso db create turso-prisma-db
```
<details>

2. Retrieve the database's connection string:
<summary>
Expand to learn how to provision a database and retrieve your credentials on
Turso
</summary>

```sh
turso db show turso-prisma-db
```
Ensure that you have the [Turso CLI](https://docs.turso.tech/reference/turso-cli) installed to manage your databases.

To provision a database on Turso, run the following command:

```sh
turso db create turso-prisma-db
```

Retrieve the database's connection string:

```sh
turso db show turso-prisma-db
```

3. Create an authentication token that will allow you to connect to the database:

```sh
turso db tokens create turso-prisma-db
```
```sh
turso db tokens create turso-prisma-db
```

4. Update your `.env` file with the authentication token and connection string:

```text
TURSO_AUTH_TOKEN="eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9..."
TURSO_DATABASE_URL="libsql://turso-prisma-db-user.turso.io"
```
```text file=.env
TURSO_AUTH_TOKEN="eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9..."
TURSO_DATABASE_URL="libsql://turso-prisma-db-user.turso.io"
```

5. Enable the `driverAdapters` Preview feature flag:

```prisma highlight=3;add
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
```
</details>

6. Generate Prisma Client:
To get started, enable the `driverAdapters` Preview feature flag:

```sh
npx prisma generate
```
```prisma highlight=3;add
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
7. Install the libSQL database client and Prisma driver adapter for libSQL packages:
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
```

```sh
npm install @libsql/client @prisma/adapter-libsql
```
Generate Prisma Client:

8. Update your Prisma Client instance with the following snippet:
```sh
npx prisma generate
```

```ts
import { PrismaClient } from '@prisma/client'
import { PrismaLibSQL } from '@prisma/adapter-libsql'
import { createClient } from '@libsql/client'
Install the libSQL database client and Prisma driver adapter for libSQL packages:

const libsql = createClient({
url: `${process.env.TURSO_DATABASE_URL}`,
authToken: `${process.env.TURSO_AUTH_TOKEN}`,
})
```sh
npm install @libsql/client @prisma/adapter-libsql
```

const adapter = new PrismaLibSQL(libsql)
const prisma = new PrismaClient({ adapter })
```
Update your Prisma Client instance:

```ts
import { PrismaClient } from '@prisma/client'
import { PrismaLibSQL } from '@prisma/adapter-libsql'
import { createClient } from '@libsql/client'

const libsql = createClient({
url: `${process.env.TURSO_DATABASE_URL}`,
authToken: `${process.env.TURSO_AUTH_TOKEN}`,
})

const adapter = new PrismaLibSQL(libsql)
const prisma = new PrismaClient({ adapter })
```

You can use Prisma Client as you normally would with full type-safety in your project.

Expand All @@ -137,7 +156,7 @@ To update your database schema:
turso db shell turso-prisma-db < ./prisma/migrations/20230922132717_init/migration.sql # Replace `20230922132717_init` with the existing migration
```

This workflow does not support tracking the history of applied migrations.
For subsequent migrations, repeat the above steps to apply changes to your database. This workflow does not support tracking the history of applied migrations.

## How to connect and query from an embedded replica

Expand Down

0 comments on commit 37170ba

Please sign in to comment.