diff --git a/content/200-orm/050-overview/500-databases/200-database-drivers.mdx b/content/200-orm/050-overview/500-databases/200-database-drivers.mdx index 4e03ada414..0023e0c7f1 100644 --- a/content/200-orm/050-overview/500-databases/200-database-drivers.mdx +++ b/content/200-orm/050-overview/500-databases/200-database-drivers.mdx @@ -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 diff --git a/content/200-orm/050-overview/500-databases/500-sqlite.mdx b/content/200-orm/050-overview/500-databases/500-sqlite.mdx index 6218a8b124..8aa0de08c8 100644 --- a/content/200-orm/050-overview/500-databases/500-sqlite.mdx +++ b/content/200-orm/050-overview/500-databases/500-sqlite.mdx @@ -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: