File tree Expand file tree Collapse file tree
packages/mizzle-orm/src/schema Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -132,6 +132,22 @@ describe('Field Builders', () => {
132132 expect ( field . _config . onUpdateNow ) . toBe ( true ) ;
133133 } ) ;
134134
135+ it ( 'should support defaultNow().onUpdateNow() chaining for timestamp fields' , ( ) => {
136+ // Regression test: defaultNow() must return DateFieldBuilder so onUpdateNow() is available
137+ // This pattern is common for updatedAt fields that need both default and auto-update behavior
138+ const field = date ( ) . defaultNow ( ) . onUpdateNow ( ) ;
139+ expect ( field . _config . defaultNow ) . toBe ( true ) ;
140+ expect ( field . _config . onUpdateNow ) . toBe ( true ) ;
141+ expect ( field . _config . type ) . toBe ( FieldType . DATE ) ;
142+ } ) ;
143+
144+ it ( 'should support onUpdateNow().defaultNow() chaining in reverse order' , ( ) => {
145+ // Ensure chaining works in either order
146+ const field = date ( ) . onUpdateNow ( ) . defaultNow ( ) ;
147+ expect ( field . _config . defaultNow ) . toBe ( true ) ;
148+ expect ( field . _config . onUpdateNow ) . toBe ( true ) ;
149+ } ) ;
150+
135151 it ( 'should support soft delete flag' , ( ) => {
136152 const field = date ( ) . softDeleteFlag ( ) ;
137153 expect ( field . _config . isSoftDeleteFlag ) . toBe ( true ) ;
Original file line number Diff line number Diff line change @@ -123,20 +123,20 @@ export class DateFieldBuilder<TConfig extends FieldConfigState = EmptyConfig>
123123 extends FieldBuilder < Date , TConfig , DateFieldBuilder < TConfig > >
124124 implements IDateFieldBuilder < TConfig >
125125{
126- constructor ( ) {
127- super ( FieldType . DATE ) ;
126+ constructor ( config : Partial < import ( '../types/field' ) . FieldConfig < Date > > = { } ) {
127+ super ( FieldType . DATE , config ) ;
128128 }
129129
130130 defaultNow ( ) : DateFieldBuilder < TConfig & { hasDefaultNow : true ; hasDefault : true } > {
131- return new FieldBuilder ( this . _config . type , {
131+ return new DateFieldBuilder ( {
132132 ...this . _config ,
133133 defaultNow : true ,
134134 defaultValue : ( ) => new Date ( ) ,
135135 } ) as any ;
136136 }
137137
138138 onUpdateNow ( ) : DateFieldBuilder < TConfig & { hasOnUpdateNow : true } > {
139- return new FieldBuilder ( this . _config . type , {
139+ return new DateFieldBuilder ( {
140140 ...this . _config ,
141141 onUpdateNow : true ,
142142 } ) as any ;
You can’t perform that action at this time.
0 commit comments