Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions .github/workflows/claude-code-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Claude Code Review

on:
pull_request:
types: [opened, synchronize]
# Optional: Only run on specific file changes
# paths:
# - "src/**/*.ts"
# - "src/**/*.tsx"
# - "src/**/*.js"
# - "src/**/*.jsx"

jobs:
claude-review:
# Optional: Filter by PR author
# if: |
# github.event.pull_request.user.login == 'external-contributor' ||
# github.event.pull_request.user.login == 'new-developer' ||
# github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR'

runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: read
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Run Claude Code Review
id: claude-review
uses: anthropics/claude-code-action@beta
with:
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}

# Optional: Specify model (defaults to Claude Sonnet 4, uncomment for Claude Opus 4)
# model: "claude-opus-4-20250514"

# Direct prompt for automated review (no @claude mention needed)
direct_prompt: |
Please review this pull request and provide feedback on:
- Code quality and best practices
- Potential bugs or issues
- Performance considerations
- Security concerns
- Test coverage

Be constructive and helpful in your feedback.

# Optional: Use sticky comments to make Claude reuse the same comment on subsequent pushes to the same PR
# use_sticky_comment: true

# Optional: Customize review based on file types
# direct_prompt: |
# Review this PR focusing on:
# - For TypeScript files: Type safety and proper interface usage
# - For API endpoints: Security, input validation, and error handling
# - For React components: Performance, accessibility, and best practices
# - For tests: Coverage, edge cases, and test quality

# Optional: Different prompts for different authors
# direct_prompt: |
# ${{ github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR' &&
# 'Welcome! Please review this PR from a first-time contributor. Be encouraging and provide detailed explanations for any suggestions.' ||
# 'Please provide a thorough code review focusing on our coding standards and best practices.' }}

# Optional: Add specific tools for running tests or linting
# allowed_tools: "Bash(npm run test),Bash(npm run lint),Bash(npm run typecheck)"

# Optional: Skip review for certain conditions
# if: |
# !contains(github.event.pull_request.title, '[skip-review]') &&
# !contains(github.event.pull_request.title, '[WIP]')
63 changes: 63 additions & 0 deletions .github/workflows/claude.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Claude Code

on:
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
issues:
types: [opened, assigned]
pull_request_review:
types: [submitted]

jobs:
claude:
if: |
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
issues: read
id-token: write
actions: read # Required for Claude to read CI results on PRs
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1

- name: Run Claude Code
id: claude
uses: anthropics/claude-code-action@beta
with:
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}

# This is an optional setting that allows Claude to read CI results on PRs
additional_permissions: |
actions: read

# Optional: Specify model (defaults to Claude Sonnet 4, uncomment for Claude Opus 4)
# model: "claude-opus-4-20250514"

# Optional: Customize the trigger phrase (default: @claude)
# trigger_phrase: "/claude"

# Optional: Trigger when specific user is assigned to an issue
# assignee_trigger: "claude-bot"

# Optional: Allow Claude to run specific commands
# allowed_tools: "Bash(npm install),Bash(npm run build),Bash(npm run test:*),Bash(npm run lint:*)"

# Optional: Add custom instructions for Claude to customize its behavior for your project
# custom_instructions: |
# Follow our coding standards
# Ensure all new code has tests
# Use TypeScript for new files

# Optional: Custom environment variables for Claude
# claude_env: |
# NODE_ENV: test
7 changes: 6 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
packages/language/src/generated/**
**/schema.ts
**/test/**/schema.ts
**/test/**/models.ts
**/test/**/input.ts
samples/**/schema.ts
samples/**/models.ts
samples/**/input.ts
12 changes: 11 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"label": "Build all - watch",
"command": "turbo watch build",
"command": "pnpm watch",
"type": "shell",
"group": {
"kind": "build"
Expand Down Expand Up @@ -50,6 +50,16 @@
"color": "terminal.ansiMagenta",
"id": "server-process"
}
},
{
"label": "Submit PR",
"command": "pnpm pr",
"type": "shell",
"icon": {
"color": "terminal.ansiWhite",
"id": "server-process"
},
"problemMatcher": []
}
]
}
84 changes: 84 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Development Commands

### Build System

- `pnpm build` - Build all packages using Turbo
- `pnpm watch` - Watch mode for all packages
- `pnpm lint` - Run ESLint across all packages
- `pnpm test` - Run tests for all packages

### Package Management

- Uses `pnpm` with workspaces
- Package manager is pinned to `pnpm@10.12.1`
- Packages are located in `packages/`, `samples/`, and `tests/`

### Testing

- Runtime package tests: `pnpm test` (includes vitest, typing generation, and typecheck)
- CLI tests: `pnpm test`
- E2E tests are in `tests/e2e/` directory

### ZenStack CLI Commands

- `npx zenstack init` - Initialize ZenStack in a project
- `npx zenstack generate` - Compile ZModel schema to TypeScript
- `npx zenstack db push` - Sync schema to database (uses Prisma)
- `npx zenstack migrate dev` - Create and apply migrations
- `npx zenstack migrate deploy` - Deploy migrations to production

## Architecture Overview

### Core Components

- **@zenstackhq/runtime** - Main database client and ORM engine built on Kysely
- **@zenstackhq/cli** - Command line interface and project management
- **@zenstackhq/language** - ZModel language specification and parser (uses Langium)
- **@zenstackhq/sdk** - Code generation utilities and schema processing

### Key Architecture Patterns

- **Monorepo Structure**: Uses pnpm workspaces with Turbo for build orchestration
- **Language-First Design**: ZModel DSL compiles to TypeScript, not runtime code generation
- **Kysely-Based ORM**: V3 uses Kysely as query builder instead of Prisma runtime dependency
- **Plugin Architecture**: Runtime plugins for query interception and entity mutation hooks

### ZModel to TypeScript Flow

1. ZModel schema (`schema.zmodel`) defines database structure and policies
2. `zenstack generate` compiles ZModel to TypeScript schema (`schema.ts`)
3. Schema is used to instantiate `ZenStackClient` with type-safe CRUD operations
4. Client provides both high-level ORM API and low-level Kysely query builder

### Package Dependencies

- **Runtime**: Depends on Kysely, Zod, and various utility libraries
- **CLI**: Depends on language package, Commander.js, and Prisma (for migrations)
- **Language**: Uses Langium for grammar parsing and AST generation
- **Database Support**: SQLite (better-sqlite3) and PostgreSQL (pg) only

### Testing Strategy

- Runtime package has comprehensive client API tests and policy tests
- CLI has action-specific tests for commands
- E2E tests validate real-world schema compatibility (cal.com, formbricks, trigger.dev)
- Type coverage tests ensure TypeScript inference works correctly

## Key Differences from Prisma

- No runtime dependency on @prisma/client
- Pure TypeScript implementation without Rust/WASM
- Built-in access control and validation (coming soon)
- Kysely query builder as escape hatch instead of raw SQL
- Schema-first approach with ZModel DSL extension of Prisma schema language

## Development Notes

- Always run `zenstack generate` after modifying ZModel schemas
- Database migrations still use Prisma CLI under the hood
- Plugin system allows interception at ORM, Kysely, and entity mutation levels
- Computed fields are evaluated at database level for performance
21 changes: 16 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
- [x] migrate
- [x] info
- [x] init
- [x] validate
- [ ] format
- [ ] db seed
- [ ] ZModel
- [ ] View support
- [ ] ORM
- [x] Create
- [x] Input validation
Expand Down Expand Up @@ -44,7 +49,7 @@
- [x] Nested to-one
- [x] Incremental update for numeric fields
- [x] Array update
- [ ] Strict typing for checked/unchecked input
- [x] Strict typing for checked/unchecked input
- [x] Upsert
- [ ] Implement with "on conflict"
- [x] Delete
Expand All @@ -53,30 +58,36 @@
- [x] Aggregate
- [x] Group by
- [x] Raw queries
- [ ] Transactions
- [x] Transactions
- [x] Interactive transaction
- [x] Sequential transaction
- [ ] Extensions
- [x] Query builder API
- [x] Computed fields
- [x] Prisma client extension
- [ ] Custom procedures
- [ ] Misc
- [x] JSDoc for CRUD methods
- [x] Cache validation schemas
- [x] Compound ID
- [ ] Cross field comparison
- [x] Many-to-many relation
- [ ] Empty AND/OR/NOT behavior
- [?] Logging
- [ ] Error system
- [x] Logging
- [x] Error system
- [x] Custom table name
- [x] Custom field name
- [ ] Strict undefined checks
- [ ] DbNull vs JsonNull
- [ ] Benchmark
- [ ] Plugin
- [ ] Post-mutation hooks should be called after transaction is committed
- [ ] Polymorphism
- [x] TypeDef and mixin
- [ ] Strongly typed JSON
- [x] Polymorphism
- [x] ZModel
- [x] Runtime
- [x] Typing
- [ ] Validation
- [ ] Access Policy
- [ ] Short-circuit pre-create check for scalar-field only policies
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-v3",
"version": "3.0.0-alpha.12",
"version": "3.0.0-alpha.13",
"description": "ZenStack",
"packageManager": "pnpm@10.12.1",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack CLI",
"description": "FullStack database toolkit with built-in access control and automatic API generation.",
"version": "3.0.0-alpha.12",
"version": "3.0.0-alpha.13",
"type": "module",
"author": {
"name": "ZenStack Team"
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/actions/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Options = {
* CLI action for generating code from schema
*/
export async function run(options: Options) {
const start = Date.now();

const schemaFile = getSchemaFile(options.schema);

const model = await loadSchemaDocument(schemaFile);
Expand All @@ -40,7 +42,7 @@ export async function run(options: Options) {
}

if (!options.silent) {
console.log(colors.green('Generation completed successfully.'));
console.log(colors.green(`Generation completed successfully in ${Date.now() - start}ms.`));
console.log(`You can now create a ZenStack client with it.

\`\`\`ts
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { run as generate } from './generate';
import { run as info } from './info';
import { run as init } from './init';
import { run as migrate } from './migrate';
import { run as validate } from './validate';

export { db, generate, info, init, migrate };
export { db, generate, info, init, migrate, validate };
22 changes: 22 additions & 0 deletions packages/cli/src/actions/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import colors from 'colors';
import { getSchemaFile, loadSchemaDocument } from './action-utils';

type Options = {
schema?: string;
};

/**
* CLI action for validating schema without generation
*/
export async function run(options: Options) {
const schemaFile = getSchemaFile(options.schema);

try {
await loadSchemaDocument(schemaFile);
console.log(colors.green('✓ Schema validation completed successfully.'));
} catch (error) {
console.error(colors.red('✗ Schema validation failed.'));
// Re-throw to maintain CLI exit code behavior
throw error;
}
}
Loading