@@ -339,6 +339,7 @@ export class Query<M extends Model = Model> {
339339 * Set the relationships that should be eager loaded.
340340 */
341341 with ( name : string , callback : EagerLoadConstraint = ( ) => { } ) : this {
342+ this . getNewHydrated = true
342343 this . eagerLoad [ name ] = callback
343344
344345 return this
@@ -645,8 +646,9 @@ export class Query<M extends Model = Model> {
645646 * Revive single model from the given schema.
646647 */
647648 reviveOne ( schema : Element ) : Item < M > {
648- const id = this . model . $getIndexId ( schema )
649+ this . getNewHydrated = false
649650
651+ const id = this . model . $getIndexId ( schema )
650652 const item = this . commit ( 'get' ) [ id ] ?? null
651653
652654 if ( ! item )
@@ -711,13 +713,17 @@ export class Query<M extends Model = Model> {
711713 * Create and persist model with default values.
712714 */
713715 new ( persist = true ) : M | null {
714- const model = this . hydrate ( { } , { operation : persist ? 'get ' : 'set ' } )
716+ let model = this . hydrate ( { } , { operation : persist ? 'set ' : 'get ' } )
715717 const isCreating = model . $self ( ) . creating ( model )
716718 const isSaving = model . $self ( ) . saving ( model )
717719 if ( isCreating === false || isSaving === false )
718720 return null
719721
722+ if ( model . $isDirty ( ) )
723+ model = this . hydrate ( model . $getAttributes ( ) , { operation : persist ? 'set' : 'get' } )
724+
720725 if ( persist ) {
726+ this . hydratedDataCache . set ( this . model . $entity ( ) + model . $getKey ( undefined , true ) , this . hydrate ( model . $getAttributes ( ) , { operation : 'get' } ) )
721727 model . $self ( ) . created ( model )
722728 model . $self ( ) . saved ( model )
723729 this . commit ( 'insert' , this . compile ( model ) )
@@ -780,7 +786,7 @@ export class Query<M extends Model = Model> {
780786 for ( const id in elements ) {
781787 const record = elements [ id ]
782788 const existing = currentData [ id ]
783- const model = existing
789+ let model = existing
784790 ? this . hydrate ( { ...existing , ...record } , { operation : 'set' , action : 'update' } )
785791 : this . hydrate ( record , { operation : 'set' , action : 'save' } )
786792
@@ -789,6 +795,9 @@ export class Query<M extends Model = Model> {
789795 if ( isSaving === false || isUpdatingOrCreating === false )
790796 continue
791797
798+ if ( model . $isDirty ( ) )
799+ model = this . hydrate ( model . $getAttributes ( ) , { operation : 'set' , action : existing ? 'update' : 'save' } )
800+
792801 afterSavingHooks . push ( ( ) => model . $self ( ) . saved ( model , record ) )
793802 afterSavingHooks . push ( ( ) => existing ? model . $self ( ) . updated ( model , record ) : model . $self ( ) . created ( model , record ) )
794803 newData [ id ] = model . $getAttributes ( )
@@ -1017,8 +1026,7 @@ export class Query<M extends Model = Model> {
10171026 * an update event trigger in vue if the object is used.
10181027 */
10191028 protected getHydratedModel ( record : Element , options ?: ModelOptions ) : M {
1020- const modelKey = this . model . $getKeyName ( )
1021- const id = ( ! isArray ( modelKey ) ? [ modelKey ] : modelKey ) . map ( key => record [ key ] ) . join ( '' )
1029+ const id = this . model . $getKey ( record , true )
10221030 const savedHydratedModel = id && this . hydratedDataCache . get ( this . model . $entity ( ) + id )
10231031
10241032 if (
0 commit comments