Skip to content

Preview/prismalint #39

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Preview/prismalint #39

wants to merge 3 commits into from

Conversation

alexcoderabbitai
Copy link

@alexcoderabbitai alexcoderabbitai commented Jul 15, 2025

image

Summary by CodeRabbit

  • New Features
    • Introduced linting rules for Prisma schema files to enforce consistent naming, field ordering, and indexing conventions.
    • Added a sample Prisma schema with models for users, tenants, and user roles, demonstrating various naming and mapping scenarios.

Copy link

coderabbitai bot commented Jul 15, 2025

Walkthrough

A new Prisma schema file and a Prisma linter configuration file are introduced. The schema defines four models with specific fields, relations, and database mappings, while the linter configuration enforces naming, ordering, and indexing conventions for Prisma models and fields. No existing code entities are modified.

Changes

File(s) Change Summary
.prismalintrc.json Added Prisma linter configuration enforcing naming, ordering, and indexing rules for schema files.
example.prisma Added Prisma schema defining four models, a PostgreSQL datasource, and a client generator with field and table mappings.

Poem

In fields and models, rules now reign,
With snake_case names and order plain.
Tenants come first, as linting decrees,
Indices on fields, all done with ease.
Prisma’s garden grows neat and bright—
A bunny’s delight in schema’s light! 🐇

Impact Analysis

Introduction of strict Prisma schema linting rules

📢 Medium 🔄 Impacts behavior

A new .prismalintrc.json file enforces strict linting rules for Prisma schema files, including naming conventions, field ordering, required indexing, and grammatical number for model names. Developers working with Prisma schemas will now encounter errors or warnings if their schema files do not comply with these conventions, potentially requiring changes to existing or future schema files to pass linting.

Test schema files with both compliant and non-compliant patterns to verify that linting errors are triggered as expected. Validate that exceptions (e.g., "S3" and "GraphQL") are handled correctly and that the developer experience remains smooth.

🔍 Related Files
  • .prismalintrc.json

Addition of new Prisma schema models and database mappings

📢 Medium 🔄 Impacts behavior

A new Prisma schema introduces four models (Users, Tenant, UserRoleFoo, and UserRole) with specific field definitions, relations, and database table mappings. This enables new data structures and relationships for applications using this schema, allowing developers to generate a Prisma client for interacting with these models in a PostgreSQL database.

Generate the Prisma client and test CRUD operations for each model. Verify that relations, field mappings, and ignored fields behave as expected, and that the generated client interacts correctly with the underlying database.

🔍 Related Files
  • example.prisma

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (2)
.prismalintrc.json (2)

28-34: Consider extending the index rule to cover composite tenant scopes

require-field-index currently safeguards one-column tenantId look-ups.
If your multi-tenant tables frequently use composite PKs/UKs that also include tenantId, add
"coverComposite": true (supported since prismalint 0.8) so the rule also flags @@id([id, tenantId]) without a matching secondary index.


35-36: Missing terminal newline

Posix tools expect a trailing newline; most editors warn when it’s absent.
Add one to avoid diff churn.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ef04b8d and 561dc6c.

📒 Files selected for processing (2)
  • .prismalintrc.json (1 hunks)
  • example.prisma (1 hunks)
🔇 Additional comments (2)
.prismalintrc.json (1)

3-8: Confirm rule key spelling matches prismalint’s spec

If the project is using @prisma/lint-pkg v0.6+ the option key is compoundWords, but earlier releases expect compoundWord. A typo silently disables the whitelist. Double-check the linter version so "compoundWords" is actually respected.

example.prisma (1)

26-29: Model name mapping suggests accidental snake_case in DB

UserRoleFoo maps to unexpected_snake_case, which is already snake_case.
If the intention is to diverge from the default mapping to satisfy an existing table, that’s fine.
If not, you can delete @@map and let Prisma generate user_role_foo.

id String @id
emailAddress String
tenantId String
removeMe String @ignore
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ignored field is required

removeMe is String (required by default). Either mark it optional (String?) or drop it; otherwise it blocks deployments despite @ignore.

🤖 Prompt for AI Agents
In example.prisma at line 15, the field `removeMe` is marked with `@ignore` but
is defined as a required `String`, which causes deployment issues. To fix this,
either make the field optional by changing its type to `String?` or remove the
field entirely from the model to avoid blocking deployments.

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

Choose a reason for hiding this comment

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

⚠️ Potential issue

Datasource URL should come from env var

Hard-coding "fake-url" prevents local overrides and breaks prisma generate in CI/CD.
Typical pattern:

-  url      = "fake-url"
+  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
"fake-url", which prevents local environment overrides and causes issues in
CI/CD pipelines. Replace the hard-coded URL with an environment variable
reference using the syntax env("DATABASE_URL") to allow dynamic configuration
based on the deployment environment.

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

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

UserRole lacks relation to User

userId is declared but no @relation or @@index([userId]) is present.
With require-field-index set to forAllRelations: true this will fail linting once a relation is added. Recommend:

 model UserRole {
   id         String @id
-  userId     String @map(name: "userid")
+  userId     String @map(name: "userid")
+  user       User   @relation(fields: [userId], references: [id])
+
+  @@index([userId])
 }
📝 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
model UserRole {
id String @id
userId String @map(name: "userid")
// No mapping.
}
model UserRole {
id String @id
userId String @map(name: "userid")
user User @relation(fields: [userId], references: [id])
@@index([userId])
}
🤖 Prompt for AI Agents
In example.prisma around lines 31 to 35, the UserRole model defines a userId
field without a relation or index. To fix this, add a proper @relation attribute
linking userId to the User model's id field and include an @@index on userId to
satisfy the require-field-index linting rule. This ensures the relation is
explicit and indexed for performance and linting compliance.

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

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Users violates four freshly-added lint rules

  1. Model name is plural (Users) but model-name-grammatical-number demands singular.
  2. Required removeMe field is marked @ignore, violating forbid-required-ignored-field.
  3. tenantId must be the first field per field-order.
  4. tenantId lacks the mandatory @@index([tenantId]).

A minimal conforming refactor:

-model Users {
-  id           String @id
-  emailAddress String
-  tenantId     String
-  removeMe     String @ignore
-  tenant       Tenant @relation(fields: [tenantId], references: [id])
-  @@map(name: "users")
-}
+model User {
+  tenantId     String
+  id           String @id
+  emailAddress String
+  // removeMe removed – ignored required fields not allowed
+  tenant       Tenant @relation(fields: [tenantId], references: [id])
+
+  @@index([tenantId])
+  @@map(name: "users")
+}
📝 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
model Users {
id String @id
emailAddress String
tenantId String
removeMe String @ignore
tenant Tenant @relation(fields: [tenantId], references: [id])
@@map(name: "users")
}
model User {
tenantId String
id String @id
emailAddress String
// removeMe removed – ignored required fields not allowed
tenant Tenant @relation(fields: [tenantId], references: [id])
@@index([tenantId])
@@map(name: "users")
}
🤖 Prompt for AI Agents
In example.prisma around lines 11 to 18, fix the Users model by renaming it to
singular form (User), remove the required field 'removeMe' or make it optional
instead of using @ignore, reorder fields so that 'tenantId' is the first field,
and add an @@index([tenantId]) attribute to index the tenantId field as required
by the lint rules.

@alexcoderabbitai
Copy link
Author

@coderabbitai full review

Copy link

coderabbitai bot commented Jul 15, 2025

✅ Actions performed

Full review triggered.

@alexcoderabbitai
Copy link
Author

@coderabbitai full review

Copy link

coderabbitai bot commented Jul 15, 2025

✅ Actions performed

Full review triggered.

@alexcoderabbitai
Copy link
Author

@coderabbitai full review

Copy link

coderabbitai bot commented Jul 16, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (3)
example.prisma (3)

11-18: Users model violates four enabled lint rules
Duplicate of an existing bot comment – still unresolved.

  1. Model name should be singular.
  2. tenantId must be the first field.
  3. Required + @ignore field removeMe is forbidden.
  4. Missing @@index([tenantId]).

Minimal compliant refactor:

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

31-35: Add relation, index, and snake-case mapping for UserRole
Earlier bot hint still applies.

 model UserRole {
   id         String @id
-  userId     String @map(name: "userid")
-  // No mapping.
+  userId     String @map(name: "user_id")
+  user       User   @relation(fields: [userId], references: [id])
+
+  @@index([userId])
+  @@map(name: "user_role")
 }

6-9: Datasource URL must be externalised

Hard-coding "fake-url" breaks local overrides and CI/CD secrets injection. Switch to an env var to stay 12-factor compliant.

 datasource db {
   provider = "postgresql"
-  url      = "fake-url"
+  url      = env("DATABASE_URL")
 }
🧹 Nitpick comments (1)
.prismalintrc.json (1)

1-35: Consider documenting the rationale behind rule exceptions

compoundWords arrays (["S3"], ["GraphQL"]) are self-explanatory to us today, but future contributors won’t know why just those tokens are exempt from snake-case. Because JSON doesn’t allow comments, add a short section in CONTRIBUTING.md (or a PRISMA_LINT_RULES.md) that explains each exception and how to request new ones.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ef04b8d and ba7b209.

⛔ Files ignored due to path filters (1)
  • .coderabbit.yaml is excluded by none and included by none
📒 Files selected for processing (2)
  • .prismalintrc.json (1 hunks)
  • example.prisma (1 hunks)
🧰 Additional context used
🪛 PrismaLint (0.10.2)
example.prisma

31-31: Model name must be mapped to snake case. (model-name-mapping-snake-case)


33-33: Field name must be mapped to "user_id". (field-name-mapping-snake-case)


26-26: Model name must be mapped to "user_role_foo". (model-name-mapping-snake-case)


11-11: Fields are not in the expected order: "tenantId", "...". (field-order)


11-11: Expected singular model name. (model-name-grammatical-number)


13-13: Field name must be mapped to snake case. (field-name-mapping-snake-case)


15-15: Field name must be mapped to snake case. (field-name-mapping-snake-case)


15-15: Do not ignore a required field without a default value. (forbid-required-ignored-field)


14-14: Field name must be mapped to snake case. (field-name-mapping-snake-case)


14-14: Field "tenantId" must have an index. (require-field-index)

Comment on lines +26 to +29
model UserRoleFoo {
id String @id
@@map(name: "unexpected_snake_case")
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

UserRoleFoo mapping should follow snake-case convention

Rule model-name-mapping-snake-case expects the map name to be user_role_foo.

 model UserRoleFoo {
   id         String @id
-  @@map(name: "unexpected_snake_case")
+  @@map(name: "user_role_foo")
 }
📝 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
model UserRoleFoo {
id String @id
@@map(name: "unexpected_snake_case")
}
model UserRoleFoo {
id String @id
@@map(name: "user_role_foo")
}
🧰 Tools
🪛 PrismaLint (0.10.2)

26-26: Model name must be mapped to "user_role_foo". (model-name-mapping-snake-case)

🤖 Prompt for AI Agents
In example.prisma around lines 26 to 29, the model UserRoleFoo has a @@map
attribute with the name "unexpected_snake_case" which does not follow the
required snake-case convention. Update the @@map name to "user_role_foo" to
comply with the model-name-mapping-snake-case rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants