feat: Drizzle 1.0 type safety fixes#234
Draft
austinm911 wants to merge 21 commits intorocicorp:0xcadams/drizzle-betafrom
Draft
feat: Drizzle 1.0 type safety fixes#234austinm911 wants to merge 21 commits intorocicorp:0xcadams/drizzle-betafrom
austinm911 wants to merge 21 commits intorocicorp:0xcadams/drizzle-betafrom
Conversation
* fix: remove warn logs for defaults * Revert "fix: remove warn logs for defaults" This reverts commit 901b09f. * chore: implement suppress warnings tests * feat: implement `suppressDefaultsWarning` flag
- Add imports for RelationV2, OneV2, ManyV2 from drizzle-orm/relations - Add isTablesRelationalConfig() detection for new API output - Add getRelationInfo() helper to normalize old/new API differences - Update drizzleZeroConfig to process TablesRelationalConfig - Update findRelationSourceAndDestFields for V2 relations - Maintain backwards compatibility with old relations() API
* fix: undeprecate row types * test: update * chore: format
- Updated drizzle-* deps to 1.0.0-beta series - Took tooling updates (oxlint, prettier) - Kept drizzle-1.0 branch structure for db/package.json
- Add IsTimestampDataType, IsBigIntDataType, IsStringNumericDataType predicates - Add IsExactType helper to distinguish default types from $type<T>() overrides - Add extractBaseType and mapDrizzle1DataTypeToZero runtime utilities - Update CustomType and ZeroMappedCustomType for Drizzle 1.0 column structure - Remove 'custom' from base type mapping (falls through to getSQLType) Drizzle 1.0 uses compound dataTypes like 'string uuid', 'object date', etc. These changes ensure proper mapping to Zero types while preserving custom type overrides.
- Use UseAliasDefinedOutsideCurrentScope flag first to preserve type aliases - Fall back to InTypeAlias for expanding custom interface types - Fixes jsonb columns incorrectly expanding ReadonlyJSONValue to union The InTypeAlias flag was expanding ReadonlyJSONValue to its full union definition with import() paths, which failed isSafeResolvedType checks.
- Update interval/cidr/macaddr/inet/point/line/geometry tests (now supported in Drizzle 1.0, no longer emit warnings) - Add drizzle-1.0.test.ts for compound dataType utility tests - Add type-level tests for timestamp/bigint/numeric → number mapping - Add type-level tests for $type<T>() preservation - Update snapshots for ReadonlyJSONValue preservation - Move backtick string literal to safe types (valid literal)
- Add relations to drizzle() config (required in Drizzle 1.0)
- Update where clause syntax to use object filters
(before: where: (t, {eq}) => eq(t.id, x), after: where: {id: x})
Comprehensive documentation of: - All changes made for Drizzle 1.0 compatibility - Drizzle 1.0 internal changes (compound dataTypes, column structure) - Type mapping logic and decision tree - Commands for testing and development
The blocklist approach broke CLI tests that expect ZeroCustomType<...> helper in generated output. User-defined $type<T>() still works via the ZeroCustomType helper which resolves at compile time. Also documents beta.11 TypeScript type issues (staying on beta.10).
Runtime changes (src/tables.ts): - Detect array columns via 'dimensions' property (Drizzle 1.0 style) - Map array columns to 'json' type in Zero - Prevent enum arrays from being treated as enums Test corrections (tests/tables.test.ts): - Fix $type<T>() usage on array columns to use element type, not array type Drizzle's .array() already wraps the type automatically - Document Drizzle 1.0 limitation: nested .array().array() only tracks single dimension at type level (runtime still receives correct 2D data) All 633 tests now pass.
- Add RESERVED_TYPE_NAMES set with built-in types (Record, Array, etc.) and utility types (Partial, Readonly, etc.) - Tables like 'records' now generate 'RecordRow' instead of 'Record' to avoid shadowing TypeScript's built-in Record<K,V> utility type - Remove ReadonlyJSONObject from isSafeResolvedType special case since it's not auto-imported (falls back to ZeroCustomType) - Add test to verify reserved type name handling Fixes circular reference error when generated schema uses Record<...> for JSON column types while also exporting 'type Record = ...'.
- Add knownSafeTypeAliases set for ReadonlyJSONValue and ReadonlyJSONObject
- Allow 'readonly' modifier in object type index signatures
- Track and import both ReadonlyJSONValue and ReadonlyJSONObject from @rocicorp/zero
- Add comprehensive tests for complex $type<> structures:
- Nested objects (UserProfile with nested address)
- Discriminated unions (NotificationEvent)
- Arrays of complex objects (FormFieldConfig[])
- Objects with ReadonlyJSONValue (WorkflowStep)
- Type alias chains (RecordData -> JsonObject -> ReadonlyJSONObject)
- Verify RecordRow type name doesn't conflict with built-in Record<K,V>
Fixes issue where type alias chain to ReadonlyJSONObject resolved to {}.
851edaa to
b420495
Compare
Member
|
I am sorry but this is such a massive PR, it’s going to be very hard for me to review this and merge. I will use it as a reference but 4k lines changing won’t be something I can review meaningfully. |
Contributor
Author
|
Absolutely no worries! I did not mean to overwhelm or expect you to review much. I just figured that if it's helpful in any way as a reference, then great. Thanks for your work ! |
- Update devDependencies to use 'beta' dist-tag (resolves to 1.0.0-beta.14) - Update peerDependency to >=1.0.0-beta.2 - Fix test-utils type issue with large table insert overload - Reformat with oxfmt - All 647 tests pass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Drizzle 1.0 Support
Human Summary
This is entirely claude generated so buyer beware, however I am migrating a project to Drizzle 1.0 beta and this seems to mostly work, except for some missing types in a JSON field.
I figured I'd keep this up in draft form in case you'd like to reference anything or take over to clean up.
Summary
This PR adds support for Drizzle ORM 1.0 (currently
^1.0.0-beta.10), which introduced significant internal changes to column type representation.Status: All 633 tests passing, 0 type errors. Working in my production app with Drizzle 1.0 beta, generating correct Zero schemas with proper types.
What Changed in Drizzle 1.0
Drizzle 1.0 changed how column types are represented internally:
dataType'string','number''string uuid','object date'columnTypecolumn._object$type<T>()_.$type_.datadirectlyrelations()helperdefineRelations()APIChanges Made
Core Type System (
src/)IsTimestampDataType,IsBigIntDataType,IsStringNumericDataTypeto detect Drizzle 1.0's compound dataTypesIsExactTypehelper: Distinguishes default types (Date, string, bigint) from explicit$type<T>()overridesextractBaseType()andmapDrizzle1DataTypeToZero()for parsing compound dataTypesCustomType/ZeroMappedCustomType: Handle new column structure while preserving backwards compatibilityType Resolution (
src/cli/)ReadonlyJSONValuebeing incorrectly expanded (now usesUseAliasDefinedOutsideCurrentScopeflag first)Previously Unsupported Types
These types now work in Drizzle 1.0 (they have valid compound dataTypes):
interval,cidr,macaddr,inet→stringpoint,line,geometry→jsonType Mapping Logic
Key insight:
$type<T>()overrides are preserved. If you usetimestamp().$type<ISODateString>(), the custom type is kept rather than being converted tonumber.Testing
New tests added:
tests/drizzle-1.0.test.ts- Compound dataType utility teststests/types.test.tsfor timestamp/bigint/numeric mappingQuestions for Maintainer
Backwards compatibility: The changes attempt to support both Drizzle 0.x and 1.0 column structures. Is this the right approach, or should we drop 0.x support?
defineRelations()vsrelations(): Both APIs are supported. The oldrelations()still works butdefineRelations()is the new recommended approach.Type coercion: Timestamps/dates/bigints all map to
numberin Zero. This matches how Zero stores these values. Is this correct?Custom type preservation: When
$type<T>()is used, we preserve the custom type instead of converting. This seemed like the right UX but want to confirm.How I Tested
Happy to address any feedback or concerns. This is based on the earlier
0xcadams/drizzle-betabranch with additional fixes for type safety issues discovered during testing.