Prisma SQLite Setup

Initial Setup

Setting up Prisma with SQLite involves a simplified process since SQLite databases are file-based. This setup includes installing the Prisma CLI, initializing Prisma in your project, and configuring your schema.prisma file to use the SQLite database. Follow these steps for a correct setup.

Configuration Steps

  1. Configure your schema.prisma file for SQLite.
  2. Update the .env file with your SQLite file path, ensuring it is correctly specified.
  3. Create and apply migrations as necessary.
  4. Generate Prisma Classes npx php generate class

schema.prisma Configuration

The /prisma/schema.prisma file is the core configuration file for your Prisma project. It defines your data model, data sources, and generators. To configure Prisma to use SQLite, update your /prisma/schema.prisma file as follows:

  generator client {
        provider = "prisma-client-js"
    }

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

This configuration points Prisma to use an SQLite database by specifying the provider as 'sqlite' and the url as the location of your SQLite file within the .env file.

.env Configuration

DATABASE_URL="file:./db/dev.db"

NOTE: The path specified in the DATABASE_URL variable should point to the location of your SQLite database file. The current path is set to /prisma/db/dev.db, which means that the SQLite database file is located in the /prisma/db directory of your project. Replace the path with the actual path to your SQLite database file. The path can be relative or absolute depending on your project's structure.

If you want to create the SQLite database file in a different directory, you can specify the path as follows:

DATABASE_URL="file:../path/to/your/db/dev.db"

NOTE: The path specified in the DATABASE_URL variable should point to the location of your SQLite database file. If you notice ../ in the path, it means that the path is going out of the prisma directory. You can specify the path according to your project's structure.

Conclusion

You've now learned how to integrate SQLite with Prisma, configure your project, and seed your database with initial data. This setup offers a streamlined, efficient approach to working with databases in development, leveraging Prisma's ORM capabilities for data management and operations.

Next Step

Go to Prisma Command to migrate your database.