Skip to content

Prismalint Showcase #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .prismalintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"rules": {
"field-name-mapping-snake-case": [
"error",
{
"compoundWords": ["S3"]
}
],
"field-order": [
"error",
{
"order": ["tenantId", "..."]
}
],
"forbid-required-ignored-field": ["error"],
"model-name-grammatical-number": [
"error",
{
"style": "singular"
}
],
"model-name-mapping-snake-case": [
"error",
{
"compoundWords": ["GraphQL"]
}
],
"require-field-index": [
"error",
{
"forAllRelations": true,
"forNames": ["tenantId"]
}
]
}
}
35 changes: 35 additions & 0 deletions example.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
generator client {
provider = "prisma-client-js"
output = "../generated/prisma/client"
}

datasource db {
provider = "postgresql"
url = "fake-url"
}
Comment on lines +6 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Database URL must come from environment variable

Hard-coding a placeholder string will break migrations & deployments. Prisma recommends env("DATABASE_URL") to pick up per-environment credentials.

-datasource db {
-  provider = "postgresql"
-  url      = "fake-url"
-}
+datasource db {
+  provider = "postgresql"
+  url      = env("DATABASE_URL")
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
datasource db {
provider = "postgresql"
url = "fake-url"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
🤖 Prompt for AI Agents
In example.prisma around lines 6 to 9, the datasource URL is hard-coded as a
placeholder string, which will cause issues with migrations and deployments.
Replace the hard-coded URL with an environment variable reference by using
env("DATABASE_URL") to ensure the database URL is dynamically loaded from the
environment configuration.


model Users {
id String @id
emailAddress String
tenantId String
removeMe String @ignore
tenant Tenant @relation(fields: [tenantId], references: [id])
@@map(name: "users")
}
Comment on lines +11 to +18
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Users model violates several lint & modelling best-practices

• Model name is plural – linter rule expects singular.
id lacks a default; every insert must supply it manually.
emailAddress probably needs @unique.
tenantId should be indexed for FK look-ups.
removeMe is both required and @ignore, which the linter flags; simply remove it.

-model Users {
-  id           String @id
-  emailAddress String
-  tenantId     String
-  removeMe     String @ignore
-  tenant       Tenant @relation(fields: [tenantId], references: [id])
-  @@map(name: "users")
-}
+model User {
+  id           String @id @default(uuid())
+  emailAddress String @unique
+  tenantId     String
+  tenant       Tenant @relation(fields: [tenantId], references: [id])
+
+  @@map(name: "users")
+  @@index([tenantId])
+}

Note: Renaming the model is a breaking change; plan a migration accordingly.

🤖 Prompt for AI Agents
In example.prisma around lines 11 to 18, the Users model should be renamed to
singular form to follow naming conventions, add a default value to the id field
(e.g., use @default(uuid()) for automatic ID generation), add @unique to
emailAddress to enforce uniqueness, add an index to tenantId for efficient
foreign key lookups, and remove the removeMe field entirely since it is both
required and ignored, which is invalid. Plan a migration to handle the model
renaming as it is a breaking change.


model Tenant {
id String @id
name String
@@map(name: "tenant")
}

model UserRoleFoo {
id String @id
@@map(name: "unexpected_snake_case")
}

model UserRole {
id String @id
userId String @map(name: "userid")
// No mapping.
}
Comment on lines +31 to +35
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

UserRole missing relation & table mapping

userId is scalar-only; without a relation you lose referential integrity. Also, table name is implicit – add @@map for consistency.

 model UserRole {
   id         String @id
-  userId     String @map(name: "userid")
-  // No mapping.
+  userId     String @map(name: "userid")
+  user       User  @relation(fields: [userId], references: [id])
+
+  @@map(name: "user_role")
+  @@index([userId])
 }
🤖 Prompt for AI Agents
In example.prisma around lines 31 to 35, the UserRole model's userId field lacks
a relation definition, which means referential integrity is not enforced. Add a
relation attribute to userId linking it to the User model's id field. Also, add
an @@map attribute at the model level to explicitly specify the database table
name for consistency and clarity.