@@ -106,6 +106,8 @@ export class Query<M extends Model = Model> {
106106
107107 protected getNewHydrated = false
108108
109+ protected hydrationKey ?: string
110+
109111 /**
110112 * Hydrated models. They are stored to prevent rerendering of child components.
111113 */
@@ -114,28 +116,29 @@ export class Query<M extends Model = Model> {
114116 /**
115117 * Create a new query instance.
116118 */
117- constructor ( database : Database , model : M , cache : WeakCache < string , Collection < M > | GroupedCollection < M > > | undefined , hydratedData : Map < string , M > , pinia ?: Pinia ) {
119+ constructor ( database : Database , model : M , cache : WeakCache < string , Collection < M > | GroupedCollection < M > > | undefined , hydratedData : Map < string , M > , pinia ?: Pinia , hydrationKey ?: string ) {
118120 this . database = database
119121 this . model = model
120122 this . pinia = pinia
121123 this . cache = cache
122124 this . hydratedDataCache = hydratedData
123125 this . getNewHydrated = false
126+ this . hydrationKey = hydrationKey
124127 }
125128
126129 /**
127130 * Create a new query instance for the given model.
128131 */
129132 newQuery ( model : string ) : Query < M > {
130133 this . getNewHydrated = true
131- return new Query < M > ( this . database , this . database . getModel ( model ) , this . cache , this . hydratedDataCache , this . pinia )
134+ return new Query < M > ( this . database , this . database . getModel ( model ) , this . cache , this . hydratedDataCache , this . pinia , this . hydrationKey )
132135 }
133136
134137 /**
135138 * Create a new query instance with constraints for the given model.
136139 */
137140 newQueryWithConstraints ( model : string ) : Query < M > {
138- const newQuery = new Query < M > ( this . database , this . database . getModel ( model ) , this . cache , this . hydratedDataCache , this . pinia )
141+ const newQuery = new Query < M > ( this . database , this . database . getModel ( model ) , this . cache , this . hydratedDataCache , this . pinia , this . hydrationKey )
139142
140143 // Copy query constraints
141144 newQuery . eagerLoad = { ...this . eagerLoad }
@@ -153,7 +156,7 @@ export class Query<M extends Model = Model> {
153156 * Create a new query instance from the given relation.
154157 */
155158 newQueryForRelation ( relation : Relation ) : Query < M > {
156- return new Query < M > ( this . database , relation . getRelated ( ) as M , this . cache , new Map < string , M > ( ) , this . pinia )
159+ return new Query < M > ( this . database , relation . getRelated ( ) as M , this . cache , new Map < string , M > ( ) , this . pinia , this . hydrationKey )
157160 }
158161
159162 /**
@@ -482,20 +485,12 @@ export class Query<M extends Model = Model> {
482485 */
483486 get < T extends 'group' | 'collection' = 'collection' > ( triggerHook ?: boolean ) : T extends 'group' ? GroupedCollection < M > : Collection < M >
484487 get ( triggerHook = true ) : Collection < M > | GroupedCollection < M > {
488+ this . hydrationKey = this . hydrationKey ?? this . generateHydrationKey ( )
485489 if ( ! this . fromCache || ! this . cache ) { return this . internalGet ( triggerHook ) }
486490
487491 const key = this . cacheConfig . key
488492 ? this . cacheConfig . key + JSON . stringify ( this . cacheConfig . params )
489- : generateKey ( this . model . $entity ( ) , {
490- where : this . wheres ,
491- groups : this . groups ,
492- orders : this . orders ,
493- eagerLoads : this . eagerLoad ,
494- skip : this . skip ,
495- take : this . take ,
496- hidden : this . hidden ,
497- visible : this . visible ,
498- } )
493+ : this . hydrationKey
499494 const result = this . cache . get ( key )
500495
501496 if ( result ) { return result }
@@ -582,6 +577,19 @@ export class Query<M extends Model = Model> {
582577 return models . filter ( model => comparator ( model ) )
583578 }
584579
580+ protected generateHydrationKey ( ) : string {
581+ return generateKey ( this . model . $entity ( ) , {
582+ where : this . wheres ,
583+ groups : this . groups ,
584+ orders : this . orders ,
585+ eagerLoads : this . eagerLoad ,
586+ skip : this . skip ,
587+ take : this . take ,
588+ hidden : this . hidden ,
589+ visible : this . visible ,
590+ } )
591+ }
592+
585593 /**
586594 * Get comparator for the where clause.
587595 */
@@ -1019,8 +1027,7 @@ export class Query<M extends Model = Model> {
10191027 const isDeleting = currentModel . $self ( ) . deleting ( currentModel )
10201028
10211029 if ( isDeleting === false ) { notDeletableIds . push ( currentModel . $getIndexId ( ) ) } else {
1022- this . hydratedDataCache . delete ( 'set' + this . model . $entity ( ) + currentModel . $getIndexId ( ) )
1023- this . hydratedDataCache . delete ( 'get' + this . model . $entity ( ) + currentModel . $getIndexId ( ) )
1030+ this . hydratedDataCache . delete ( this . model . $entity ( ) + currentModel . $getIndexId ( ) )
10241031 afterHooks . push ( ( ) => currentModel . $self ( ) . deleted ( currentModel ) )
10251032 this . checkAndDeleteRelations ( currentModel )
10261033 }
@@ -1066,11 +1073,11 @@ export class Query<M extends Model = Model> {
10661073 */
10671074 protected getHydratedModel ( record : Element , options ?: ModelOptions ) : M {
10681075 const id = this . model . $entity ( ) + this . model . $getKey ( record , true )
1069- const operationId = options ?. operation + id
1076+ const operationId = id
10701077 let savedHydratedModel = this . hydratedDataCache . get ( operationId )
10711078
1072- if ( options ?. action === 'update' ) {
1073- this . hydratedDataCache . delete ( 'get' + id )
1079+ if ( options ?. action === 'update' || this . hydrationKey === undefined ) {
1080+ this . hydratedDataCache . delete ( id )
10741081 savedHydratedModel = undefined
10751082 }
10761083
@@ -1081,10 +1088,12 @@ export class Query<M extends Model = Model> {
10811088
10821089 const modelByType = this . model . $types ( ) [ record [ this . model . $typeKey ( ) ] ]
10831090 const getNewInsance = ( newOptions ?: ModelOptions ) => ( modelByType ? modelByType . newRawInstance ( ) as M : this . model )
1084- . $newInstance ( record , { relations : false , ...( options || { } ) , ...newOptions } )
1091+ . $newInstance ( record , { ...( options || { } ) , ...newOptions , relations : false } )
10851092 const hydratedModel = getNewInsance ( )
10861093
1087- if ( isEmpty ( this . eagerLoad ) && options ?. operation !== 'set' ) { this . hydratedDataCache . set ( operationId , hydratedModel ) }
1094+ if ( isEmpty ( this . eagerLoad ) && options ?. operation !== 'set' && this . hydrationKey !== undefined ) {
1095+ this . hydratedDataCache . set ( operationId , hydratedModel )
1096+ }
10881097
10891098 return hydratedModel
10901099 }
0 commit comments