apps/docs/content/docs.v6/orm/overview/databases/sqlite.mdx
The SQLite data source connector connects Prisma ORM to a SQLite database file. These files always have the file ending .db (e.g.: dev.db).
By default, the SQLite connector contains a database driver responsible for connecting to your database. You can use a driver adapter (Preview) to connect to your database using a JavaScript database driver from Prisma Client.
To connect to a SQLite database file, you need to configure a datasource block in your Prisma schema:
datasource db {
provider = "sqlite"
}
The datasource block specifies the sqlite data source connector.
In Prisma ORM 7, the database connection URL is configured in prisma.config.ts:
import { defineConfig } from "prisma/config";
export default defineConfig({
schema: "prisma/schema.prisma",
datasource: {
url: "file:./dev.db",
},
});
The connection URL always starts with the prefix file: and then contains a file path pointing to the SQLite database file. In this example, the file is located in the same directory and called dev.db.
better-sqlite3 driverAs of v5.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.
For SQLite, better-sqlite3 is one of the most popular drivers in the JavaScript ecosystem.
This section explains how you can use it with Prisma ORM and the @prisma/adapter-better-sqlite3 driver adapter.
First, install Prisma ORM's driver adapter for better-sqlite3:
npm install @prisma/adapter-better-sqlite3
Now, when you instantiate Prisma Client, you need to pass an instance of Prisma ORM's driver adapter to the PrismaClient constructor:
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
import { PrismaClient } from "./generated/prisma";
const adapter = new PrismaBetterSqlite3({
url: "file:./prisma/dev.db",
});
const prisma = new PrismaClient({ adapter });
:::tip[Using SQLite with Bun]
When running Prisma Client on Bun, use the @prisma/adapter-libsql driver adapter. Bun doesn't support the native SQLite driver that better-sqlite3 relies on (see the node:sqlite reference). Instantiate the adapter and pass it to PrismaClient:
import { PrismaClient } from "../prisma/generated/client";
import { PrismaLibSql } from "@prisma/adapter-libsql";
const adapter = new PrismaLibSql({
url: process.env.DATABASE_URL ?? "",
});
const prisma = new PrismaClient({ adapter });
export default prisma;
:::
When using driver adapters with SQLite, you can configure how DateTime values are stored in the database using the timestampFormat option.
By default, driver adapters store DateTime values as ISO 8601 strings, which is the most convenient format for SQLite since SQLite date/time functions expect ISO 8601 by default.
However, if you need 100% backward compatibility with Prisma ORM's native SQLite driver (for example, when migrating an existing database), you should use the unixepoch-ms format, which stores timestamps as the number of milliseconds since the Unix epoch:
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
import { PrismaClient } from "./generated/prisma";
const adapter = new PrismaBetterSqlite3(
{
url: "file:./prisma/dev.db",
},
{
timestampFormat: "unixepoch-ms",
},
);
const prisma = new PrismaClient({ adapter });
:::info
The timestampFormat option is available for both @prisma/adapter-better-sqlite3 and @prisma/adapter-libsql driver adapters.
:::
When to use each format:
unixepoch-ms: Required when migrating from Prisma ORM's native SQLite driver to maintain compatibility with existing timestamp data.The SQLite connector maps the scalar types from the data model to native column types as follows:
Alternatively, see Prisma schema reference for type mappings organized by Prisma ORM type.
| Prisma ORM | SQLite |
|---|---|
String | TEXT |
Boolean | BOOLEAN |
Int | INTEGER |
BigInt | INTEGER |
Float | REAL |
Decimal | DECIMAL |
DateTime | NUMERIC |
Json | JSONB |
Bytes | BLOB |
Enum | TEXT |
:::note
SQLite doesn't have a dedicated Boolean type. While this table shows BOOLEAN, columns are assigned a NUMERIC affinity (storing 0 for false and 1 for true). Learn more.
:::
:::warning
When using enum fields in SQLite, be aware of the following:
:::
SQLite is a loosely-typed database. If your Schema has a field of type Int, then Prisma ORM prevents you from inserting a value larger than an integer. However, nothing prevents the database from directly accepting a bigger number. These manually-inserted big numbers cause rounding errors when queried.
To avoid this problem, Prisma ORM 4.0.0 and later checks numbers on the way out of the database to verify that they fit within the boundaries of an integer. If a number does not fit, then Prisma ORM throws a P2023 error, such as:
Inconsistent column data: Conversion failed:
Value 9223372036854775807 does not fit in an INT column,
try migrating the 'int' column type to BIGINT
The connection URL of a SQLite connector points to a file on your file system and is configured in prisma.config.ts. For example, the following two paths are equivalent because the .db is in the same directory:
datasource: {
url: 'file:./dev.db',
}
is the same as:
datasource: {
url: 'file:dev.db',
}
You can also target files from the root or any other place in your file system:
datasource: {
url: 'file:/Users/janedoe/dev.db',
}