feat: use enhanced prisma to support Polymorphism (#28)#29
Conversation
* feat: use enhanced prisma to support Polymorphism * resolve comments
📝 WalkthroughWalkthroughVersion 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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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.
| getPrisma: () => { | ||
| return enhanceFunc( | ||
| prisma, | ||
| {}, | ||
| { | ||
| kinds: Enhancements, | ||
| } | ||
| ) | ||
| }, |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| type EnhancementKind = 'password' | 'omit' | 'policy' | 'validation' | 'delegate' | 'encryption' | ||
| // enable all enhancements except policy |
There was a problem hiding this comment.
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.
| // 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. |
| getPrisma: () => { | ||
| return enhanceFunc( | ||
| prisma, | ||
| {}, |
There was a problem hiding this comment.
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.
| } 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` + |
There was a problem hiding this comment.
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.
| `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` + |
There was a problem hiding this comment.
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 | 🟠 MajorAdd validation to ensure
enhanceFuncis 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. Ifenhanceis not exported from the loaded module,enhanceFuncsilently becomesundefined, 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'}` + ) + }
feat: use enhanced prisma to support Polymorphism
resolve comments
Summary by CodeRabbit
Release Notes
New Features
Improvements
Chores