@@ -6,7 +6,10 @@ import EntityConfiguration from './EntityConfiguration';
66import EntityDatabaseAdapter from './EntityDatabaseAdapter' ;
77import { EntityEdgeDeletionBehavior } from './EntityFields' ;
88import EntityLoaderFactory from './EntityLoaderFactory' ;
9- import { EntityMutationTrigger , EntityMutationTriggerConfiguration } from './EntityMutationTrigger' ;
9+ import EntityMutationTriggerConfiguration , {
10+ EntityMutationTrigger ,
11+ } from './EntityMutationTriggerConfiguration' ;
12+ import EntityMutationValidator from './EntityMutationValidator' ;
1013import EntityPrivacyPolicy from './EntityPrivacyPolicy' ;
1114import { EntityQueryContext , EntityTransactionalQueryContext } from './EntityQueryContext' ;
1215import ReadonlyEntity from './ReadonlyEntity' ;
@@ -42,7 +45,7 @@ abstract class BaseMutator<
4245 TSelectedFields
4346 > ,
4447 protected readonly privacyPolicy : TPrivacyPolicy ,
45- protected readonly mutationValidators : EntityMutationTrigger <
48+ protected readonly mutationValidators : EntityMutationValidator <
4649 TFields ,
4750 TID ,
4851 TViewerContext ,
@@ -68,18 +71,21 @@ abstract class BaseMutator<
6871 protected readonly metricsAdapter : IEntityMetricsAdapter
6972 ) { }
7073
71- protected async executeTriggers (
72- triggers :
74+ protected async executeMutationTriggersOrValidatorsAsync (
75+ triggersOrValidators :
7376 | EntityMutationTrigger < TFields , TID , TViewerContext , TEntity , TSelectedFields > [ ]
77+ | EntityMutationValidator < TFields , TID , TViewerContext , TEntity , TSelectedFields > [ ]
7478 | undefined ,
7579 queryContext : EntityQueryContext ,
7680 entity : TEntity
7781 ) : Promise < void > {
78- if ( ! triggers ) {
82+ if ( ! triggersOrValidators ) {
7983 return ;
8084 }
8185 await Promise . all (
82- triggers . map ( ( trigger ) => trigger . executeAsync ( this . viewerContext , queryContext , entity ) )
86+ triggersOrValidators . map ( ( triggerOrValidator ) =>
87+ triggerOrValidator . executeAsync ( this . viewerContext , queryContext , entity )
88+ )
8389 ) ;
8490 }
8591}
@@ -137,7 +143,7 @@ export class CreateMutator<
137143 ( innerQueryContext ) => this . createInternalAsync ( innerQueryContext )
138144 ) ;
139145 if ( internalResult . ok ) {
140- await this . executeTriggers (
146+ await this . executeMutationTriggersOrValidatorsAsync (
141147 this . mutationTriggers . afterCommit ,
142148 this . queryContext ,
143149 internalResult . value
@@ -165,17 +171,17 @@ export class CreateMutator<
165171 return authorizeCreateResult ;
166172 }
167173
168- await this . executeTriggers (
174+ await this . executeMutationTriggersOrValidatorsAsync (
169175 this . mutationValidators ,
170176 queryContext ,
171177 temporaryEntityForPrivacyCheck
172178 ) ;
173- await this . executeTriggers (
179+ await this . executeMutationTriggersOrValidatorsAsync (
174180 this . mutationTriggers . beforeAll ,
175181 queryContext ,
176182 temporaryEntityForPrivacyCheck
177183 ) ;
178- await this . executeTriggers (
184+ await this . executeMutationTriggersOrValidatorsAsync (
179185 this . mutationTriggers . beforeCreate ,
180186 queryContext ,
181187 temporaryEntityForPrivacyCheck
@@ -191,8 +197,16 @@ export class CreateMutator<
191197 . enforcing ( )
192198 . loadByIDAsync ( unauthorizedEntityAfterInsert . getID ( ) ) ;
193199
194- await this . executeTriggers ( this . mutationTriggers . afterCreate , queryContext , newEntity ) ;
195- await this . executeTriggers ( this . mutationTriggers . afterAll , queryContext , newEntity ) ;
200+ await this . executeMutationTriggersOrValidatorsAsync (
201+ this . mutationTriggers . afterCreate ,
202+ queryContext ,
203+ newEntity
204+ ) ;
205+ await this . executeMutationTriggersOrValidatorsAsync (
206+ this . mutationTriggers . afterAll ,
207+ queryContext ,
208+ newEntity
209+ ) ;
196210
197211 return result ( newEntity ) ;
198212 }
@@ -232,7 +246,7 @@ export class UpdateMutator<
232246 TSelectedFields
233247 > ,
234248 privacyPolicy : TPrivacyPolicy ,
235- mutationValidators : EntityMutationTrigger <
249+ mutationValidators : EntityMutationValidator <
236250 TFields ,
237251 TID ,
238252 TViewerContext ,
@@ -310,7 +324,7 @@ export class UpdateMutator<
310324 ( innerQueryContext ) => this . updateInternalAsync ( innerQueryContext )
311325 ) ;
312326 if ( internalResult . ok ) {
313- await this . executeTriggers (
327+ await this . executeMutationTriggersOrValidatorsAsync (
314328 this . mutationTriggers . afterCommit ,
315329 this . queryContext ,
316330 internalResult . value
@@ -334,13 +348,17 @@ export class UpdateMutator<
334348 return authorizeUpdateResult ;
335349 }
336350
337- await this . executeTriggers ( this . mutationValidators , queryContext , entityAboutToBeUpdated ) ;
338- await this . executeTriggers (
351+ await this . executeMutationTriggersOrValidatorsAsync (
352+ this . mutationValidators ,
353+ queryContext ,
354+ entityAboutToBeUpdated
355+ ) ;
356+ await this . executeMutationTriggersOrValidatorsAsync (
339357 this . mutationTriggers . beforeAll ,
340358 queryContext ,
341359 entityAboutToBeUpdated
342360 ) ;
343- await this . executeTriggers (
361+ await this . executeMutationTriggersOrValidatorsAsync (
344362 this . mutationTriggers . beforeUpdate ,
345363 queryContext ,
346364 entityAboutToBeUpdated
@@ -363,8 +381,16 @@ export class UpdateMutator<
363381 . enforcing ( )
364382 . loadByIDAsync ( unauthorizedEntityAfterUpdate . getID ( ) ) ;
365383
366- await this . executeTriggers ( this . mutationTriggers . afterUpdate , queryContext , updatedEntity ) ;
367- await this . executeTriggers ( this . mutationTriggers . afterAll , queryContext , updatedEntity ) ;
384+ await this . executeMutationTriggersOrValidatorsAsync (
385+ this . mutationTriggers . afterUpdate ,
386+ queryContext ,
387+ updatedEntity
388+ ) ;
389+ await this . executeMutationTriggersOrValidatorsAsync (
390+ this . mutationTriggers . afterAll ,
391+ queryContext ,
392+ updatedEntity
393+ ) ;
368394
369395 return result ( updatedEntity ) ;
370396 }
@@ -400,7 +426,7 @@ export class DeleteMutator<
400426 TSelectedFields
401427 > ,
402428 privacyPolicy : TPrivacyPolicy ,
403- mutationValidators : EntityMutationTrigger <
429+ mutationValidators : EntityMutationValidator <
404430 TFields ,
405431 TID ,
406432 TViewerContext ,
@@ -471,7 +497,7 @@ export class DeleteMutator<
471497 )
472498 ) ;
473499 if ( internalResult . ok ) {
474- await this . executeTriggers (
500+ await this . executeMutationTriggersOrValidatorsAsync (
475501 this . mutationTriggers . afterCommit ,
476502 this . queryContext ,
477503 internalResult . value
@@ -498,8 +524,16 @@ export class DeleteMutator<
498524 processedEntityIdentifiersFromTransitiveDeletions
499525 ) ;
500526
501- await this . executeTriggers ( this . mutationTriggers . beforeAll , queryContext , this . entity ) ;
502- await this . executeTriggers ( this . mutationTriggers . beforeDelete , queryContext , this . entity ) ;
527+ await this . executeMutationTriggersOrValidatorsAsync (
528+ this . mutationTriggers . beforeAll ,
529+ queryContext ,
530+ this . entity
531+ ) ;
532+ await this . executeMutationTriggersOrValidatorsAsync (
533+ this . mutationTriggers . beforeDelete ,
534+ queryContext ,
535+ this . entity
536+ ) ;
503537
504538 if ( ! skipDatabaseDeletion ) {
505539 await this . databaseAdapter . deleteAsync (
@@ -512,8 +546,16 @@ export class DeleteMutator<
512546 const entityLoader = this . entityLoaderFactory . forLoad ( this . viewerContext , queryContext ) ;
513547 await entityLoader . invalidateFieldsAsync ( this . entity . getAllDatabaseFields ( ) ) ;
514548
515- await this . executeTriggers ( this . mutationTriggers . afterDelete , queryContext , this . entity ) ;
516- await this . executeTriggers ( this . mutationTriggers . afterAll , queryContext , this . entity ) ;
549+ await this . executeMutationTriggersOrValidatorsAsync (
550+ this . mutationTriggers . afterDelete ,
551+ queryContext ,
552+ this . entity
553+ ) ;
554+ await this . executeMutationTriggersOrValidatorsAsync (
555+ this . mutationTriggers . afterAll ,
556+ queryContext ,
557+ this . entity
558+ ) ;
517559
518560 return result ( this . entity ) ;
519561 }
0 commit comments