Skip to content

Conversation

@ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 2, 2025

Summary by CodeRabbit

  • Refactor

    • Extracted and reorganized schema exports into a dedicated package for improved modularity
  • Chores

    • Updated build configurations and internal dependencies across packages

Copilot AI review requested due to automatic review settings November 2, 2025 17:18
@coderabbitai
Copy link

coderabbitai bot commented Nov 2, 2025

Walkthrough

The changes extract schema utilities into a standalone @zenstackhq/schema package, decoupling it from the SDK. Dependencies are updated across packages to reference the new schema package. The ORM restructures imports and redistributes the AuthType type. SDK removes schema export mappings.

Changes

Cohort / File(s) Summary
Schema Package (New)
packages/schema/package.json, packages/schema/src/index.ts, packages/schema/src/expression-utils.ts, packages/schema/tsconfig.json, packages/schema/tsup.config.ts, packages/schema/eslint.config.js
New standalone schema package created with build configuration, dependencies (decimal.js), and exports surface. Re-exports expression-utils and removes OperandExpression re-export.
ORM Schema Restructuring
packages/orm/package.json, packages/orm/src/schema.ts, packages/orm/src/schema/index.ts, packages/orm/src/schema/auth.ts
Added @zenstackhq/schema dependency; removed devDependencies. Created new src/schema.ts re-exporting from @zenstackhq/schema. Removed public exports from schema/index.ts. Deleted AuthType from schema/auth.ts.
ORM Client Changes
packages/orm/src/client/contract.ts, packages/orm/src/client/client-impl.ts, packages/orm/src/client/crud/validator/utils.ts
Moved AuthType from schema/auth to contract.ts. Consolidated imports. Updated import sources for FieldExpression, MemberExpression, UnaryExpression.
ORM Build Config
packages/orm/tsup.config.ts
Updated schema entry point from src/schema/index.ts to src/schema.ts.
SDK Export Removal
packages/sdk/package.json, packages/sdk/tsup.config.ts
Removed "./schema" export mapping. Removed schema entry from build configuration.
Dependency Updates
packages/testtools/package.json, packages/testtools/src/schema.ts, tests/e2e/package.json, tests/e2e/orm/schemas/basic/helper.ts
Added @zenstackhq/schema as dependency. Updated import sources from @zenstackhq/sdk/schema to @zenstackhq/schema.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Areas requiring extra attention:
    • AuthType relocation and its conditional type definition in packages/orm/src/client/contract.ts — verify the type logic is preserved correctly
    • Public API surface changes across schema module exports — ensure no breaking changes for consumers
    • New schema package structure and its export mappings — confirm ESM/CJS dual publishing setup is correct
    • Dependency chain updates across packages — verify no circular dependencies introduced

Possibly related PRs

  • #128: Both PRs coordinate schema packaging and module reorganization, moving @zenstackhq/schema to a standalone package and updating consumer imports across testtools and other packages.

Poem

🐰 A schema hops to its own little home,
No longer in SDK's domain to roam,
AuthType finds a cozy new nest,
Exports polished up, the refactor's the best!
✨ One package to rule them all, so bright!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "refactor: extract a runtime schema package" directly and accurately reflects the primary objective of the changeset. The PR performs a significant refactoring that creates a new @zenstackhq/schema package by extracting schema-related functionality from the existing SDK package, updating import paths across the monorepo, and removing the schema export from the SDK. The title is concise, specific, and uses clear terminology ("extract" and "runtime schema package") that conveys the essential nature of the change without vague language or unnecessary details.
✨ 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 refactor/extract-schema-package

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.

@claude
Copy link

claude bot commented Nov 2, 2025

Claude finished @ymc9's task —— View job


PR Review: Extract Runtime Schema Package

This PR extracts schema-related functionality from @zenstackhq/orm and @zenstackhq/sdk into a new @zenstackhq/schema package. While the concept is sound for improving modularity, there are critical issues that need to be addressed.

Positive Aspects

Good Architecture Decision

  • Clean separation of concerns - schema types are now in their own package
  • Minimal dependencies for the schema package (only decimal.js)
  • Proper TypeScript configuration and build setup with tsup
  • Clean package.json with appropriate exports for ESM/CJS

Code Quality

  • Well-structured type definitions in schema.ts with comprehensive type extraction utilities
  • Good organization of expression types and utilities
  • Proper re-exports in index files

🚨 Critical Issues

1. Incomplete Import Migration (HIGH PRIORITY)
39 files still import from @zenstackhq/orm/schema instead of @zenstackhq/schema:

Affected Packages:

  • @zenstackhq/server (11+ files) - Missing @zenstackhq/schema dependency
  • @zenstackhq/plugins (5+ files) - Missing dependency
  • @zenstackhq/zod (2+ files) - Missing dependency
  • @zenstackhq/tanstack-query (1+ files)
  • Test files and samples throughout

Examples of broken imports:

// packages/server/src/types.ts:2
import type { SchemaDef } from '@zenstackhq/orm/schema';
// Should be:
import type { SchemaDef } from '@zenstackhq/schema';

2. Missing Package Dependencies
Several packages import from @zenstackhq/schema but don't declare it as a dependency:

  • @zenstackhq/server package.json missing @zenstackhq/schema dependency
  • @zenstackhq/plugin-policy package.json missing @zenstackhq/schema dependency
  • @zenstackhq/zod package.json missing @zenstackhq/schema dependency

⚠️ Additional Concerns

Schema Re-export Strategy
The ORM package now re-exports everything from @zenstackhq/schema via src/schema.ts, but some files still import specific types from the schema package directly. Consider:

  • Remove the re-export if packages should import directly from @zenstackhq/schema
  • Or update all imports to use the re-export consistently

Build Impact
Cannot verify build success due to permission restrictions, but the import issues will likely cause TypeScript compilation failures.

📋 Required Actions

  1. Fix Import Paths - Update all 39 files to import from @zenstackhq/schema instead of @zenstackhq/orm/schema
  2. Add Dependencies - Add @zenstackhq/schema dependency to affected package.json files
  3. Verify Build - Ensure pnpm build passes after fixes
  4. Update Tests - Verify all tests still pass after import changes

🎯 Recommendation

This refactoring is architecturally sound but incomplete. The PR needs significant fixes before it can be safely merged. Consider running a comprehensive find/replace across the codebase to fix import paths, then verify the build succeeds.


Copy link
Contributor

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 extracts schema-related types and utilities from the @zenstackhq/sdk package into a new standalone package @zenstackhq/schema. This enables packages to depend on schema types without requiring the heavier SDK package, improving modularity and reducing dependency weight.

  • Created new @zenstackhq/schema package with schema types, expression types, and expression utilities
  • Updated import paths from @zenstackhq/sdk/schema to @zenstackhq/schema across the codebase
  • Moved AuthType from a separate file to the contract types file for better organization

Reviewed Changes

Copilot reviewed 20 out of 23 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/schema/package.json New package configuration for the standalone schema package
packages/schema/src/schema.ts Schema type definitions moved from SDK package
packages/schema/src/expression.ts Expression type definitions extracted to new package
packages/schema/src/expression-utils.ts Expression utility functions with updated import path
packages/schema/src/index.ts Main export file for the schema package
packages/schema/tsup.config.ts Build configuration for the schema package
packages/schema/tsconfig.json TypeScript configuration for the schema package
packages/schema/eslint.config.js ESLint configuration for the schema package
packages/sdk/package.json Removed /schema export now handled by separate package
packages/sdk/tsup.config.ts Removed schema entry point from build configuration
packages/orm/src/schema.ts Re-exports schema types from new package for backward compatibility
packages/orm/src/schema/index.ts Deleted - functionality moved to src/schema.ts
packages/orm/src/schema/auth.ts Deleted - AuthType moved to contract.ts
packages/orm/src/client/contract.ts Added AuthType definition previously in separate file
packages/orm/src/client/client-impl.ts Updated import to get AuthType from contract.ts
packages/orm/src/client/crud/validator/utils.ts Updated import to use new schema package
packages/orm/package.json Added schema package dependency, removed language and sdk from devDependencies
packages/orm/tsup.config.ts Updated schema entry point path
packages/testtools/package.json Added schema package dependency
packages/testtools/src/schema.ts Updated import to use new schema package
tests/e2e/package.json Added schema package dependency
tests/e2e/orm/schemas/basic/helper.ts Updated import to use new schema package
pnpm-lock.yaml Updated lockfile reflecting new package and dependency changes
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

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

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

🧹 Nitpick comments (2)
packages/schema/tsconfig.json (1)

4-4: Consider keeping noUnusedLocals enabled.

Disabling noUnusedLocals can allow unused variables to accumulate, reducing code quality. Unless there's a specific reason (e.g., temporary during development), consider keeping this check enabled to maintain clean code.

packages/schema/package.json (1)

18-29: Consider adding main and types fields for broader compatibility.

While the exports field is the modern approach, adding top-level main and types fields can improve compatibility with older tooling that doesn't fully support the exports field.

Apply this diff:

     "files": [
         "dist"
     ],
+    "main": "./dist/index.cjs",
+    "types": "./dist/index.d.ts",
     "exports": {
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7826da3 and af95981.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (20)
  • packages/orm/package.json (1 hunks)
  • packages/orm/src/client/client-impl.ts (1 hunks)
  • packages/orm/src/client/contract.ts (1 hunks)
  • packages/orm/src/client/crud/validator/utils.ts (1 hunks)
  • packages/orm/src/schema.ts (1 hunks)
  • packages/orm/src/schema/auth.ts (0 hunks)
  • packages/orm/src/schema/index.ts (0 hunks)
  • packages/orm/tsup.config.ts (1 hunks)
  • packages/schema/eslint.config.js (1 hunks)
  • packages/schema/package.json (1 hunks)
  • packages/schema/src/expression-utils.ts (1 hunks)
  • packages/schema/src/index.ts (1 hunks)
  • packages/schema/tsconfig.json (1 hunks)
  • packages/schema/tsup.config.ts (1 hunks)
  • packages/sdk/package.json (0 hunks)
  • packages/sdk/tsup.config.ts (0 hunks)
  • packages/testtools/package.json (1 hunks)
  • packages/testtools/src/schema.ts (1 hunks)
  • tests/e2e/orm/schemas/basic/helper.ts (1 hunks)
  • tests/e2e/package.json (1 hunks)
💤 Files with no reviewable changes (4)
  • packages/sdk/package.json
  • packages/orm/src/schema/index.ts
  • packages/orm/src/schema/auth.ts
  • packages/sdk/tsup.config.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-21T16:09:31.218Z
Learnt from: ymc9
Repo: zenstackhq/zenstack-v3 PR: 319
File: packages/runtime/src/client/executor/zenstack-query-executor.ts:63-72
Timestamp: 2025-10-21T16:09:31.218Z
Learning: In ZenStack, TypeDefs can be inherited by models. When a TypeDef contains fields with `map` attributes, those mapped field names need to be processed by the QueryNameMapper since they become part of the inheriting model's schema. Therefore, when checking if a schema has mapped names (e.g., in `schemaHasMappedNames`), both `schema.models` and `schema.typeDefs` must be inspected for `@map` and `map` attributes.

Applied to files:

  • packages/testtools/src/schema.ts
🧬 Code graph analysis (1)
packages/orm/src/client/contract.ts (2)
packages/schema/src/schema.ts (2)
  • SchemaDef (10-18)
  • GetModels (109-109)
packages/orm/src/client/crud-types.ts (1)
  • ModelResult (135-179)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: build-test (20.x, postgresql)
  • GitHub Check: build-test (20.x, sqlite)
  • GitHub Check: claude-review
🔇 Additional comments (15)
packages/orm/tsup.config.ts (1)

6-6: LGTM!

The entry point update from src/schema/index.ts to src/schema.ts correctly aligns with the new ORM schema surface that re-exports from @zenstackhq/schema.

packages/orm/src/client/crud/validator/utils.ts (1)

2-10: LGTM!

The import path update to @zenstackhq/schema correctly reflects the new schema package structure. Type imports remain unchanged functionally.

packages/schema/tsup.config.ts (1)

1-13: LGTM!

The tsup configuration follows established patterns in the project with appropriate settings for building a library package (dual CJS/ESM output, type declarations, sourcemaps).

packages/orm/src/client/contract.ts (1)

806-814: LGTM!

The local AuthType definition correctly centralizes the type where it's used in the client contract. The conditional type logic properly handles schema-defined auth types vs. fallback to generic records.

packages/orm/src/client/client-impl.ts (1)

15-21: LGTM!

The import reorganization consolidates AuthType with other contract types in a single import block, aligning with the type now being defined in contract.ts. No functional changes.

packages/schema/eslint.config.js (1)

1-4: LGTM!

The ESLint configuration correctly re-exports the shared base config, following the established pattern for configuration sharing across packages.

tests/e2e/package.json (1)

18-18: LGTM! Schema package dependency added.

The new @zenstackhq/schema workspace dependency is correctly added to support the refactored schema package.

packages/orm/package.json (1)

66-66: LGTM! Schema package dependency added.

The ORM package correctly references the newly extracted @zenstackhq/schema workspace package.

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

3-3: LGTM! Import updated to new schema package.

The import path is correctly updated to reference the extracted @zenstackhq/schema package.

tests/e2e/orm/schemas/basic/helper.ts (1)

1-1: LGTM! Import updated to new schema package.

The import path correctly references the extracted @zenstackhq/schema package.

packages/orm/src/schema.ts (1)

1-2: LGTM! Clean re-export module.

This new file provides a convenient entry point that combines the extracted schema package exports with ORM-specific types like OperandExpression from Kysely. The separation makes sense as OperandExpression is Kysely-specific and belongs in the ORM layer.

packages/testtools/package.json (1)

36-36: LGTM! Schema package dependency added.

The testtools package correctly references the newly extracted @zenstackhq/schema workspace package.

packages/schema/src/expression-utils.ts (2)

14-14: LGTM! More explicit import path.

The import path change from '.' to './expression' makes the dependency more explicit and improves code maintainability.


16-18: LGTM! Documentation added.

The JSDoc comment clearly describes the purpose of the ExpressionUtils object.

packages/schema/src/index.ts (1)

2-2: LGTM! Expression utilities added to public API.

The expression-utils module is now part of the schema package's public API surface. The removal of OperandExpression re-export (now in the ORM package) correctly reflects that it's Kysely-specific and belongs at the ORM layer rather than the core schema package.

@ymc9 ymc9 merged commit f2d7190 into dev Nov 2, 2025
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.

2 participants