Skip to content

perf: Add an explicit DTO + other type improvements to membership repo#22163

Merged
hbjORbj merged 3 commits intomainfrom
perf/prevent-crash-ts-limit-exceeded-explicit-dto
Jul 24, 2025
Merged

perf: Add an explicit DTO + other type improvements to membership repo#22163
hbjORbj merged 3 commits intomainfrom
perf/prevent-crash-ts-limit-exceeded-explicit-dto

Conversation

@emrysal
Copy link
Copy Markdown
Contributor

@emrysal emrysal commented Jun 30, 2025

What does this PR do?

Added an explicit Membership DTO and improved type safety in the membership repository to prevent TypeScript performance issues.

  • Refactors
    • Replaced Prisma.validator usage with satisfies.
    • Simplified select logic and improved type inference for repository methods.
image

Fixes deployment issue introduced in #22057

@graphite-app graphite-app bot requested a review from a team June 30, 2025 14:26
@emrysal emrysal changed the title perf: Add an explicit DTO + some other type improvements to membershi… perf: Add an explicit DTO + other type improvements to membership repo Jun 30, 2025
@vercel
Copy link
Copy Markdown

vercel bot commented Jun 30, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Jul 24, 2025 7:10pm
cal-eu ⬜️ Ignored (Inspect) Visit Preview Jul 24, 2025 7:10pm

@keithwillcode keithwillcode added core area: core, team members only dba foundation labels Jun 30, 2025
@emrysal emrysal requested a review from eunjae-lee June 30, 2025 14:26
@delve-auditor
Copy link
Copy Markdown

delve-auditor bot commented Jun 30, 2025

No security or compliance issues detected. Reviewed everything up to a89a1a1.

Security Overview
  • 🔎 Scanned files: 1 changed file(s)
Detected Code Changes
Change Type Relevant files
Refactor ► events.ts
    Simplified attendee list formatting and data structure
► membership.ts
    Updated type definitions and optimized team membership queries
Configuration changes ► package.json
    Updated version numbers and peer dependencies
► CHANGELOG.md
    Removed test release changelog entry

Reply to this PR with @delve-auditor followed by a description of what change you want and we'll auto-submit a change to this PR to implement it.

@graphite-app
Copy link
Copy Markdown

graphite-app bot commented Jun 30, 2025

Graphite Automations

"Add foundation team as reviewer" took an action on this PR • (06/30/25)

1 reviewer was added to this PR based on Keith Williams's automation.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

cubic found 1 issue across 1 file. Review it in cubic.dev

React with 👍 or 👎 to teach cubic. Tag @cubic-dev-ai to give specific feedback.

import { availabilityUserSelect, prisma, type PrismaTransaction, type PrismaClient } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/client";
import { Prisma } from "@calcom/prisma/client";
import type { Prisma, Membership } from "@calcom/prisma/client";
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now type only (through satisfies)

});
// this is explicit, and typed in TSelect default typings
select: select ?? { userId: true },
})) as unknown as Promise<MembershipDTOFromSelect<TSelect>[]>;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately there is no way to tell prisma.membership.findMany it will be compatible based on the given select (not enough overlap with the inferred types); however, we don't need that as we have direct control over the input and output and it is inferred based on the Prisma generated types.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jun 30, 2025

E2E results are ready!

hbjORbj
hbjORbj previously approved these changes Jul 3, 2025
Copy link
Copy Markdown
Contributor

@hbjORbj hbjORbj left a comment

Choose a reason for hiding this comment

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

Love the changes. Let's get this in

@hbjORbj hbjORbj enabled auto-merge (squash) July 3, 2025 15:23
const teamParentSelect = Prisma.validator<Prisma.TeamSelect>()({
type MembershipSelectableKeys = keyof typeof membershipSelect;

type MembershipPartialSelect = Partial<Record<MembershipSelectableKeys, boolean>>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

smart!

eunjae-lee
eunjae-lee previously approved these changes Jul 4, 2025
Copy link
Copy Markdown
Contributor

@eunjae-lee eunjae-lee left a comment

Choose a reason for hiding this comment

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

Thanks for tagging me. The changes look very good.

@github-actions
Copy link
Copy Markdown
Contributor

This PR is being marked as stale due to inactivity.

@github-actions github-actions bot added the Stale label Jul 19, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 19, 2025

Walkthrough

The changes refactor TypeScript type usage and query construction in the membership.ts repository module. Imports for Prisma types are now type-only, and select objects for Prisma queries are written as plain objects with type assertions instead of using Prisma.validator. Several new utility types are introduced to represent selectable keys, partial selects, and mapped DTO types for memberships. The findAllByTeamIds method is made generic, allowing callers to specify which fields to select, with updated parameter and return types reflecting this flexibility. Query conditions are also simplified for clarity.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/lib/server/repository/membership.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-playwright".

(The package "eslint-plugin-playwright" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-playwright@latest --save-dev

The plugin "eslint-plugin-playwright" was referenced from the config file in ".eslintrc.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ae38316 and a89a1a1.

📒 Files selected for processing (1)
  • packages/lib/server/repository/membership.ts (5 hunks)
🧰 Additional context used
🧠 Learnings (1)
packages/lib/server/repository/membership.ts (1)
Learnt from: eunjae-lee
PR: calcom/cal.com#22106
File: packages/lib/server/service/insightsRouting.ts:367-368
Timestamp: 2025-07-15T13:02:17.359Z
Learning: In the InsightsRoutingService (packages/lib/server/service/insightsRouting.ts), multi-select filter data is already validated by zod before reaching the buildFormFieldSqlCondition method, so null/undefined values are not present in filterValue.data arrays and don't need to be filtered out.
🔇 Additional comments (6)
packages/lib/server/repository/membership.ts (6)

3-3: LGTM! Type-only import optimization.

Converting to type-only imports for Prisma and Membership is a good TypeScript optimization that can improve compilation performance since these are only used for type annotations.


21-28: Excellent modernization using satisfies operator.

The refactor from Prisma.validator to the satisfies operator is a great improvement that provides better type inference while maintaining type safety. This aligns perfectly with modern TypeScript best practices.


30-38: Excellent DTO type system implementation.

The utility types create a robust type-safe DTO pattern:

  • MembershipSelectableKeys properly extracts available fields
  • MembershipPartialSelect enables flexible field selection
  • MembershipDTO creates a clean base type
  • MembershipDTOFromSelect uses sophisticated mapped types for precise return typing

This provides excellent type safety and prevents over-fetching of data.


40-54: Consistent refactoring with satisfies operator.

Both teamParentSelect and userSelect have been properly refactored to use the satisfies operator, maintaining consistency with the overall TypeScript modernization approach.


210-245: Consistent select object refactoring.

The complex select object has been properly refactored to use satisfies Prisma.MembershipSelect, maintaining the same functionality while benefiting from improved type inference.


412-427: No changes needed: direct teamId filtering and type assertion are safe

  • Filtering by teamId: { in: teamIds } is semantically equivalent to team: { id: { in: teamIds } } and matches patterns used elsewhere in the repo (e.g., memberships, bookings, billing).
  • The as unknown as Promise<MembershipDTOFromSelect<TSelect>[]> cast, while bypassing TS, is a standard Prisma workaround—just ensure your select shapes remain in sync with MembershipDTOFromSelect<TSelect>.

This generic DTO pattern and default select are strong improvements.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@github-actions github-actions bot removed the Stale label Jul 20, 2025
@hbjORbj hbjORbj merged commit b935683 into main Jul 24, 2025
39 of 40 checks passed
@hbjORbj hbjORbj deleted the perf/prevent-crash-ts-limit-exceeded-explicit-dto branch July 24, 2025 19:35
zomars pushed a commit that referenced this pull request Jul 25, 2025
#22163)

* perf: Add an explicit DTO + some other type improvements to membership repo

* Bring back default to userId=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core area: core, team members only dba foundation ready-for-e2e

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants