Skip to content

Comments

fix: return DateFieldBuilder from defaultNow() and onUpdateNow() for proper method chaining#22

Merged
dustintownsend merged 1 commit intomizzle-dev:mainfrom
CaptnClaw:bugfix/date-field-builder-chain
Feb 11, 2026
Merged

fix: return DateFieldBuilder from defaultNow() and onUpdateNow() for proper method chaining#22
dustintownsend merged 1 commit intomizzle-dev:mainfrom
CaptnClaw:bugfix/date-field-builder-chain

Conversation

@CaptnClaw
Copy link
Contributor

Bug Description

The DateFieldBuilder.defaultNow() method incorrectly returns new FieldBuilder(...) instead of new DateFieldBuilder(...). The as any cast 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 of new DateFieldBuilder(...). The as any cast 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 returns new FieldBuilder(...) instead of new DateFieldBuilder(...). This means the common timestamp pattern date().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.

@dustintownsend dustintownsend merged commit 5b7b42b into mizzle-dev:main Feb 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants