Prisma Create Data Model

Marickian
By -
0
Setting Up Prisma Connection and Data Model

Setting Up Prisma Connection and Data Model

In this guide, we’ll walk through setting up a data model in Prisma, connecting it to a database (such as Planetscale), and configuring important fields such as timestamps and unique constraints.

1. Defining a Data Model

The first step is to define your model in the schema.prisma file. This defines the structure of the data you want to store. Let's create a simple model for demonstration purposes.

model Entity {
  id          String   @id @default(uuid())  // Primary key, generated as UUID
  createdAt   DateTime @default(now())       // Automatically set timestamp when record is created
  updatedAt   DateTime @updatedAt            // Automatically updated whenever the record is changed
  name        String
  description String   @db.Text              // Field for longer text content
  metadata    Json                           // Stores structured JSON data
  @@unique([name])                           // Ensures the name is unique
}

ID: The id field serves as the primary key and is automatically generated as a UUID.
CreatedAt and UpdatedAt: These fields manage timestamps, automatically recording when a record is created or modified.
Name: A basic string field for a name.
Description: The description field is set to store larger text values using the @db.Text annotation.
Metadata: A Json field that can hold structured data in JSON format.
Unique Constraint: A unique constraint ensures that no two records can have the same name.

2. Applying Unique Constraints

Unique constraints prevent duplicate records. In the example above, we’ve applied a unique constraint on the name field to ensure that each Entity has a distinct name.

@@unique([name])

3. Pushing Changes to the Database

Once your model is defined, you’ll need to push the changes to your database. This syncs the model with your database schema.

  • Stop the Development Server: Before pushing changes, ensure your dev server is stopped.
  • Push Schema Changes: Use the following command to apply the changes to your database:
    npx prisma db push
    This command will update your database schema with the latest changes defined in schema.prisma.

4. Using Prisma Studio

Prisma Studio is a web-based tool that allows you to visually inspect and interact with your database. It’s a great tool for managing data directly from a browser.

To open Prisma Studio, run the following command:

npx prisma studio

Prisma Studio will open in your browser, where you can view the Entity model and any other models you’ve defined. You can manually add, edit, or delete records from here.

5. Handling Data in API Mutations

When creating new records through your API, you may need to check for the existence of an entity before proceeding with a mutation. Since we’ve applied a unique constraint to the name field, Prisma will automatically throw an error if you attempt to create a record with a duplicate name.

This is useful for ensuring data integrity, especially when your API handles user input or external data sources. By leveraging unique constraints, you can avoid duplicate records and ensure that your data remains consistent.

Post a Comment

0Comments

Post a Comment (0)