fix: return DateFieldBuilder from defaultNow() and onUpdateNow() for proper method chaining#22
Merged
dustintownsend merged 1 commit intomizzle-dev:mainfrom Feb 11, 2026
Conversation
…proper method chaining
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.
Bug Description
The DateFieldBuilder.defaultNow() method incorrectly returns
new FieldBuilder(...)instead ofnew DateFieldBuilder(...). Theas anycast masks this at compile time, but at runtime the returned object is a plain FieldBuilder that lacks DateFieldBuilder-specific methods like onUpdateNow(). This breaks the expected method chaining for date fields: date().defaultNow().onUpdateNow() is a common pattern for createdAt/updatedAt timestamps that currently fails.Severity: medium
Root Cause
In packages/mizzle-orm/src/schema/field-builders-primitive.ts, lines 115-120, the DateFieldBuilder.defaultNow() method returns
new FieldBuilder(this._config.type, {...})instead ofnew DateFieldBuilder(...). Theas anycast masks this type error at compile time, making TypeScript believe it returns a DateFieldBuilder. However, at runtime the returned object is a plain FieldBuilder instance which lacks DateFieldBuilder-specific methods like onUpdateNow(). The same bug exists in onUpdateNow() at lines 122-127 - it also returnsnew FieldBuilder(...)instead ofnew DateFieldBuilder(...). This means the common timestamp patterndate().defaultNow().onUpdateNow()fails because after calling defaultNow(), the returned FieldBuilder has no onUpdateNow method.Fix
Modified DateFieldBuilder in packages/mizzle-orm/src/schema/field-builders-primitive.ts: (1) Updated constructor to accept optional config parameter, (2) Changed defaultNow() to return new DateFieldBuilder instead of new FieldBuilder, (3) Changed onUpdateNow() to return new DateFieldBuilder instead of new FieldBuilder. This ensures method chaining preserves the DateFieldBuilder type at runtime.
Regression Test
Added 2 tests in packages/mizzle-orm/src/schema/tests/fields.test.ts: 'should support defaultNow().onUpdateNow() chaining for timestamp fields' and 'should support onUpdateNow().defaultNow() chaining in reverse order'. These tests verify the common timestamp pattern works correctly.
Verification
Tests pass locally. CI will run on PR.