OverviewDatabases

SQLite

This page explains how Prisma can connect to a SQLite database using the SQLite database connector.

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.

Example

To connect to a SQLite database file, you need to configure a datasource block in your Prisma schema:

schema.prisma
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:

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.

Using the better-sqlite3 driver

As 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.

1. Install the dependencies

First, install Prisma ORM's driver adapter for better-sqlite3:

npm install @prisma/adapter-better-sqlite3

2. Instantiate Prisma Client using the driver adapter

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 });

3. Configure timestamp format for backward compatibility

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 });

When to use each format:

  • ISO 8601 (default): Best for new projects and integrates well with SQLite's built-in date/time functions.
  • unixepoch-ms: Required when migrating from Prisma ORM's native SQLite driver to maintain compatibility with existing timestamp data.

Type mapping between SQLite to Prisma schema

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.

Native type mapping from Prisma ORM to SQLite

Prisma ORMSQLite
StringTEXT
BooleanBOOLEAN
IntINTEGER
BigIntINTEGER
FloatREAL
DecimalDECIMAL
DateTimeNUMERIC
JsonJSONB
BytesBLOB
EnumTEXT

Rounding errors on big numbers

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

Connection details

Connection URL

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:

prisma.config.ts
datasource: {
  url: 'file:./dev.db',
}

is the same as:

prisma.config.ts
datasource: {
  url: 'file:dev.db',
}

You can also target files from the root or any other place in your file system:

prisma.config.ts
datasource: {
  url: 'file:/Users/janedoe/dev.db',
}

On this page