Skip to content
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

fix: compatibility with Prisma's "omit" feature #1432

Merged
merged 1 commit into from
May 11, 2024
Merged

fix: compatibility with Prisma's "omit" feature #1432

merged 1 commit into from
May 11, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented May 11, 2024

  • Upgrade @prisma/internals and @prisma/generator-helper to latest.
  • Respect omit clause when reading entities.
  • Conditionally generate createMany based on Prisma version.

Copy link
Contributor

coderabbitai bot commented May 11, 2024

Walkthrough

Walkthrough

The recent updates across various packages primarily introduce the supportCreateMany function and enforce immutability by changing parameters to readonly arrays in several functions. Additionally, there are enhancements in handling schema and model generation with new conditions and extended functionalities, particularly focusing on input and output types and the handling of enums. These changes aim to improve the SDK's robustness and adaptability in managing database operations and schema transformations.

Changes

Files Summary
.../openapi/src/rpc-generator.ts
.../swr/src/generator.ts
.../tanstack-query/src/generator.ts
.../trpc/src/generator.ts
.../trpc/src/helpers.ts
Added supportCreateMany import and related conditional logic. Updated parameter immutability to readonly. Renamed parameters for clarity.
.../runtime/src/enhancements/policy/policy-utils.ts Enhanced handling of the omit property to avoid interference with field selection.
.../schema/src/plugins/...
.../sdk/src/dmmf-helpers/...
Updated function signatures to accept readonly arrays. Extended functionalities in schema and model generation processes.
.../sdk/src/utils.ts Introduced new utility functions and imports to handle data sources and version management.
.../testtools/src/schema.ts
tests/integration/tests/enhancements/with-policy/prisma-omit.test.ts
Added handling for previewFeatures in schema options and introduced a new test scenario for the omit feature in Prisma schemas.

This table groups files with similar changes, focusing on the introduction of new functions, enhancement of existing functionalities, and ensuring data integrity through immutability.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 generate unit testing code for this file.
    • @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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@ymc9
Copy link
Member Author

ymc9 commented May 11, 2024

Fixes #1278

Copy link
Contributor

@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: 0

Out of diff range and nitpick comments (1)
packages/schema/src/plugins/zod/transformer.ts (1)

Line range hint 393-493: Review the method generateInputSchemas. The method handles the generation of input schemas for various operations like findUnique, findFirst, findMany, etc., based on model operations. It uses conditional checks to determine which schemas to generate and includes handling for both checked and unchecked scenarios.

  • The method is quite large and complex, consider refactoring to improve readability and maintainability.
  • Ensure that the use of supportCreateMany is consistent and correctly checks the container's support for batch operations.
Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 23a9bbb and b2e001c.
Files ignored due to path filters (2)
  • packages/sdk/package.json is excluded by !**/*.json
  • pnpm-lock.yaml is excluded by !pnpm-lock.yaml, !**/*.yaml
Files selected for processing (17)
  • packages/plugins/openapi/src/rpc-generator.ts (4 hunks)
  • packages/plugins/swr/src/generator.ts (2 hunks)
  • packages/plugins/tanstack-query/src/generator.ts (2 hunks)
  • packages/plugins/trpc/src/generator.ts (5 hunks)
  • packages/plugins/trpc/src/helpers.ts (1 hunks)
  • packages/runtime/src/enhancements/policy/policy-utils.ts (2 hunks)
  • packages/schema/src/plugins/enhancer/enhance/index.ts (1 hunks)
  • packages/schema/src/plugins/zod/generator.ts (2 hunks)
  • packages/schema/src/plugins/zod/transformer.ts (4 hunks)
  • packages/schema/src/plugins/zod/types.ts (1 hunks)
  • packages/sdk/src/dmmf-helpers/include-helpers.ts (1 hunks)
  • packages/sdk/src/dmmf-helpers/model-helpers.ts (1 hunks)
  • packages/sdk/src/dmmf-helpers/modelArgs-helpers.ts (1 hunks)
  • packages/sdk/src/dmmf-helpers/select-helpers.ts (2 hunks)
  • packages/sdk/src/utils.ts (4 hunks)
  • packages/testtools/src/schema.ts (2 hunks)
  • tests/integration/tests/enhancements/with-policy/prisma-omit.test.ts (1 hunks)
Files not reviewed due to errors (1)
  • packages/runtime/src/enhancements/policy/policy-utils.ts (no review received)
Files skipped from review due to trivial changes (1)
  • tests/integration/tests/enhancements/with-policy/prisma-omit.test.ts
Additional comments not posted (31)
packages/schema/src/plugins/zod/types.ts (3)

5-5: Ensure immutability by using readonly in enumTypes.


6-6: Ensure immutability by using readonly in fields.


8-8: Ensure immutability by using readonly in models.

packages/sdk/src/dmmf-helpers/model-helpers.ts (1)

34-34: Ensure immutability by using readonly in models parameter of findModelByName.

packages/sdk/src/dmmf-helpers/modelArgs-helpers.ts (2)

4-7: Ensure immutability by using readonly in models parameter of addMissingInputObjectTypesForModelArgs.


14-14: Ensure immutability by using readonly in models parameter of generateModelArgsInputObjectTypes.

packages/sdk/src/dmmf-helpers/include-helpers.ts (2)

4-7: Ensure immutability by using readonly in models parameter of addMissingInputObjectTypesForInclude.


15-15: Ensure immutability by using readonly in models parameter of generateModelIncludeInputObjectTypes.

packages/sdk/src/dmmf-helpers/select-helpers.ts (2)

7-7: Ensure immutability by using readonly in models parameter of addMissingInputObjectTypesForSelect.


92-118: Optimize the generation of model select input object types by using readonly in models parameter.

packages/testtools/src/schema.ts (2)

100-100: Ensure conditional handling of preview features in schema generation.


137-137: Ensure proper handling of preview features by making it a part of SchemaLoadOptions.

packages/plugins/trpc/src/helpers.ts (1)

330-330: Enhance handling of model comments to determine hidden models.

packages/plugins/trpc/src/generator.ts (4)

10-10: Ensure support for createMany operation based on model capabilities.


Line range hint 83-176: Enhance the app router creation to handle model operations more dynamically, including conditional generation based on model visibility and actions.


246-247: Optimize model router creation by dynamically generating routes based on model operations and client helper requirements.


304-307: Correctly handle the conditional generation of createMany operation based on model support.

packages/plugins/swr/src/generator.ts (1)

89-89: Use of supportCreateMany correctly implements conditional feature generation based on Prisma version.

Verification successful

The search results confirm the presence and usage of the supportCreateMany function across various parts of the codebase, including the specific file and line in question (packages/plugins/swr/src/generator.ts). The function is defined in packages/sdk/src/utils.ts and is imported and used in several modules to conditionally enable features based on the model's capabilities, which aligns with the intended functionality described in the review comment.

Given this information, the use of supportCreateMany in the generator.ts file of the SWR plugin is consistent with its usage in other parts of the codebase and is implemented correctly to handle different versions of Prisma as intended.

Conclusion:

  • The implementation of supportCreateMany is verified to be correct and is used appropriately across the codebase to handle feature compatibility based on the model's capabilities.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of `supportCreateMany`.

# Test: Search for the function implementation. Expect: Proper handling of Prisma versions.
rg --type typescript $'supportCreateMany'

Length of output: 75



Script:

#!/bin/bash
# Description: Re-verify the implementation of `supportCreateMany`.

# Test: Search for the function implementation without specifying file type. Expect: Proper handling of Prisma versions.
rg 'supportCreateMany'

Length of output: 1162

packages/sdk/src/utils.ts (2)

541-551: Implementation of getDataSourceProvider correctly handles potential absence of data source or provider field.


556-561: Implementation of supportCreateMany correctly determines support for createMany based on Prisma version and provider.

Verification successful

The implementations of getPrismaVersion and getDataSourceProvider are correctly defined and used across the codebase. The functions are essential for determining the support for createMany based on the Prisma version and provider, and they are implemented as expected.

  • getPrismaVersion is defined in packages/sdk/src/prisma.ts
  • getDataSourceProvider is defined in packages/sdk/src/utils.ts
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementations of `getPrismaVersion` and `getDataSourceProvider`.

# Test: Search for the function implementations. Expect: Correct handling of versions and providers.
rg --type typescript $'getPrismaVersion|getDataSourceProvider'

Length of output: 98



Script:

#!/bin/bash
# Description: Re-verify the implementations of `getPrismaVersion` and `getDataSourceProvider`.

# Test: Search for the function implementations in TypeScript files. Expect: Correct handling of versions and providers.
rg 'getPrismaVersion|getDataSourceProvider' -g '*.ts' -g '*.tsx'

Length of output: 1226

packages/schema/src/plugins/zod/generator.ts (2)

192-195: Changes to generateEnumSchemas correctly handle both Prisma and model-specific enums.


110-110: Addition of the model parameter to generateInputSchemas enhances schema generation capabilities.

packages/schema/src/plugins/enhancer/enhance/index.ts (1)

59-59: Ensure the return type Promise<{ dmmf: DMMF.Document | undefined }> aligns with the intended usage of the generate method across the application.

packages/plugins/tanstack-query/src/generator.ts (1)

352-352: Verify the implementation of supportCreateMany to ensure it correctly determines when the createMany method should be generated based on the Prisma version.

Verification successful

The implementation of the supportCreateMany function has been successfully retrieved and reviewed. The function checks if the createMany method is supported based on the Prisma version and the data source provider. Specifically, it supports createMany for SQLite from Prisma version 5.12.0 onwards. This aligns with the expected functionality as described in the review comment, ensuring that the createMany method is generated conditionally based on the Prisma version.

  • The function correctly retrieves the Prisma version using getPrismaVersion.
  • It checks the data source provider with getDataSourceProvider.
  • It conditionally supports createMany for SQLite if the Prisma version is 5.12.0 or higher.

This confirms that the implementation is as expected and correctly integrates version checking for feature support.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of `supportCreateMany`.

# Test: Search for the function implementation. Expect: Correct logic for Prisma version check.
rg --type typescript $'function supportCreateMany'

Length of output: 86



Script:

#!/bin/bash
# Description: Re-verify the implementation of `supportCreateMany`.

# Test: Search for the function implementation using a broader file type. Expect: Correct logic for Prisma version check.
rg --type js,ts $'function supportCreateMany'

Length of output: 76



Script:

#!/bin/bash
# Description: Re-verify the implementation of `supportCreateMany`.

# Test: Search for the function implementation using correct file type specification. Expect: Correct logic for Prisma version check.
rg --type js --type ts 'function supportCreateMany'

Length of output: 129



Script:

#!/bin/bash
# Description: Retrieve the implementation of `supportCreateMany`.

# Test: Display the implementation of `supportCreateMany`. Expect: Detailed function logic.
cat packages/sdk/src/utils.ts | grep -A 10 'function supportCreateMany'

Length of output: 382

packages/schema/src/plugins/zod/transformer.ts (4)

15-15: Ensure immutability by using readonly for fields.


17-17: Ensure immutability by using readonly for models.


20-20: Ensure immutability by using readonly for enumTypes.


493-493: Verify the conditional generation of createMany method. This is crucial for compatibility with different versions of Prisma.

Verification successful

The conditional generation of the createMany method appears to be implemented correctly across the codebase, particularly in relation to compatibility with different versions of Prisma. Notably, in packages/schema/src/plugins/zod/transformer.ts, the method's generation is conditioned on the result of supportCreateMany(zmodel), which is a good practice for ensuring compatibility.

  • File: packages/schema/src/plugins/zod/transformer.ts
  • Context: Conditional generation based on supportCreateMany(zmodel)
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the conditional generation of `createMany` method across the codebase.

# Test: Search for the usage of `createMany`. Expect: Conditional checks before usage.
rg --type typescript $'createMany'

Length of output: 68



Script:

#!/bin/bash
# Description: Verify the conditional generation of `createMany` method across the codebase.

# Test: Search for the usage of `createMany`. Expect: Conditional checks before usage.
rg --type ts $'createMany'

Length of output: 10863

packages/plugins/openapi/src/rpc-generator.ts (3)

3-3: Ensure that the import of supportCreateMany is used appropriately for conditional feature generation.


6-6: Review the import of AggregateOperationSupport and related helper functions. These are crucial for handling aggregate operations in the OpenAPI specification generation.


169-169: Review the conditional generation of createMany method in the RPCOpenAPIGenerator class. This method should only be generated if supportCreateMany returns true for the given model container, ensuring compatibility with Prisma versions that do not support batch operations.

  • Verify that supportCreateMany is correctly implemented and used across the codebase.
Verification successful

The verification process has successfully identified the implementation and usage of the supportCreateMany function across various parts of the codebase. Here are the key findings:

  • The function supportCreateMany is defined in packages/sdk/src/utils.ts.
  • It is used in several generator files across different plugins (trpc, swr, tanstack-query, openapi) to conditionally generate methods based on the model's compatibility with batch operations.
  • The usage pattern consistently checks the capability of the model container before proceeding with batch operations like createMany.

This confirms that the function is implemented and utilized correctly to ensure compatibility with Prisma versions that do not support batch operations, as intended in the original review comment.

Locations where supportCreateMany is used:

  • packages/schema/src/plugins/zod/transformer.ts
  • packages/plugins/trpc/src/generator.ts
  • packages/plugins/swr/src/generator.ts
  • packages/plugins/tanstack-query/src/generator.ts
  • packages/plugins/openapi/src/rpc-generator.ts
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correct implementation and usage of `supportCreateMany` across the codebase.

# Test: Search for the implementation and usage of `supportCreateMany`. Expect: Correct checks and usage patterns.
rg --type typescript $'supportCreateMany'

Length of output: 75



Script:

#!/bin/bash
# Description: Re-verify the correct implementation and usage of `supportCreateMany` across the codebase using corrected file type specifications.

# Test: Search for the implementation and usage of `supportCreateMany` in TypeScript files. Expect: Correct checks and usage patterns.
rg 'supportCreateMany' --glob '*.ts' --glob '*.tsx'

Length of output: 1191

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.

1 participant