Extend Existing Models

Learn how you can leverage the Entity Builder while using your own database model.

  • Alexandro MartΓ­nez
    Author
    by Alexandro MartΓ­nez
    2 years ago
  • Goals:

    • Add a model at schema.prisma.

    • Extend it with the Custom Entity Builder.

    Steps

    πŸ’Ώ First, open the schema.prisma file and add the following model:

    model JobPost {
      id     String  @id @default(cuid())
      rowId  String  @unique
      row    Row     @relation(fields: [rowId], references: [id], onDelete: Cascade)
      title  String
      type   String // Fixed - Hourly
      budget Decimal
    }
    

    πŸ’Ώ On the Row model, add 2 properties:

    • jobPostId - Nullable String? (not all Rows will be job posts)

    • jobPost - Nullable JobPost?

    model Row {
      id                String         @id @default(cuid())
      ...
      jobPostId         String?
      jobPost           JobPost?
    }
    

    This property name will be important, so take note if you have jobPost or JobPost.

    πŸ’Ώ Add a migration to update the database:

    npx prisma migrate dev --name added_job_post_model
    

    πŸ’Ώ Start your app, and add the JobPost Custom Entity.

    WARNING

    The name value should be the same as the one on the Row model (jobPost or JobPost).

    Job Post Entity

    πŸ’Ώ For each field, click on Show advanced options and set Is dynamic to false.

    Job Post Property is not Dynamic

    You should have at least these 3 properties:

    Job Post Properties

    πŸ’Ώ Add a Job Post.

    New Job Post

    There will be an error, since we're trying to access the properties for an object that is not included on the database query (e.g. row.jobPost.title).

    Job Post Details Error

    πŸ’Ώ Add the jobPost property to the includeRowDetails constant:

    export const includeRowDetails = {
    + jobPost: true,
      createdByUser: true,
    

    πŸ’Ώ Reload the job post.

    Job Post

    πŸ’Ώ List all the Job Posts.

    Job Posts

    πŸ’Ώ Update all the fields.

    Job Post Updated

    πŸ’Ώ Open a new terminal window an run prisma studio to browser the JobPosts model/table rows.

    npx prisma studio
    
    Job Post Prisma Studio Rows

    πŸ’Ώ Finally, delete the job post.

    Job Post Deleted

    I hope this quick guide was useful! Let me know if you have any questions.

    We respect your privacy.

    TLDR: We use cookies for language selection, theme, and analytics. Learn more.