Skip to content

[6.6.0 ]Docs for SQLite driver adapter #6770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ There are two different types of driver adapters:
You can connect to your database using a Node.js-based driver from Prisma Client using a database driver adapter. Prisma maintains the following database driver adapters:

- [PostgreSQL](/orm/overview/databases/postgresql#using-the-node-postgres-driver)
- [SQLite](/orm/overview/databases/postgresql#using-the-better-sqlite3-driver)
- [Turso](/orm/overview/databases/turso#how-to-connect-and-query-a-turso-database)

### Serverless driver adapters
Expand Down
59 changes: 59 additions & 0 deletions content/200-orm/050-overview/500-databases/500-sqlite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,65 @@ The fields passed to the `datasource` block are:
- `provider`: Specifies the `sqlite` data source connector.
- `url`: Specifies the [connection URL](/orm/reference/connection-urls) for the SQLite database. The connection URL always starts with the prefix `file:` and then contains a file path pointing to the SQLite database file. In this case, the file is located in the same directory and called `dev.db`.


## Using the `better-sqlite3` driver

As of [`v5.4.0`](https://github.com/prisma/prisma/releases/tag/5.4.0), you can use Prisma ORM with database drivers from the JavaScript ecosystem (instead of using Prisma ORM's built-in drivers). You can do this by using a [driver adapter](/orm/overview/databases/database-drivers).

For SQLite, [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3/) is one of the most popular drivers in the JavaScript ecosystem. It can be used with any SQLite database file.

This section explains how you can use it with Prisma ORM and the `@prisma/adapter-better-sqlite3` driver adapter.

### 1. Enable the `driverAdapters` Preview feature flag

Since driver adapters are currently in [Preview](/orm/more/releases#preview), you need to enable its feature flag on the `datasource` block in your Prisma schema:

```prisma
// schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}

datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
```

Once you have added the feature flag to your schema, re-generate Prisma Client:

```terminal copy
npx prisma generate
```

### 2. Install the dependencies

Next, install the `pg` package and Prisma ORM's driver adapter:

```terminal copy
npm install better-sqlite3
npm install @prisma/adapter-better-sqlite3
```

### 3. Instantiate Prisma Client using the driver adapter

Finally, when you instantiate Prisma Client, you need to pass an instance of Prisma ORM's driver adapter to the `PrismaClient` constructor:

```ts copy
import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3'
import { PrismaClient } from '@prisma/client'

const url = `${process.env.DATABASE_URL}` // e.g. './dev.db'
const adapterFactory = new PrismaBetterSQLite3({ url, ... }) // uses BetterSQLite3InputParams
const adapter = await adapterFactory.connect()

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

Notice that this code requires the `DATABASE_URL` environment variable to be set to your SQLite connection string (i.e. file path). You can learn more about the connection string below.


## Type mapping between SQLite to Prisma schema

The SQLite connector maps the [scalar types](/orm/prisma-schema/data-model/models#scalar-fields) from the [data model](/orm/prisma-schema/data-model/models) to native column types as follows:
Expand Down
Loading