Tips for dividing multiple schema files

It's amazing that Prisma now supports multiple schema files. But I have trouble splitting up my schemas and deciding what belongs together. For example, would you put all of these Auth.js related schemas into one file? Or would you make a separate file for each of them?
model User {
id String @id @default(cuid())
name String?
email String @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Account {
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@id([provider, providerAccountId])
}

model Session {
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model VerificationToken {
identifier String
token String
expires DateTime

@@id([identifier, token])
}
model User {
id String @id @default(cuid())
name String?
email String @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Account {
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@id([provider, providerAccountId])
}

model Session {
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model VerificationToken {
identifier String
token String
expires DateTime

@@id([identifier, token])
}
1 Reply
Prisma AI Help
Skipping coffee for instant knowledge! Let's brew that AI answer now. Thank you for your question about organizing multiple schema files in Prisma. I'm happy to provide some guidance based on the information available in the knowledge sources. Prisma introduced support for multiple schema files in version 5.15.0 as a preview feature. Here are some tips for dividing your schemas effectively: 1. Organize by domain: Group related models into the same file. For example, keep all user-related models in one file and post-related models in another. 2. Use clear naming conventions: Name your schema files clearly and succinctly, like user.prisma or auth.prisma. 3. Avoid "kitchen sink" files: Try not to create files that contain a mix of unrelated or loosely related components. 4. Have a main schema file: Keep a single file (e.g., schema.prisma or main.prisma) for defining datasource and generator blocks. Prisma Docs: Multi-file Prisma Schema For your specific example with Auth.js related schemas, you have a few options: 1. Single file approach: Since all these models (User, Account, Session, and VerificationToken) are closely related to authentication, you could keep them in a single file, perhaps named auth.prisma. 2. Separate files approach: If you prefer more granular organization, you could split them into separate files: - user.prisma for the User model - account.prisma for the Account model - session.prisma for the Session model - verification.prisma for the VerificationToken model The choice between these approaches depends on your project's size, team preferences, and how closely coupled these models are in your application logic. Given that these models are all part of the authentication system and have relationships with each other, the single file approach (auth.prisma) might be more suitable in this case. This keeps related models together, making it easier to understand and mana ge the authentication schema as a whole. Remember, the goal is to make your schema more manageable and easier to navigate. Choose the approach that best suits your project's needs and your team's workflow. ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?