@@ -98,7 +98,7 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
9898 // reject non-existing model
9999 this . tryRejectNonexistentModel ( mutationModel ) ;
100100
101- // --- Pre mutation work ---
101+ // #region Pre mutation work
102102
103103 if ( InsertQueryNode . is ( node ) ) {
104104 // pre-create policy evaluation happens before execution of the query
@@ -128,11 +128,15 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
128128 beforeUpdateInfo = await this . loadBeforeUpdateEntities ( mutationModel , node . where , proceed ) ;
129129 }
130130
131- // proceed with query
131+ // #endregion
132+
133+ // #region query execution
132134
133135 const result = await proceed ( this . transformNode ( node ) ) ;
134136
135- // --- Post mutation work ---
137+ // #endregion
138+
139+ // #region Post mutation work
136140
137141 if ( hasPostUpdatePolicies && result . rows . length > 0 ) {
138142 // verify if before-update rows and post-update rows still id-match
@@ -210,9 +214,11 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
210214 'some or all updated rows failed to pass post-update policy check' ,
211215 ) ;
212216 }
217+
218+ // #endregion
213219 }
214220
215- // --- Read back ---
221+ // #region Read back
216222
217223 if ( ! node . returning || this . onlyReturningId ( node ) ) {
218224 // no need to check read back
@@ -228,6 +234,8 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
228234 }
229235 return readBackResult ;
230236 }
237+
238+ // #endregion
231239 }
232240
233241 // #endregion
@@ -292,8 +300,8 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
292300 // when there's no field-level policies, we merge model-level policy filters into where clause directly
293301 // for generating simpler SQL
294302
295- let whereNode = this . transformNode ( node . where ) ;
296- const policyFilter = this . createPolicyFilterForFrom ( node . from ) ;
303+ let whereNode = result . where ;
304+ const policyFilter = this . createPolicyFilterForFrom ( result . from ) ;
297305 if ( policyFilter && ! isTrueNode ( policyFilter ) ) {
298306 whereNode = WhereNode . create (
299307 whereNode ?. where ? conjunction ( this . dialect , [ whereNode . where , policyFilter ] ) : policyFilter ,
@@ -538,7 +546,10 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
538546 selections,
539547 } ;
540548
541- return { hasPolicies, query : AliasNode . create ( nestedQuery , IdentifierNode . create ( alias ?? model ) ) } ;
549+ return {
550+ hasPolicies,
551+ query : AliasNode . create ( ParensNode . create ( nestedQuery ) , IdentifierNode . create ( alias ?? model ) ) ,
552+ } ;
542553 }
543554
544555 private createFieldSelectionWithPolicy ( model : string , field : string , operation : FieldLevelPolicyOperations ) {
@@ -559,7 +570,7 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
559570 return { hasPolicies : true , selection : SelectionNode . create ( selection ) } ;
560571 }
561572
562- private hasFieldLevelPolicies ( model : string ) : unknown {
573+ private hasFieldLevelPolicies ( model : string ) {
563574 const modelDef = QueryUtils . getModel ( this . client . $schema , model ) ;
564575 if ( ! modelDef ) {
565576 return false ;
0 commit comments