Part 97: Defining Data Models in Prisma for Your Next.js Application

[App] Database Integration with Prisma

[App] Database Integration with Prisma

In our previous post, we set up Prisma in our Next.js application to facilitate database interactions. Now, it's time to define a "model" that describes the kind of data we want to store in our database. A model in Prisma is akin to defining a class in JavaScript or an interface in TypeScript.

Understanding the Prisma Model

A model in Prisma is a blueprint for a database table. It specifies the fields (or columns) of the table and their data types. Let's define a "Comment" model to store user comments in our application.

Defining a Model

First, let's open the schema.prisma file and remove any unnecessary comments. We'll start by defining the "Comment" model with its properties:

// File: /path/to/your/project/prisma/schema.prisma

model Comment {
  id       String   @id @default(uuid())
  slug     String   // same as Review.slug in Strapi
  user     String
  message  String
  postedAt DateTime @default(now())
}

Understanding the Model Fields

  • id: This is the primary key for the table. It's a String field, and we use the @id directive to mark it as the identifier. The @default(uuid()) directive generates a unique UUID for each new entry.

  • slug: This field stores a string that identifies the review page associated with the comment. It should match the "Review.slug" from our CMS (e.g., Strapi).

  • user: A string field that captures the name of the user who posted the comment.

  • message: This field stores the actual comment text.

  • postedAt: A DateTime field that records when the comment was posted. The @default(now()) directive sets this field to the current date and time when a comment is added.

Applying the Schema to the Database

To implement the model in the database, we can use the Prisma CLI. While developing locally, we use the db push command to apply the schema directly to the database.

# Apply the schema to the database
npx prisma db push

File: /path/to/your/project

This command creates an SQLite database file (dev.db) and generates the Prisma Client, which is a library for querying the database.

Managing the Database File

The dev.db file contains your database data, so it should not be committed to your version control. To ensure this, add it to your .gitignore file:

# File: /path/to/your/project/.gitignore

/prisma/*.db

Exploring the Database

To inspect the SQLite database, you can use the sqlite3 command-line tool. Here's a quick guide on how to install it based on your operating system:

  • macOS: Use Homebrew with brew install sqlite.

  • Windows: Use Chocolatey with choco install sqlite.

  • Linux: Use apt with sudo apt install sqlite3.

Once installed, you can open the database and view the schema with:

# Open the SQLite database
sqlite3 prisma/dev.db

# Show the SQL schema
.schema

File: /path/to/your/project/prisma/dev.db

This will display the SQL commands used to create the "Comment" table, confirming that Prisma generated the correct structure based on your model.

Installing the SQLite Viewer Extension

To make your database exploration more intuitive, you can install the SQLite Viewer extension for Visual Studio Code. This tool allows you to visually inspect your SQLite databases, making it easier to understand the structure and data without entering SQL commands manually.

How to Install SQLite Viewer

  1. Open Visual Studio Code: Launch your VS Code editor.

  2. Access Extensions Marketplace: Click on the Extensions icon on the sidebar or press Ctrl+Shift+X to open the Extensions view.

  3. Search for SQLite Viewer: In the search bar, type "SQLite Viewer" to find the extension. There are several options available, but any basic viewer should suffice for simple database inspection.

  4. Install the Extension: Click on the "Install" button to add the extension to your Visual Studio Code environment.

Using SQLite Viewer

Once you've installed the extension, you can open your SQLite database file directly in Visual Studio Code:

  1. Open the Database: Navigate to the dev.db file in your project directory. Right-click on the file and choose "Open with SQLite Viewer" from the context menu. If prompted with "Open Anyway," confirm your choice.

  2. View the Database Structure: The SQLite Viewer will display the tables and their columns, providing a clear overview of your data model. In our case, you’ll see the "Comment" table with its columns: id, slug, user, message, and postedAt.

  3. Inspect Data: Although this viewer primarily allows you to view the database structure, it can also show any data you've inserted. This is particularly useful for debugging and ensuring your model is correctly applied.

Limitations and Tips

  • Read-Only Access: Note that many SQLite Viewer extensions are read-only, which means you cannot modify the data directly through the viewer. For data manipulation, you'll still need to use SQL commands or Prisma Client.

  • Other Tools: If you find the SQLite Viewer extension lacking, there are other database management tools available that might offer more features, such as DB Browser for SQLite.

Conclusion

With the "Comment" model defined and applied to the database, you're now ready to interact with your data using Prisma's powerful querying capabilities. In our next exploration, we'll delve into using the Prisma Client to perform database operations such as inserting, querying, and updating data.

By following these steps, you now have a robust setup for defining and managing your data models in Prisma, streamlining database interactions in your Next.js application.

Last updated