From d81b2bec099a90e83dcc23924ed9b830973fd219 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Thu, 27 Mar 2025 09:17:57 +0100 Subject: [PATCH 1/3] add docs for better-sqlite3 driver adapter --- .../500-databases/200-database-drivers.mdx | 1 + .../050-overview/500-databases/500-sqlite.mdx | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+) 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..6cd514d58f 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-node-postgres-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..d9d9d4fd0b 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 { tbd } from '@prisma/adapter-better-sqlite3' +import { PrismaClient } from '@prisma/client' + +const connectionString = `${process.env.DATABASE_URL}` + +// tbd +const adapter = new tbd +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: From d3959ea47bce87b2afaed085b4a9f9b8561cf5ba Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Thu, 27 Mar 2025 09:25:13 +0100 Subject: [PATCH 2/3] update usage of sqlite driver adapter --- content/200-orm/050-overview/500-databases/500-sqlite.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 d9d9d4fd0b..8aa0de08c8 100644 --- a/content/200-orm/050-overview/500-databases/500-sqlite.mdx +++ b/content/200-orm/050-overview/500-databases/500-sqlite.mdx @@ -71,13 +71,13 @@ npm install @prisma/adapter-better-sqlite3 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 { tbd } from '@prisma/adapter-better-sqlite3' +import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3' import { PrismaClient } from '@prisma/client' -const connectionString = `${process.env.DATABASE_URL}` +const url = `${process.env.DATABASE_URL}` // e.g. './dev.db' +const adapterFactory = new PrismaBetterSQLite3({ url, ... }) // uses BetterSQLite3InputParams +const adapter = await adapterFactory.connect() -// tbd -const adapter = new tbd const prisma = new PrismaClient({ adapter }) ``` From 6dd61259f17d7f497e56ade4b2a96599495d8e78 Mon Sep 17 00:00:00 2001 From: Nikolas Burk Date: Thu, 27 Mar 2025 09:28:15 +0100 Subject: [PATCH 3/3] fix typo --- .../200-orm/050-overview/500-databases/200-database-drivers.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6cd514d58f..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,7 +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-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