Commit graph

90 commits

Author SHA1 Message Date
Yiming Cao
a49c2da456
fix(cli): add "@zenstackhq/schema" package installation for "zenstack create" and "zen init" (#2437) 2026-03-03 09:43:45 -08:00
Román Benjámin
b2305cd54f
fix(cli): exclude Prisma migrations from SQLite pull (#2427) 2026-03-02 09:43:53 -08:00
Jiasheng
4629810fe6
test(proxy): add ut for proxy (#2432) 2026-03-01 22:35:28 -08:00
Jiasheng
c99c47bca4
fix(studio): add option to skip validation for computed fields when start proxy (#2429) 2026-03-01 20:13:03 -08:00
Yiming Cao
7de2af2be2
fix(cli): dynamically load pg module in "db pull" (#2421) 2026-02-27 19:48:52 -08:00
Jiasheng
4fe27ef8be
chore(cli): show notifications for generate command (#2409)
Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
2026-02-27 10:44:01 -08:00
Yiming Cao
851e744ba5
fix(cli): update version check tag to latest (#2412) 2026-02-26 20:47:51 -05:00
Jiasheng
608133aa7d
refactor(proxy): change database driver packages to optional peer dependency (#2388) 2026-02-21 13:32:57 -08:00
Yiming Cao
3e03ab1506
refactor: remove import from orm package in generated schema (#2387) 2026-02-19 18:16:13 -08:00
sanny-io
9a5f5da40c
chore: add warning for mismatched versions during zen generate (#660)
* chore: add warning for mismatched versions during `zen generate`

* chore: swap falsy checks for length checks

* chore: do not block generation upon error

* Add additional new line.

* Trigger Build

* Trigger Build

* fix: addressing PR comments

- use `createRequire` instead of dynamic import for better compatibility
- use file search to locate nearest package.json file
- use longer padding length when formatting warnings

* fix: make sure require base dir is absolute path

---------

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
2026-02-10 16:13:01 +08:00
Román Benjámin
d9cdd9c8bc
fix(introspection): preserve data validation attributes, enum ordering, and comments during db pull (#672)
* fix(introspection): preserve schema integrity during db pull

- Retain data validation attributes (e.g., @email) on fields after
  introspection (#670)
- Preserve original declaration order of enums instead of moving them
  to the end of the schema file (#669)
- Preserve triple-slash comments above enum declarations (#669)

Fixes #669, fixes #670

* fix: address PR comments

* fix(cli): improve db pull for relations and defaults

Prevents field name collisions during introspection by refining the naming strategy for self-referencing relations with multiple foreign keys.

Extends support for JSON and Bytes default values across MySQL, PostgreSQL, and SQLite providers to ensure consistent schema restoration.

Adds test cases for self-referencing models to verify the avoidance of duplicate fields.

* fix: address PR comments
2026-02-10 15:57:49 +08:00
Román Benjámin
e7a35883bc
feat: db pull implementation (#268)
* feat: initial db pull implementation

* fix: generate imports and attributes for zmodel-code-generator

* fix: add option to not exclude imports in loadDocument

* fix: continue work on db pull

* fix: missing import

* fix: rewrite model generation

generate model  from ground up and diff later

* feat: add ast factory

* fix: ast factory import order

* fix: some runtime bugs

* fix: lint fix

* fix: update zmodel code generator

- include imports in output
- fix indentaions
- include comments in output

* feat: add exclude schemas option

* feat: implement initial diff update

* fix: update format in zmodel code generator

* fix: typo

* feat: progress on database introspection and syncing

* fix: make ignore behave it does in prisma with no index models

* fix: lint fix

* feat: make all format options configurable

* fix: lint fix

* feat:  Handle the database type mapping

* fix: catch up with feature updates

- improve code styling
- enable schema support for db pull

* fix: add sqlite e2e test and fix some bugs

* fix: lint fix

* fix: formatting for e2e test schemas

* test: run db pull e2e test also for postgres

* fix: postgres instorspection schema filter

* test: update cli tests

* feat(cli): Improves database introspection and syncing

Enhances the `db pull` command with a spinner for better UX.
Adds color-coded logging to highlight important steps.
Provides more detailed output on schema changes, including deleted models, enums, added fields, and deleted attributes.
Also includes minor improvements to enum mapping and constraint handling.

* fix(cli): fixes field casing and sort issues

* chore(cli): remove temporary test script

Deletes an unused script used for experimenting with URI path resolution. Cleans up the codebase by removing development-only artifacts.

* chore: update pnpm-lock.yaml

* feat(cli): add MySQL support for schema introspection

Introduces a MySQL-specific introspection provider to support pulling existing database schemas into ZenStack.

The implementation includes logic for mapping MySQL data types to ZenStack types, handling auto-incrementing fields, and parsing MySQL-specific enum definitions. It utilizes dynamic imports for database drivers to minimize the CLI footprint for users not targeting MySQL.

* fix(cli): improve field matching logic during db pull

* feat(cli): enhance SQLite introspection with autoincrement support

* fix(cli): refine attribute generation in db pull

* test(cli): update db pull tests for SQLite specific behavior

* refactor(language): export ZModelServices type

* fix(cli): improve sqlite introspection for autoincrement and fk names

* feat(cli): enhance field matching logic during pull by using relation fields

* refactor(cli): refine relation name generation and table syncing

* test(cli): update pull tests to reflect improved schema generation

* test(cli): add MySQL support to test utility helpers

Extends the testing infrastructure to support MySQL databases.

Adds MySQL configuration defaults and environment variable overrides.
Updates the prelude generation logic to handle MySQL connection strings and provider types, enabling broader database integration testing across the CLI.

* fix(cli): omit default constraint names in table sync

Avoids explicitly declaring unique constraint names when they match the default database naming convention. This results in cleaner generated schema code by removing redundant mapping arguments.

* fix: correctly handle default values for 'text' type in PostgreSQL

* fix: sort table indexes to ensure stable schema generation

* refactor: dynamically determine supported db providers in CLI

* test: fix typo in pull test description

* chore(cli): remove debug artifacts and silence test logs

Removes hardcoded file system path debugging and unnecessary console logging from the introspector and test suites. Silences CLI command output during tests to provide a cleaner test execution environment.

* fix(cli): ensure MySQL column and index ordering

Wraps JSON_ARRAYAGG calls in subqueries with explicit ORDER BY clauses to maintain correct metadata ordering.

This addresses a limitation in MySQL versions prior to 8.0.21, where ORDER BY is not supported directly within the JSON_ARRAYAGG function, ensuring consistent introspection results across different database versions.

* fix(cli): preserve column order during MySQL pull

Ensures database columns are sorted by their ordinal position during the introspection process. This maintains the original schema structure and provides a consistent output that matches the physical database layout.

* refactor(cli): remove schema fields from MySQL queries

Eliminates redundant schema and database name fields from the MySQL introspection query. Since MySQL does not support multi-schema architectures internal to a single connection in this context, removing these fields simplifies the data structure and avoids unnecessary metadata overhead.

* fix(cli): improve MySQL introspection and index mapping

Refines the database pull process to better handle MySQL-specific patterns. Improves unique constraint detection to prevent redundant mapping attributes when default naming conventions are used.

Updates the MySQL introspection logic to correctly identify boolean types, handle timestamp precision in default values, and normalize numeric defaults. Also ensures auto-incrementing columns and primary key indexes are correctly mapped to prevent schema duplication.

* test(cli): pass provider to default prelude in tests

Ensures that the default schema prelude correctly reflects the database provider specified in test options. This prevents inconsistencies when generating test projects with non-default providers.

* fix(cli): improve MySQL introspection for types and defaults

Disables NativeEnum support for MySQL to prevent loss of schema-level enums since MySQL enums are column-specific.

Refines boolean and numeric type mapping to better handle synthetic boolean types and preserve decimal precision in default values.

Updates default value parsing logic to correctly identify unquoted strings and avoid misinterpreting numeric literals as booleans.

* fix(cli): improve MySQL default value introspection

Refines how default values are handled during database introspection for MySQL by considering the specific field type. This ensures that boolean variants and numeric literals for Float and Decimal types are correctly formatted and preserved.

Also clarifies unsupported features in the SQLite provider to improve codebase maintainability.

* test(cli): expand and reorganize db pull tests

Enhances the test suite for the database pull command by adding comprehensive coverage for common schema features and PostgreSQL-specific functionality.

Includes new test cases for:
- Restoring complex schemas from scratch, including relations and indexes
- Preserving existing imports in multi-file schema setups
- Handling PostgreSQL-specific features like multi-schema support and native enums
- Verifying schema preservation for field and table mappings

The tests are restructured for better clarity across different database providers.

* refactor: restructure introspection provider interface and attribute generation

* feat: modernize MySQL introspection provider

* feat: modernize PostgreSQL introspection provider

* feat: modernize SQLite introspection provider

* fix: improve relation field naming and default action handling

* feat: track imports and auto-format during db pull

* test: update pull tests to reflect naming and formatting improvements

* fix(cli): refactor PostgreSQL type casting and fix index order

Extracts PostgreSQL type casting logic into a reusable helper function to improve maintainability and ensure consistent attribute handling across all field types.

Adjusts the table index sorting logic to better preserve the original database creation order while maintaining the priority of unique indexes.

* fix(cli): filter out auto-generated MySQL indexes

Prevents foreign key indexes created automatically by MySQL from appearing in the introspected schema. This ensures the output reflects manually defined indexes and avoids redundancy in schema definitions.

* test(cli): support datasource extras in test utils

Enhances the test utility helpers to allow passing extra datasource properties, such as multi-schema configurations for PostgreSQL.

Refactors existing database pull tests to use these extra properties, ensuring the generated ZModel schema correctly reflects multi-schema environments while simplifying assertions.

* fix: address PR comments

* fix: address PR comments

* fix: address PR comments

* fix: address PR comments

* fix(cli): improve file path resolution in pull action

* refactor(cli): extract and enhance name casing logic

* refactor(cli): consolidate default value normalization

* feat(cli): improve enum syncing and relation naming during pull

* docs(cli): add documentation comments to SQL introspection queries

* test(cli): refactor test utilities and modernize test suites

* fix(cli): improve db pull for composite FKs and MySQL uniqueness

Enhances database introspection to correctly handle composite foreign keys by mapping columns by position rather than name alone.

Improves MySQL introspection by checking statistics tables for single-column unique indexes, ensuring accurate model generation even when column keys are ambiguous.

Ensures MySQL synthetic enum names respect requested model casing to prevent unnecessary schema mapping.

Adds comprehensive tests for composite relations and database-specific uniqueness detection.

* fix: address PR comments

* fix(cli): improve SQLite introspection for untyped columns and composite FKs

Ensures columns with no declared type are correctly mapped to Bytes following SQLite affinity rules, preventing them from being marked as Unsupported.

Updates the DDL parser to correctly identify and map constraint names for composite foreign keys. This ensures that multi-column relations are properly restored during the pull process.

Adds regression tests for both untyped columns and composite foreign key restoration.

* feat(cli): pull generated/computed columns as Unsupported type

Improves database introspection by identifying generated columns in MySQL, PostgreSQL, and SQLite. These columns are now pulled as `Unsupported` types containing their full DDL definition, preventing issues where read-only database fields were incorrectly treated as writable application-level fields.

Includes normalization for expression formatting and a fix for string literal escaping in the code generator to ensure stable schema output.

Relates to ZModel introspection consistency.

* fix(cli): Use parameterized queries for MySQL introspection

Switches from template literal interpolation to parameterized queries in MySQL introspection functions. This improves security by preventing potential SQL injection and ensures better handling of database names containing special characters.

* fix(cli): use nullish coalescing for precision check
2026-02-08 10:02:26 +08:00
Jiasheng
21659802e2
fix(proxy): add omit configuration for computed fields in ZenStackClient (#658)
* fix(proxy): add omit configuration for computed fields in ZenStackClient

* fix typo
2026-02-06 16:13:37 +08:00
Jiasheng
897a85ef78
fix(proxy): fix the problem when database url is a string literal (#645)
* fix(proxy): fix the problem when database url is a string literal

* resolve comments

* Get the string literal directly from the AST for datasource url
2026-01-31 21:39:15 +08:00
Jiasheng
b3aff66943
fix(cli): update logLevel option to accept multiple values (#639) 2026-01-30 21:09:34 +08:00
Jiasheng
f612723b8a
refactor(cli): replace dynamic function call eval with regex match for datasource url (#637)
* refactor(cli): replace dynamic function call eval with regex match for datasource url

* fix(proxy): improve regex for env() function call matching
2026-01-30 20:49:27 +08:00
Copilot
f9475e66e7
fix(cli): redact credentials from database URL console logs (#626)
* feat(cli): add mysql support to proxy

* Initial plan

* fix: redact credentials from database URL logs

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>

* fix: also redact SQLite URLs for consistency

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>

* Merge dev branch and remove SQLite URL redaction

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>

* docs: clean up merge conflicts by rebasing on correct base

* Push new branch to create fresh PR

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>

* clean up agent mess

---------

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-01-30 16:17:50 +08:00
Jiasheng
a42efc178e
fix(cli): handle error for proxy server (#634)
* fix(cli): handle error for proxy server

* fix(cli): update default port for ZenStack proxy server from 8008 to 2311
2026-01-30 16:02:41 +08:00
Jiasheng
c5da34924c
feat(cli): add dotenv support (#629)
* feat(cli): add dotenv support

* [WIP] Update dotenv support implementation in CLI based on review (#631)

* Initial plan

* fix(cli): move dotenv import to top of file

Co-authored-by: jiashengguo <16688722+jiashengguo@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: jiashengguo <16688722+jiashengguo@users.noreply.github.com>

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
2026-01-30 13:39:45 +08:00
Yiming Cao
6719d4d494
feat(cli): add mysql support to proxy (#625) 2026-01-27 16:43:53 +08:00
Yiming Cao
e8717e422f
fix(cli): properly quote prisma exec path (#624)
fixes #609
2026-01-27 16:25:22 +08:00
Jiasheng
94c5c8a1d5
feat(cli): ZenStack proxy (#597)
* feat(cli): ZenStack proxy

* add the missing change

* resolve comments

* feat(cli): add alias for proxy command and improve console messages

* fix(cli): update output option description for ZenStack proxy command
2026-01-18 21:13:14 +08:00
FTB_lag
3fde86d9cf
feat(cli): implement watch mode for generate (#554)
* feat(cli): implement watch mode for generate

* chore(root): update pnpm-lock.yaml

* chore(cli): track all model declaration and removed paths, logs in past tense

* fix(cli): typo, unused double array from

* fix(orm): preserve zod validation errors when validating custom json types

* update

* chore(cli): move import, fix parallel generation on watch

* feat(common-helpers): implement single-debounce

* chore(cli): use single-debounce for debouncing

* feat(common-helpers): implement single-debounce

* fix(common-helpers): re run single-debounce

---------

Co-authored-by: Yiming Cao <yiming@whimslab.io>
Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
2026-01-07 21:52:50 +08:00
FTB_lag
b83ed8d806
fix: use latest version for create-zenstack and cli init (#536) 2025-12-28 12:00:12 +08:00
Yiming Cao
9c846645ef
chore: update doc link (#534)
* chore: update README badge

* update
2025-12-24 21:20:30 +08:00
Yiming Cao
b0178a5893
chore: bump version 3.0.0 (#522)
* chore: bump version 3.0.0

* update

* update README

* update
2025-12-24 16:48:58 +08:00
Yiming Cao
351780c880
refactor(cli): support loading typescript plugins (#519)
* refactor(cli): support loading typescript plugins

* update

* update

* update

* update
2025-12-18 23:09:57 +08:00
Yiming Cao
b53ff2bdce
fix(cli): use correct yarn command to install packages (#518)
* fix(cli): use correct yarn command to install packages

* Update packages/cli/src/actions/init.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-18 21:47:50 +08:00
Yiming Cao
fe109c8926
fix(orm):typing issue with deeply nested included relations (#504)
fixes #503
2025-12-16 20:59:53 +08:00
Yiming Cao
a147840b51
chore: vscode version, cli template update (#502) 2025-12-16 17:03:50 +08:00
Yiming Cao
53d1a75169
chore: improve "npm create zenstack" template (#491) 2025-12-14 14:33:37 +08:00
Yiming Cao
61719d5d48
chore(cli): suppress prisma update message (#456)
* chore(cli): suppress prisma update message

* update
2025-12-01 20:54:26 +08:00
Yiming Cao
7e5786431c
fix: misc fixes addressing PR comments (#438)
* fix: misc fixes addressing PR comments

* update
2025-11-20 09:35:48 -08:00
Yiming Cao
f5c883178e
chore(cli): improve help text of migrate reset (#433) 2025-11-19 22:25:10 -08:00
Yiming Cao
7a90d87a50
fix(cli): require datasource url for running migration and db push commands (#431) 2025-11-19 18:44:14 -08:00
Yiming Cao
1d4011b8fc
feat(cli): db seed command (#428)
* feat(cli): db seed command

* add tests

* improve help text
2025-11-19 13:04:37 -08:00
Yiming Cao
710cb8bb36
chore: minor fixes (#421) 2025-11-17 21:50:02 -08:00
Yiming Cao
fcd557312d
feat(cli): add "format" command (#419)
* feat(cli): add "format" command

* better-auth: format generated schema

* update
2025-11-17 21:14:25 -08:00
Yiming Cao
c27e25de36
feat: better-auth adapter (#416)
* feat: better-auth adapter

* address pr comments
2025-11-17 16:57:13 -08:00
Yiming Cao
160437a949
feat(cli): allow importing with file extension in generated schema (#412)
fixes #378
2025-11-15 14:49:57 -08:00
Yiming Cao
70b970fcb9
chore: import CLI help text (#411)
* chore: import CLI help text

* update
2025-11-15 14:17:14 -08:00
Yiming Cao
dc53b11040
fix(cli): add a npx/bunx fallback for running prisma commands (#409) 2025-11-15 09:10:20 -08:00
Yiming Cao
e4885b0fb4
fix(cli): improve compatibility for executing prisma commands (#401)
* fix(cli): improve compatibility for executing prisma commands

* update
2025-11-14 15:54:18 -08:00
Yiming Cao
170535a6f9
feat(schema): add option for generating lite schema (#357)
* feat(schema): add option for generating lite schema

* fix --lite-only option and add CLI test
2025-11-03 09:25:23 -08:00
Yiming Cao
f9cbedc255
feat(server): migrate tanstack-start adapter (#344)
* feat(server): migrate tanstack-start adapter

* prettier format
2025-10-29 19:26:12 -07:00
Yiming Cao
cccd34491d
refactor: rename runtime package to orm (#332) 2025-10-23 22:07:55 -07:00
Yiming Cao
05bf237833
fix: clean up plugin loading and fix VSCode extension (#312)
* fix: clean up plugin loading and fix VSCode extension

* address pr comments
2025-10-17 12:41:11 -07:00
sanny-io
6f48c74344
fix: skip generators for migrate reset (#308) 2025-10-17 12:39:00 -07:00
Yiming Cao
59dfa73f01
refactor: move policy attributes to its own zmodel (#307)
* refactor: move policy attributes to its own zmodel

* update

* update

* fix tests
2025-10-16 21:52:22 -07:00
sanny-io
d3a560681b
fix: prisma plugin not respecting zenstack.output (#298)
* fix: prisma plugin not respecting `zenstack.output`

* chore: add test for #295

---------

Co-authored-by: = <=>
2025-10-15 18:13:54 -07:00