Commit graph

40 commits

Author SHA1 Message Date
sanny-io
97d674ecdc
feat: phone validation (#2672) 2026-05-23 21:27:54 -07:00
sanny-io
76febc90d4
fix: invalid prisma schema with format args (#2677)
Some checks failed
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
2026-05-22 08:33:41 -07:00
Román Benjámin
e492c937dd
fix(cli): add missing opposite relation fields during db pull when multiple FKs target the same model (#2652) 2026-05-13 09:52:15 -07:00
Yiming Cao
9cef0272e7
chore: upgrade to TypeScript 6 (#2629) 2026-04-29 20:41:27 -07:00
Jesús Gómez
b749cd0012
fix(cli): fix race condition during push db (#2491) (#2587) 2026-04-21 09:22:12 -07:00
Yiming Cao
b8209d7eeb
chore: migrate from tsup to tsdown (#2580) 2026-04-15 12:13:34 -07:00
Elliot Shepherd
a017550dac
fix: a few small fixes (#2487)
Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
2026-03-23 16:58:26 -07:00
Yiming Cao
3c181f6832
fix(cli): report error when plugin module cannot be resolved (#2447)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 18:14:00 -08:00
Jiasheng
4629810fe6
test(proxy): add ut for proxy (#2432) 2026-03-01 22:35:28 -08:00
Yiming Cao
3e03ab1506
refactor: remove import from orm package in generated schema (#2387) 2026-02-19 18:16:13 -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
sanny-io
2b3d7d6f61
feat: ignore argument for @updatedAt (#572)
* feat: `ignore` argument for `@updatedAt`

* chore: add tests

* Trigger Build

* Check test.

* Use `getTime`

* Retry.

* Retry.

* Retry.

* Retry.

* Retry.

* Retry.

* Retry.

* Clean up.

* Document param.

* Extract to function.

* Relocate function.

* Adjust formatting.

* Use `getAttributeArg`

* Use `$resolvedParam`

* Null check.

* fix: resolve a merge error

* fix: delay data clone to right before changing it

---------

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
2026-01-30 16:16:16 +08:00
Mike Willbanks
2172614e0e
custom procedures (#551)
* feat: custom procs

* chore: cleanup

* fix: remove $procedures from client

* fix: failing test due to previous alias

* feat(custom-procs)!: make procedures envelope-only via $procs

- Switch procedure calls to `db.$procs.name({ args: {...} })` (no positional args)
- Remove legacy `$procedures` alias entirely (client API + server routing/logging)
- Validate procedure envelope input (`args` object, required/unknown keys)
- Keep TanStack Query procedure hooks as `(args, options)` (with conditional args optionality)
- Update server/ORM/client tests for the envelope API

* fix: code review feedback

* fix: code review comments

* fix: coderabbit review comments

* fix: remove useless proxy method

* test: add a couple of e2e tests that verify both typing and runtime

* test: improve e2e tests

* test: add missing mutation flag

* regenerate test schema

* refactor: procedure params generation fix and type refactors

- Simplified procedure's params definition from a tuple an object, since procs are now called with an envelop now

- Refactored procedure related typing to make them more consistent with other CURD types (that usually takes the schema as the first type parameter, and a name as the second)

- Moved detailed procedure's types to "crud-types" where other ORM client detailed types are defined

- Removed some type duplication from hooks side

- Updated the "orm" sample to demonstrate procedures

* fix: disable infinite custom proc queries for now

---------

Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
2026-01-08 11:21:51 +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
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
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
cccd34491d
refactor: rename runtime package to orm (#332) 2025-10-23 22:07:55 -07:00
Yiming Cao
e0040cb974
chore: test refactor (#303) 2025-10-15 18:57:56 -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
sanny-io
348abee261
fix: literal function arguments with @default() (#288)
* fix: literal function arguments with `@default()`

* Add test.

---------

Co-authored-by: = <=>
2025-10-08 20:54:54 -07:00
Yiming Cao
e013774347
chore: fix telemetry env var for publish CI (#230)
* chore: fix telemetry env var for publish CI

* update test and config

* update

* remove unused settings
2025-09-02 18:02:47 +08:00
Yiming Cao
3faa2cffb5
fix: output path handling for prisma plugin (#194) 2025-08-21 13:30:50 +08:00
Yiming Cao
3cdadd7429
chore: misc changes (#186)
* chore: misc changes

* update

* fix tests

* fix lint

* update test

* update
2025-08-19 15:08:33 +08:00
Yiming Cao
98668e881d
feat: cli plugin support (#181)
* feat: cli plugin support

* update

* update

* more fixes
2025-08-17 16:35:07 +08:00
Yiming Cao
1ac89c1cba
feat: add "migrate resolve" command to CLI (#171)
* feat: add "migrate resolve" command to CLI

* Update packages/cli/src/index.ts

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

* add test

* update test

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-08-15 17:00:08 +08:00
Yiming Cao
0df015ad8d
feat: implement zmodel import (#126)
* feat: implement zmodel import

* addressing PR comments

* update
2025-07-30 16:45:55 +08:00
Yiming Cao
cbf1ce3366
feat: implement delegate models (create + read) (#110)
* feat: implement delegate models (create + read)

* update

* update

* update
2025-07-23 22:54:28 +08:00
Yiming Cao
39cf629b0c
feat: implementing mixin (#106)
* feat: implementing mixin

* format

* update
2025-07-21 19:48:48 +08:00
Yiming Cao
8df3f88f55
feat: cli validate command (#103) 2025-07-17 22:46:57 +08:00
Yiming Cao
7304cd22ae
chore: update zod, add several helpers, CLI loads config from pkg.json (#84)
* chore: update zod, add several helpers, CLI loads config from pkg.json

* add test

* addressing comments
2025-07-11 11:45:07 +08:00
Yiming Cao
4f7df7ed12
fix: schema generation for unsupported field type (#64)
* fix: schema generation for unsupported field type

* fix build

* update

* more tests

* update
2025-07-01 15:48:02 +02:00
Yiming Cao
72de807b5a
test: more test cases (#63) 2025-07-01 10:37:07 +02:00
Yiming Cao
6cd7e2eff1
refactor: get rid of ds config provider from generated schema (#28)
* refactor: get rid of ds config provider from generated schema

* update
2025-06-17 21:03:48 +08:00
Yiming Cao
22246e87c9
chore: enable lint and CI (#17)
* chore: enable lint and CI

* get rid of cyclic dependencies

* add missing eslint plugin packages

* fix build
2025-06-13 09:49:11 +08:00
Yiming Cao
b4321e7728
feat: many-to-many relations (#15)
* feat: many-to-many relation (sqlite only)

* postgres support
2025-06-08 22:24:59 -07:00
Yiming Cao
e35a0aed57
getting more policy test cases to pass (#3) 2025-05-12 18:17:23 -07:00
Yiming Cao
52aec66c95
feat: got policies to work with todo sample (#1)
* WIP

* got todo sample running with policies
2025-05-08 18:35:38 -07:00
ymc9
8875f06836 refactor: implement a custom kysely executor for better control how queries are executed 2025-04-22 22:26:15 -05:00