Skip to content

feat: use enhanced prisma to support Polymorphism (#28)#29

Merged
jiashengguo merged 1 commit intomainfrom
dev
Feb 3, 2026
Merged

feat: use enhanced prisma to support Polymorphism (#28)#29
jiashengguo merged 1 commit intomainfrom
dev

Conversation

@jiashengguo
Copy link
Member

@jiashengguo jiashengguo commented Feb 3, 2026

  • feat: use enhanced prisma to support Polymorphism

  • resolve comments

Summary by CodeRabbit

Release Notes

  • New Features

    • Server enhancement system now integrated, enabling advanced data handling and access capabilities.
  • Improvements

    • Enhanced error messaging during module initialization for clearer troubleshooting.
  • Chores

    • Version updated to 0.3.0.

* feat: use enhanced prisma to support Polymorphism

* resolve comments
Copilot AI review requested due to automatic review settings February 3, 2026 00:31
@coderabbitai
Copy link

coderabbitai bot commented Feb 3, 2026

📝 Walkthrough

Walkthrough

Version bumped to 0.3.0. EnhancementKind type and Enhancements array introduced in server initialization. EnhanceFunc handling added to ZenStack module loading, with modelMeta and enhance function loaded from runtime. ZenStackMiddleware getPrisma updated to return enhanced Prisma client.

Changes

Cohort / File(s) Summary
Version Update
package.json
Version bumped from 0.2.7 to 0.3.0.
Enhancement Infrastructure
src/server.ts
Introduces EnhancementKind type and Enhancements array. Adds enhanceFunc handling to loadZenStackModules with modelMeta and enhance function loading from ZenStack runtime. Extends return value to include enhanceFunc and propagates it through startServer. Updates ZenStackMiddleware getPrisma to return enhanced Prisma client via enhanceFunc with configured enhancement kinds.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Version blooms to point-three-oh so bright,
Enhancements dance through the middleware's flight,
With enhanceFunc wired through ZenStack's core,
The Prisma client shines brighter than before! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title 'feat: use enhanced prisma to support Polymorphism' accurately reflects the main changes: introducing enhanced Prisma client functionality with enhancements wiring in src/server.ts and a version bump in package.json.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dev

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces enhanced Prisma client support to enable polymorphism features in the ZenStack proxy server. The changes wrap the Prisma client with ZenStack's enhance function, applying specific enhancements while explicitly excluding policy enforcement.

Changes:

  • Added enhance function loading from ZenStack runtime or generated modules
  • Modified getPrisma to wrap Prisma client with enhance function, enabling password, omit, validation, delegate, and encryption enhancements (excluding policy)
  • Version bump from 0.2.7 to 0.3.0

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/server.ts Added EnhancementKind type and Enhancements constant; loaded enhance function from ZenStack modules; wrapped Prisma client with enhance in getPrisma callback
package.json Bumped version to 0.3.0 for this new feature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +243 to +251
getPrisma: () => {
return enhanceFunc(
prisma,
{},
{
kinds: Enhancements,
}
)
},
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

The getPrisma function creates a new enhanced Prisma instance on every invocation. Depending on how frequently ZenStackMiddleware calls getPrisma and how the enhance function works internally, this could create performance overhead. Consider caching the enhanced Prisma instance if enhance() is expensive, or verify that enhance() is designed to be called on every request. If enhance() is a lightweight wrapper that should be called per-request (e.g., for request-specific context), this is fine and can be ignored.

Copilot uses AI. Check for mistakes.
}

type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate' | 'encryption'
// enable all enhancements except policy
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

The EnhancementKind type and Enhancements constant define which enhancements are enabled, notably excluding 'policy'. Consider adding more detailed documentation explaining why 'policy' is specifically excluded from the enhancements, as this is a significant configuration decision that affects the behavior of the proxy server.

Suggested change
// enable all enhancements except policy
// Note: 'policy' is intentionally excluded here. Policy enforcement is expected to be handled by the
// application or an upstream API layer, not by this proxy server. Enabling the 'policy' enhancement
// at this level could alter behavior in ways that depend on application-specific authorization logic,
// which is outside the scope of this generic proxy. Therefore, only data-level enhancements are enabled.

Copilot uses AI. Check for mistakes.
getPrisma: () => {
return enhanceFunc(
prisma,
{},
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

The enhance function is called with an empty object ({}) as the context parameter. This might be intentional for a stateless proxy server, but verify that this is the correct usage. If the enhance function is designed to receive request-specific context (e.g., user information for policy enforcement), this empty context could bypass important security or access control features.

Copilot uses AI. Check for mistakes.
} catch {
throw new CliError(
`Failed to load ZenStack generated model meta from: ${modelMetaPath}\n` +
`Failed to load ZenStack generated model meta from: ${zenstackAbsPath || '@zenstackhq/runtime'}\n` +
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

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

The error message only mentions "model meta" but the try-catch block now covers loading both modelMeta and enhanceFunc. Consider updating the error message to reflect that it covers loading all ZenStack modules including the enhance function, or handle the errors separately to provide more specific error messages.

Suggested change
`Failed to load ZenStack generated model meta from: ${zenstackAbsPath || '@zenstackhq/runtime'}\n` +
`Failed to load ZenStack generated modules (model meta and enhance function) from: ${
zenstackAbsPath || '@zenstackhq/runtime'
}\n` +

Copilot uses AI. Check for mistakes.
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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/server.ts (1)

143-203: ⚠️ Potential issue | 🟠 Major

Add validation to ensure enhanceFunc is a function; prevent runtime crash on first request.

The try-catch block only catches require() failures (file not found, syntax errors), not property access errors. If enhance is not exported from the loaded module, enhanceFunc silently becomes undefined, then crashes at line 212 with an unclear error. Add validation immediately after loading to fail fast with a helpful message.

🛠️ Suggested fix
   try {
     if (zenstackAbsPath) {
       modelMeta = require(path.join(zenstackAbsPath, 'model-meta')).default
       enhanceFunc = require(path.join(zenstackAbsPath, 'enhance')).enhance
     } else {
       modelMeta = require('@zenstackhq/runtime/model-meta').default
       enhanceFunc = require('@zenstackhq/runtime').enhance
     }
   } catch {
     throw new CliError(
       `Failed to load ZenStack generated model meta from: ${zenstackAbsPath || '@zenstackhq/runtime'}\n` +
         `Please run \`zenstack generate\` first or specify the correct output directory of ZenStack generated modules using the \`-z\` option.`
     )
   }

   if (!modelMeta.models) {
     throw new CliError(`Generated model meta not found. Please run \`zenstack generate\` first.`)
   }
+
+  if (typeof enhanceFunc !== 'function') {
+    throw new CliError(
+      `Failed to load ZenStack enhance function from: ${zenstackAbsPath || '@zenstackhq/runtime'}`
+    )
+  }

@jiashengguo jiashengguo merged commit 37f9bb7 into main Feb 3, 2026
12 checks passed
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