Docs
Models

Models

Understand how to use models in your application.

Models are the core of your application.

They are the data layer of your application and are responsible for managing the data of the application.

Models are used to manage the rules of interaction with a corresponding database table.

Models are typically stored in the app/models directory. They are based on the Prism ORM.

They are derived from the schema of your application.

By default, SaaSKits provides

  • a User model
  • a Plan model
  • a Subscription model
  • a Checkout model

Creating a Model

To create a model, you can start by adding new model to the schema.prisma file.

 
model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  Int?
}
 

Then you can run the following command to generate the model.

prisma run generate

If you are using VS Code, reload the window to see the new model.

Using a Model

You can use a model in your application by creating new file in the app/models directory.

app/models/post.ts
import { prisma } from "@/services/db/db.server"
import type { Post, Prisma } from "@prisma/client"
 
 
export const getAllPosts = async () => {
  return await prisma.post.findMany({
    where: { isActive: true },
    include: { prices: true },
  })
}
 

Prisma will automatically generate the Post type for you.

Once you have created the model, you can use it in your application.

You can add more methods to the model to perform CRUD operations.

Since, the model is based on the schema, you can use the schema to add more fields to the model.

Though, very minimal, the schema is very powerful and can be used to create complex models.