@declaro/data 2.0.0-beta.126 → 2.0.0-beta.128

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/browser/index.js +11 -11
  2. package/dist/browser/index.js.map +6 -6
  3. package/dist/node/index.cjs +217 -92
  4. package/dist/node/index.cjs.map +6 -6
  5. package/dist/node/index.js +209 -84
  6. package/dist/node/index.js.map +6 -6
  7. package/dist/ts/application/model-controller.d.ts +15 -5
  8. package/dist/ts/application/model-controller.d.ts.map +1 -1
  9. package/dist/ts/application/read-only-model-controller.d.ts +5 -1
  10. package/dist/ts/application/read-only-model-controller.d.ts.map +1 -1
  11. package/dist/ts/domain/services/model-service.d.ts +27 -0
  12. package/dist/ts/domain/services/model-service.d.ts.map +1 -1
  13. package/dist/ts/domain/services/read-only-model-service.d.ts +8 -0
  14. package/dist/ts/domain/services/read-only-model-service.d.ts.map +1 -1
  15. package/dist/ts/shared/utils/schema-inheritance.test.d.ts +2 -0
  16. package/dist/ts/shared/utils/schema-inheritance.test.d.ts.map +1 -0
  17. package/dist/ts/shared/utils/test/animal-schema.d.ts +57 -0
  18. package/dist/ts/shared/utils/test/animal-schema.d.ts.map +1 -0
  19. package/dist/ts/shared/utils/test/animal-trait-schema.d.ts +55 -0
  20. package/dist/ts/shared/utils/test/animal-trait-schema.d.ts.map +1 -0
  21. package/dist/ts/shared/utils/test/elephant-schema.d.ts +30 -0
  22. package/dist/ts/shared/utils/test/elephant-schema.d.ts.map +1 -0
  23. package/dist/ts/shared/utils/test/elephant-trait-schema.d.ts +26 -0
  24. package/dist/ts/shared/utils/test/elephant-trait-schema.d.ts.map +1 -0
  25. package/package.json +5 -5
  26. package/src/application/model-controller.ts +110 -59
  27. package/src/application/read-only-model-controller.ts +43 -25
  28. package/src/domain/services/model-service.test.ts +460 -0
  29. package/src/domain/services/model-service.ts +165 -67
  30. package/src/domain/services/read-only-model-service.test.ts +230 -0
  31. package/src/domain/services/read-only-model-service.ts +65 -40
  32. package/src/shared/utils/schema-inheritance.test.ts +295 -0
  33. package/src/shared/utils/test/animal-schema.ts +46 -0
  34. package/src/shared/utils/test/animal-trait-schema.ts +45 -0
  35. package/src/shared/utils/test/elephant-schema.ts +58 -0
  36. package/src/shared/utils/test/elephant-trait-schema.ts +53 -0
  37. package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts +0 -1
  38. package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts.map +0 -1
  39. package/src/test/mock/repositories/mock-memory-repository.custom-lookup.test.ts +0 -0
@@ -10,10 +10,11 @@ var __export = (target, all) => {
10
10
  };
11
11
 
12
12
  // src/application/model-controller.ts
13
- import { PermissionValidator } from "@declaro/core";
13
+ import { PermissionValidator as PermissionValidator2 } from "@declaro/core";
14
14
 
15
15
  // src/application/read-only-model-controller.ts
16
16
  import"@declaro/auth";
17
+ import { PermissionValidator } from "@declaro/core";
17
18
 
18
19
  class ReadOnlyModelController {
19
20
  service;
@@ -22,32 +23,48 @@ class ReadOnlyModelController {
22
23
  this.service = service;
23
24
  this.authValidator = authValidator;
24
25
  }
25
- async load(lookup, options) {
26
- this.authValidator.validatePermissions((v) => v.someOf([
26
+ async loadPermissions(lookup) {
27
+ return PermissionValidator.create().someOf([
27
28
  this.service.getDescriptor("load", "*").toString(),
28
29
  this.service.getDescriptor("read", "*").toString()
29
- ]));
30
+ ]);
31
+ }
32
+ async load(lookup, options) {
33
+ const permissions = await this.loadPermissions(lookup);
34
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
30
35
  return this.service.load(lookup, options);
31
36
  }
32
- async loadMany(lookups, options) {
33
- this.authValidator.validatePermissions((v) => v.someOf([
37
+ async loadManyPermissions(lookups) {
38
+ return PermissionValidator.create().someOf([
34
39
  this.service.getDescriptor("loadMany", "*").toString(),
35
40
  this.service.getDescriptor("read", "*").toString()
36
- ]));
41
+ ]);
42
+ }
43
+ async loadMany(lookups, options) {
44
+ const permissions = await this.loadManyPermissions(lookups);
45
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
37
46
  return this.service.loadMany(lookups, options);
38
47
  }
39
- async search(input, options) {
40
- this.authValidator.validatePermissions((v) => v.someOf([
48
+ async searchPermissions(input, options) {
49
+ return PermissionValidator.create().someOf([
41
50
  this.service.getDescriptor("search", "*").toString(),
42
51
  this.service.getDescriptor("read", "*").toString()
43
- ]));
52
+ ]);
53
+ }
54
+ async search(input, options) {
55
+ const permissions = await this.searchPermissions(input, options);
56
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
44
57
  return this.service.search(input, options);
45
58
  }
46
- async count(input, options) {
47
- this.authValidator.validatePermissions((v) => v.someOf([
59
+ async countPermissions(input, options) {
60
+ return PermissionValidator.create().someOf([
48
61
  this.service.getDescriptor("count", "*").toString(),
49
62
  this.service.getDescriptor("read", "*").toString()
50
- ]));
63
+ ]);
64
+ }
65
+ async count(input, options) {
66
+ const permissions = await this.countPermissions(input, options);
67
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
51
68
  return this.service.count(input, options);
52
69
  }
53
70
  }
@@ -61,64 +78,106 @@ class ModelController extends ReadOnlyModelController {
61
78
  this.service = service;
62
79
  this.authValidator = authValidator;
63
80
  }
64
- async create(input) {
65
- this.authValidator.validatePermissions((v) => v.someOf([
81
+ async createPermissions(input, options) {
82
+ return PermissionValidator2.create().someOf([
66
83
  this.service.getDescriptor("create", "*").toString(),
67
84
  this.service.getDescriptor("write", "*").toString()
68
- ]));
69
- return this.service.create(input);
85
+ ]);
70
86
  }
71
- async update(lookup, input) {
72
- this.authValidator.validatePermissions((v) => v.someOf([
87
+ async create(input, options) {
88
+ const permissions = await this.createPermissions(input, options);
89
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
90
+ return this.service.create(input, options);
91
+ }
92
+ async updatePermissions(lookup, input, options) {
93
+ return PermissionValidator2.create().someOf([
73
94
  this.service.getDescriptor("update", "*").toString(),
74
95
  this.service.getDescriptor("write", "*").toString()
75
- ]));
76
- return this.service.update(lookup, input);
96
+ ]);
77
97
  }
78
- async remove(lookup) {
79
- this.authValidator.validatePermissions((v) => v.someOf([
98
+ async update(lookup, input, options) {
99
+ const permissions = await this.updatePermissions(lookup, input, options);
100
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
101
+ return this.service.update(lookup, input, options);
102
+ }
103
+ async removePermissions(lookup, options) {
104
+ return PermissionValidator2.create().someOf([
80
105
  this.service.getDescriptor("remove", "*").toString(),
81
106
  this.service.getDescriptor("write", "*").toString()
82
- ]));
83
- return this.service.remove(lookup);
107
+ ]);
84
108
  }
85
- async restore(lookup) {
86
- this.authValidator.validatePermissions((v) => v.someOf([
109
+ async remove(lookup, options) {
110
+ const permissions = await this.removePermissions(lookup, options);
111
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
112
+ return this.service.remove(lookup, options);
113
+ }
114
+ async restorePermissions(lookup, options) {
115
+ return PermissionValidator2.create().someOf([
87
116
  this.service.getDescriptor("restore", "*").toString(),
88
117
  this.service.getDescriptor("write", "*").toString()
89
- ]));
90
- return this.service.restore(lookup);
118
+ ]);
91
119
  }
92
- async upsert(input, options) {
93
- const createAndUpdateValidator = PermissionValidator.create().allOf([
120
+ async restore(lookup, options) {
121
+ const permissions = await this.restorePermissions(lookup, options);
122
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
123
+ return this.service.restore(lookup, options);
124
+ }
125
+ async upsertPermissions(input, options) {
126
+ const createAndUpdateValidator = PermissionValidator2.create().allOf([
94
127
  this.service.getDescriptor("create", "*").toString(),
95
128
  this.service.getDescriptor("update", "*").toString()
96
129
  ]);
97
- this.authValidator.validatePermissions((v) => v.someOf([createAndUpdateValidator, this.service.getDescriptor("write", "*").toString()]));
130
+ return PermissionValidator2.create().someOf([
131
+ createAndUpdateValidator,
132
+ this.service.getDescriptor("write", "*").toString()
133
+ ]);
134
+ }
135
+ async upsert(input, options) {
136
+ const permissions = await this.upsertPermissions(input, options);
137
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
98
138
  return this.service.upsert(input, options);
99
139
  }
100
- async bulkUpsert(inputs, options) {
101
- const createAndUpdateValidator = PermissionValidator.create().allOf([
140
+ async bulkUpsertPermissions(inputs, options) {
141
+ const createAndUpdateValidator = PermissionValidator2.create().allOf([
102
142
  this.service.getDescriptor("create", "*").toString(),
103
143
  this.service.getDescriptor("update", "*").toString()
104
144
  ]);
105
- this.authValidator.validatePermissions((v) => v.someOf([createAndUpdateValidator, this.service.getDescriptor("write", "*").toString()]));
145
+ return PermissionValidator2.create().someOf([
146
+ createAndUpdateValidator,
147
+ this.service.getDescriptor("write", "*").toString()
148
+ ]);
149
+ }
150
+ async bulkUpsert(inputs, options) {
151
+ const permissions = await this.bulkUpsertPermissions(inputs, options);
152
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
106
153
  return this.service.bulkUpsert(inputs, options);
107
154
  }
108
- async permanentlyDeleteFromTrash(lookup) {
109
- this.authValidator.validatePermissions((v) => v.someOf([
155
+ async permanentlyDeleteFromTrashPermissions(lookup) {
156
+ return PermissionValidator2.create().someOf([
110
157
  this.service.getDescriptor("permanently-delete-from-trash", "*").toString(),
111
158
  this.service.getDescriptor("permanently-delete", "*").toString(),
112
159
  this.service.getDescriptor("empty-trash", "*").toString()
113
- ]));
160
+ ]);
161
+ }
162
+ async permanentlyDeleteFromTrash(lookup) {
163
+ const permissions = await this.permanentlyDeleteFromTrashPermissions(lookup);
164
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
114
165
  return this.service.permanentlyDeleteFromTrash(lookup);
115
166
  }
167
+ async permanentlyDeletePermissions(lookup) {
168
+ return PermissionValidator2.create().someOf([this.service.getDescriptor("permanently-delete", "*").toString()]);
169
+ }
116
170
  async permanentlyDelete(lookup) {
117
- this.authValidator.validatePermissions((v) => v.someOf([this.service.getDescriptor("permanently-delete", "*").toString()]));
171
+ const permissions = await this.permanentlyDeletePermissions(lookup);
172
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
118
173
  return this.service.permanentlyDelete(lookup);
119
174
  }
175
+ async emptyTrashPermissions(filters) {
176
+ return PermissionValidator2.create().someOf([this.service.getDescriptor("empty-trash", "*").toString()]);
177
+ }
120
178
  async emptyTrash(filters) {
121
- this.authValidator.validatePermissions((v) => v.someOf([this.service.getDescriptor("empty-trash", "*").toString()]));
179
+ const permissions = await this.emptyTrashPermissions(filters);
180
+ this.authValidator.validatePermissions((v) => v.extend(permissions));
122
181
  return this.service.emptyTrash(filters);
123
182
  }
124
183
  }
@@ -12673,38 +12732,54 @@ class ReadOnlyModelService extends BaseModelService {
12673
12732
  return summary;
12674
12733
  }
12675
12734
  async load(lookup, options) {
12676
- const beforeLoadEvent = new QueryEvent(this.getDescriptor("beforeLoad" /* BeforeLoad */, options?.scope), lookup);
12677
- await this.emitter.emitAsync(beforeLoadEvent);
12735
+ if (!options?.doNotDispatchEvents) {
12736
+ const beforeLoadEvent = new QueryEvent(this.getDescriptor("beforeLoad" /* BeforeLoad */, options?.scope), lookup);
12737
+ await this.emitter.emitAsync(beforeLoadEvent);
12738
+ }
12678
12739
  const details = await this.repository.load(lookup, options);
12679
- const afterLoadEvent = new QueryEvent(this.getDescriptor("afterLoad" /* AfterLoad */, options?.scope), lookup).setResult(details);
12680
- await this.emitter.emitAsync(afterLoadEvent);
12740
+ if (!options?.doNotDispatchEvents) {
12741
+ const afterLoadEvent = new QueryEvent(this.getDescriptor("afterLoad" /* AfterLoad */, options?.scope), lookup).setResult(details);
12742
+ await this.emitter.emitAsync(afterLoadEvent);
12743
+ }
12681
12744
  return await this.normalizeDetail(details);
12682
12745
  }
12683
12746
  async loadMany(lookups, options) {
12684
- const beforeLoadManyEvent = new QueryEvent(this.getDescriptor("beforeLoadMany" /* BeforeLoadMany */, options?.scope), lookups);
12685
- await this.emitter.emitAsync(beforeLoadManyEvent);
12747
+ if (!options?.doNotDispatchEvents) {
12748
+ const beforeLoadManyEvent = new QueryEvent(this.getDescriptor("beforeLoadMany" /* BeforeLoadMany */, options?.scope), lookups);
12749
+ await this.emitter.emitAsync(beforeLoadManyEvent);
12750
+ }
12686
12751
  const details = await this.repository.loadMany(lookups, options);
12687
- const afterLoadManyEvent = new QueryEvent(this.getDescriptor("afterLoadMany" /* AfterLoadMany */, options?.scope), lookups).setResult(details);
12688
- await this.emitter.emitAsync(afterLoadManyEvent);
12752
+ if (!options?.doNotDispatchEvents) {
12753
+ const afterLoadManyEvent = new QueryEvent(this.getDescriptor("afterLoadMany" /* AfterLoadMany */, options?.scope), lookups).setResult(details);
12754
+ await this.emitter.emitAsync(afterLoadManyEvent);
12755
+ }
12689
12756
  return await Promise.all(details.map((detail) => this.normalizeDetail(detail)));
12690
12757
  }
12691
12758
  async search(filters, options) {
12692
- const beforeSearchEvent = new QueryEvent(this.getDescriptor("beforeSearch" /* BeforeSearch */, options?.scope), filters);
12693
- await this.emitter.emitAsync(beforeSearchEvent);
12759
+ if (!options?.doNotDispatchEvents) {
12760
+ const beforeSearchEvent = new QueryEvent(this.getDescriptor("beforeSearch" /* BeforeSearch */, options?.scope), filters);
12761
+ await this.emitter.emitAsync(beforeSearchEvent);
12762
+ }
12694
12763
  const results = await this.repository.search(filters, options);
12695
- const afterSearchEvent = new QueryEvent(this.getDescriptor("afterSearch" /* AfterSearch */, options?.scope), filters).setResult(results);
12696
- await this.emitter.emitAsync(afterSearchEvent);
12764
+ if (!options?.doNotDispatchEvents) {
12765
+ const afterSearchEvent = new QueryEvent(this.getDescriptor("afterSearch" /* AfterSearch */, options?.scope), filters).setResult(results);
12766
+ await this.emitter.emitAsync(afterSearchEvent);
12767
+ }
12697
12768
  return {
12698
12769
  ...results,
12699
12770
  results: await Promise.all(results.results.map((detail) => this.normalizeSummary(detail)))
12700
12771
  };
12701
12772
  }
12702
12773
  async count(filters, options) {
12703
- const beforeCountEvent = new QueryEvent(this.getDescriptor("beforeCount" /* BeforeCount */, options?.scope), filters);
12704
- await this.emitter.emitAsync(beforeCountEvent);
12774
+ if (!options?.doNotDispatchEvents) {
12775
+ const beforeCountEvent = new QueryEvent(this.getDescriptor("beforeCount" /* BeforeCount */, options?.scope), filters);
12776
+ await this.emitter.emitAsync(beforeCountEvent);
12777
+ }
12705
12778
  const count = await this.repository.count(filters, options);
12706
- const afterCountEvent = new QueryEvent(this.getDescriptor("afterCount" /* AfterCount */, options?.scope), filters).setResult(count);
12707
- await this.emitter.emitAsync(afterCountEvent);
12779
+ if (!options?.doNotDispatchEvents) {
12780
+ const afterCountEvent = new QueryEvent(this.getDescriptor("afterCount" /* AfterCount */, options?.scope), filters).setResult(count);
12781
+ await this.emitter.emitAsync(afterCountEvent);
12782
+ }
12708
12783
  return count;
12709
12784
  }
12710
12785
  }
@@ -12717,6 +12792,34 @@ class ModelService extends ReadOnlyModelService {
12717
12792
  async normalizeInput(input, args) {
12718
12793
  return input;
12719
12794
  }
12795
+ async detailsToInput(detail) {
12796
+ const inputModel = this.schema.definition.input;
12797
+ const inputJsonSchema = inputModel.toJSONSchema();
12798
+ const inputFields = Object.keys(inputJsonSchema.properties ?? {});
12799
+ const picked = {};
12800
+ for (const field of inputFields) {
12801
+ if (field in detail) {
12802
+ picked[field] = detail[field];
12803
+ }
12804
+ }
12805
+ const result = await inputModel.validate(picked);
12806
+ if ("value" in result) {
12807
+ return result.value;
12808
+ }
12809
+ return picked;
12810
+ }
12811
+ async duplicate(lookup, overrides, options) {
12812
+ const existing = await this.load(lookup);
12813
+ if (!existing) {
12814
+ throw new Error("Item not found");
12815
+ }
12816
+ const input = await this.detailsToInput(existing);
12817
+ if (this.entityMetadata?.primaryKey) {
12818
+ delete input[this.entityMetadata.primaryKey];
12819
+ }
12820
+ const finalInput = overrides ? Object.assign({}, input, overrides) : input;
12821
+ return this.create(finalInput, options);
12822
+ }
12720
12823
  async remove(lookup, options) {
12721
12824
  const beforeRemoveEvent = new MutationEvent(this.getDescriptor("beforeRemove" /* BeforeRemove */), lookup);
12722
12825
  await this.emitter.emitAsync(beforeRemoveEvent);
@@ -12737,24 +12840,32 @@ class ModelService extends ReadOnlyModelService {
12737
12840
  const normalizedInput = await this.normalizeInput(input, {
12738
12841
  descriptor: this.getDescriptor("create" /* Create */)
12739
12842
  });
12740
- const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
12741
- await this.emitter.emitAsync(beforeCreateEvent);
12843
+ if (!options?.doNotDispatchEvents) {
12844
+ const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
12845
+ await this.emitter.emitAsync(beforeCreateEvent);
12846
+ }
12742
12847
  const result = await this.repository.create(normalizedInput, options);
12743
- const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), normalizedInput).setResult(result);
12744
- await this.emitter.emitAsync(afterCreateEvent);
12848
+ if (!options?.doNotDispatchEvents) {
12849
+ const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), normalizedInput).setResult(result);
12850
+ await this.emitter.emitAsync(afterCreateEvent);
12851
+ }
12745
12852
  return await this.normalizeDetail(result);
12746
12853
  }
12747
12854
  async update(lookup, input, options) {
12748
- const existing = await this.repository.load(lookup, options);
12855
+ const existing = await this.repository.load(lookup, { ...options, doNotDispatchEvents: true });
12749
12856
  const normalizedInput = await this.normalizeInput(input, {
12750
12857
  existing,
12751
12858
  descriptor: this.getDescriptor("update" /* Update */)
12752
12859
  });
12753
- const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
12754
- await this.emitter.emitAsync(beforeUpdateEvent);
12860
+ if (!options?.doNotDispatchEvents) {
12861
+ const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
12862
+ await this.emitter.emitAsync(beforeUpdateEvent);
12863
+ }
12755
12864
  const result = await this.repository.update(lookup, normalizedInput, options);
12756
- const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */), normalizedInput).setResult(result);
12757
- await this.emitter.emitAsync(afterUpdateEvent);
12865
+ if (!options?.doNotDispatchEvents) {
12866
+ const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */), normalizedInput).setResult(result);
12867
+ await this.emitter.emitAsync(afterUpdateEvent);
12868
+ }
12758
12869
  return await this.normalizeDetail(result);
12759
12870
  }
12760
12871
  async upsert(input, options) {
@@ -12770,7 +12881,10 @@ class ModelService extends ReadOnlyModelService {
12770
12881
  } else {
12771
12882
  existingItem = await this.load({
12772
12883
  [this.entityMetadata.primaryKey]: primaryKeyValue
12773
- }, options);
12884
+ }, {
12885
+ ...options,
12886
+ doNotDispatchEvents: true
12887
+ });
12774
12888
  if (existingItem) {
12775
12889
  operation = "update" /* Update */;
12776
12890
  beforeOperation = "beforeUpdate" /* BeforeUpdate */;
@@ -12785,11 +12899,15 @@ class ModelService extends ReadOnlyModelService {
12785
12899
  descriptor: this.getDescriptor(operation),
12786
12900
  existing: existingItem
12787
12901
  });
12788
- const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
12789
- await this.emitter.emitAsync(beforeUpsertEvent);
12902
+ if (!options?.doNotDispatchEvents) {
12903
+ const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
12904
+ await this.emitter.emitAsync(beforeUpsertEvent);
12905
+ }
12790
12906
  const result = await this.repository.upsert(normalizedInput, options);
12791
- const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation), normalizedInput).setResult(result);
12792
- await this.emitter.emitAsync(afterUpsertEvent);
12907
+ if (!options?.doNotDispatchEvents) {
12908
+ const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation), normalizedInput).setResult(result);
12909
+ await this.emitter.emitAsync(afterUpsertEvent);
12910
+ }
12793
12911
  return await this.normalizeDetail(result);
12794
12912
  }
12795
12913
  async bulkUpsert(inputs, options) {
@@ -12816,7 +12934,10 @@ class ModelService extends ReadOnlyModelService {
12816
12934
  const existingEntitiesMap = new Map;
12817
12935
  if (uniqueLookups.size > 0) {
12818
12936
  const lookups = Array.from(uniqueLookups.values());
12819
- const existingEntities = await this.loadMany(lookups, options);
12937
+ const existingEntities = await this.loadMany(lookups, {
12938
+ ...options,
12939
+ doNotDispatchEvents: true
12940
+ });
12820
12941
  existingEntities.forEach((entity) => {
12821
12942
  if (entity) {
12822
12943
  const pkValue = this.getPrimaryKeyValue(entity);
@@ -12839,20 +12960,24 @@ class ModelService extends ReadOnlyModelService {
12839
12960
  return normalizedInput;
12840
12961
  });
12841
12962
  const normalizedInputs = await Promise.all(normalizationPromises);
12842
- const beforeEvents = [];
12843
- for (const inputInfo of inputInfos) {
12844
- beforeEvents.push(new MutationEvent(this.getDescriptor(inputInfo.operation), inputInfo.input));
12963
+ if (!options?.doNotDispatchEvents) {
12964
+ const beforeEvents = [];
12965
+ for (const inputInfo of inputInfos) {
12966
+ beforeEvents.push(new MutationEvent(this.getDescriptor(inputInfo.operation), inputInfo.input));
12967
+ }
12968
+ await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
12845
12969
  }
12846
- await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
12847
12970
  const results = await this.repository.bulkUpsert(normalizedInputs, options);
12848
- const afterEvents = [];
12849
- for (let i = 0;i < inputInfos.length; i++) {
12850
- const inputInfo = inputInfos[i];
12851
- const result = results[i];
12852
- const afterOperation = inputInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12853
- afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), inputInfo.input).setResult(result));
12854
- }
12855
- await Promise.all(afterEvents.map((event) => this.emitter.emitAsync(event)));
12971
+ if (!options?.doNotDispatchEvents) {
12972
+ const afterEvents = [];
12973
+ for (let i = 0;i < inputInfos.length; i++) {
12974
+ const inputInfo = inputInfos[i];
12975
+ const result = results[i];
12976
+ const afterOperation = inputInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12977
+ afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), inputInfo.input).setResult(result));
12978
+ }
12979
+ await Promise.all(afterEvents.map((event) => this.emitter.emitAsync(event)));
12980
+ }
12856
12981
  return await Promise.all(results.map((result) => this.normalizeDetail(result)));
12857
12982
  }
12858
12983
  async emptyTrash(filters) {
@@ -13210,5 +13335,5 @@ export {
13210
13335
  BaseModelService
13211
13336
  };
13212
13337
 
13213
- //# debugId=4FCC17E6392BEFF464756E2164756E21
13338
+ //# debugId=B7049DCEB4D8885164756E2164756E21
13214
13339
  //# sourceMappingURL=index.js.map