@declaro/data 2.0.0-beta.116 → 2.0.0-beta.117

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.
@@ -246,12 +246,16 @@ var ModelQueryEvent;
246
246
  })(ModelQueryEvent ||= {});
247
247
  var ModelMutationAction;
248
248
  ((ModelMutationAction2) => {
249
+ ModelMutationAction2["Create"] = "create";
249
250
  ModelMutationAction2["BeforeCreate"] = "beforeCreate";
250
251
  ModelMutationAction2["AfterCreate"] = "afterCreate";
252
+ ModelMutationAction2["Update"] = "update";
251
253
  ModelMutationAction2["BeforeUpdate"] = "beforeUpdate";
252
254
  ModelMutationAction2["AfterUpdate"] = "afterUpdate";
255
+ ModelMutationAction2["Remove"] = "remove";
253
256
  ModelMutationAction2["BeforeRemove"] = "beforeRemove";
254
257
  ModelMutationAction2["AfterRemove"] = "afterRemove";
258
+ ModelMutationAction2["Restore"] = "restore";
255
259
  ModelMutationAction2["BeforeRestore"] = "beforeRestore";
256
260
  ModelMutationAction2["AfterRestore"] = "afterRestore";
257
261
  })(ModelMutationAction ||= {});
@@ -12715,7 +12719,7 @@ class ModelService extends ReadOnlyModelService {
12715
12719
  constructor(args) {
12716
12720
  super(args);
12717
12721
  }
12718
- async normalizeInput(input) {
12722
+ async normalizeInput(input, args) {
12719
12723
  return input;
12720
12724
  }
12721
12725
  async remove(lookup, options) {
@@ -12735,7 +12739,9 @@ class ModelService extends ReadOnlyModelService {
12735
12739
  return await this.normalizeSummary(result);
12736
12740
  }
12737
12741
  async create(input, options) {
12738
- const normalizedInput = await this.normalizeInput(input);
12742
+ const normalizedInput = await this.normalizeInput(input, {
12743
+ descriptor: this.getDescriptor("create" /* Create */)
12744
+ });
12739
12745
  const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
12740
12746
  await this.emitter.emitAsync(beforeCreateEvent);
12741
12747
  const result = await this.repository.create(normalizedInput, options);
@@ -12744,7 +12750,11 @@ class ModelService extends ReadOnlyModelService {
12744
12750
  return await this.normalizeDetail(result);
12745
12751
  }
12746
12752
  async update(lookup, input, options) {
12747
- const normalizedInput = await this.normalizeInput(input);
12753
+ const existing = await this.repository.load(lookup, options);
12754
+ const normalizedInput = await this.normalizeInput(input, {
12755
+ existing,
12756
+ descriptor: this.getDescriptor("update" /* Update */)
12757
+ });
12748
12758
  const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
12749
12759
  await this.emitter.emitAsync(beforeUpdateEvent);
12750
12760
  const result = await this.repository.update(lookup, normalizedInput, options);
@@ -12753,25 +12763,33 @@ class ModelService extends ReadOnlyModelService {
12753
12763
  return await this.normalizeDetail(result);
12754
12764
  }
12755
12765
  async upsert(input, options) {
12756
- const normalizedInput = await this.normalizeInput(input);
12757
- const primaryKeyValue = this.getPrimaryKeyValue(normalizedInput);
12766
+ const primaryKeyValue = this.getPrimaryKeyValue(input);
12767
+ let operation;
12758
12768
  let beforeOperation;
12759
12769
  let afterOperation;
12770
+ let existingItem = undefined;
12760
12771
  if (primaryKeyValue === undefined) {
12772
+ operation = "create" /* Create */;
12761
12773
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12762
12774
  afterOperation = "afterCreate" /* AfterCreate */;
12763
12775
  } else {
12764
- const existingItem = await this.load({
12776
+ existingItem = await this.load({
12765
12777
  [this.entityMetadata.primaryKey]: primaryKeyValue
12766
12778
  }, options);
12767
12779
  if (existingItem) {
12780
+ operation = "update" /* Update */;
12768
12781
  beforeOperation = "beforeUpdate" /* BeforeUpdate */;
12769
12782
  afterOperation = "afterUpdate" /* AfterUpdate */;
12770
12783
  } else {
12784
+ operation = "create" /* Create */;
12771
12785
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12772
12786
  afterOperation = "afterCreate" /* AfterCreate */;
12773
12787
  }
12774
12788
  }
12789
+ const normalizedInput = await this.normalizeInput(input, {
12790
+ descriptor: this.getDescriptor(operation),
12791
+ existing: existingItem
12792
+ });
12775
12793
  const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
12776
12794
  await this.emitter.emitAsync(beforeUpsertEvent);
12777
12795
  const result = await this.repository.upsert(normalizedInput, options);
@@ -12783,68 +12801,61 @@ class ModelService extends ReadOnlyModelService {
12783
12801
  if (inputs.length === 0) {
12784
12802
  return [];
12785
12803
  }
12786
- const normalizedInputs = await Promise.all(inputs.map((input) => this.normalizeInput(input)));
12787
- const entityInfoMap = new Map;
12788
- const inputsWithoutPrimaryKey = [];
12789
- for (const input of normalizedInputs) {
12804
+ const inputInfos = [];
12805
+ const uniqueLookups = new Map;
12806
+ for (let i = 0;i < inputs.length; i++) {
12807
+ const input = inputs[i];
12790
12808
  const primaryKeyValue = this.getPrimaryKeyValue(input);
12809
+ const inputInfo = {
12810
+ input,
12811
+ index: i,
12812
+ primaryKeyValue
12813
+ };
12814
+ inputInfos.push(inputInfo);
12791
12815
  if (primaryKeyValue !== undefined) {
12792
- const entityInfo = {
12793
- input,
12794
- primaryKeyValue,
12795
- lookup: {
12796
- [this.entityMetadata.primaryKey]: primaryKeyValue
12797
- }
12798
- };
12799
- entityInfoMap.set(primaryKeyValue, entityInfo);
12800
- } else {
12801
- inputsWithoutPrimaryKey.push(input);
12816
+ uniqueLookups.set(primaryKeyValue, {
12817
+ [this.entityMetadata.primaryKey]: primaryKeyValue
12818
+ });
12802
12819
  }
12803
12820
  }
12804
- const lookups = Array.from(entityInfoMap.values()).map((info) => info.lookup);
12805
- if (lookups.length > 0) {
12821
+ const existingEntitiesMap = new Map;
12822
+ if (uniqueLookups.size > 0) {
12823
+ const lookups = Array.from(uniqueLookups.values());
12806
12824
  const existingEntities = await this.loadMany(lookups, options);
12807
12825
  existingEntities.forEach((entity) => {
12808
12826
  if (entity) {
12809
12827
  const pkValue = this.getPrimaryKeyValue(entity);
12810
- if (pkValue !== undefined && entityInfoMap.has(pkValue)) {
12811
- const entityInfo = entityInfoMap.get(pkValue);
12812
- entityInfo.existingEntity = entity;
12828
+ if (pkValue !== undefined) {
12829
+ existingEntitiesMap.set(pkValue, entity);
12813
12830
  }
12814
12831
  }
12815
12832
  });
12816
12833
  }
12834
+ const normalizationPromises = inputInfos.map(async (inputInfo) => {
12835
+ if (inputInfo.primaryKeyValue !== undefined) {
12836
+ inputInfo.existingEntity = existingEntitiesMap.get(inputInfo.primaryKeyValue);
12837
+ }
12838
+ inputInfo.operation = inputInfo.existingEntity ? "beforeUpdate" /* BeforeUpdate */ : "beforeCreate" /* BeforeCreate */;
12839
+ const normalizedInput = await this.normalizeInput(inputInfo.input, {
12840
+ existing: inputInfo.existingEntity,
12841
+ descriptor: this.getDescriptor(inputInfo.existingEntity ? "update" /* Update */ : "create" /* Create */)
12842
+ });
12843
+ inputInfo.input = normalizedInput;
12844
+ return normalizedInput;
12845
+ });
12846
+ const normalizedInputs = await Promise.all(normalizationPromises);
12817
12847
  const beforeEvents = [];
12818
- for (const entityInfo of entityInfoMap.values()) {
12819
- const operation = entityInfo.existingEntity ? "beforeUpdate" /* BeforeUpdate */ : "beforeCreate" /* BeforeCreate */;
12820
- entityInfo.operation = operation;
12821
- beforeEvents.push(new MutationEvent(this.getDescriptor(operation), entityInfo.input));
12822
- }
12823
- for (const input of inputsWithoutPrimaryKey) {
12824
- beforeEvents.push(new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), input));
12848
+ for (const inputInfo of inputInfos) {
12849
+ beforeEvents.push(new MutationEvent(this.getDescriptor(inputInfo.operation), inputInfo.input));
12825
12850
  }
12826
12851
  await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
12827
12852
  const results = await this.repository.bulkUpsert(normalizedInputs, options);
12828
- const resultsByPrimaryKey = new Map;
12829
- const resultsWithoutPrimaryKey = [];
12830
- for (const result of results) {
12831
- const pkValue = this.getPrimaryKeyValue(result);
12832
- if (pkValue !== undefined) {
12833
- resultsByPrimaryKey.set(pkValue, result);
12834
- } else {
12835
- resultsWithoutPrimaryKey.push(result);
12836
- }
12837
- }
12838
12853
  const afterEvents = [];
12839
- let resultsWithoutPkIndex = 0;
12840
- for (const entityInfo of entityInfoMap.values()) {
12841
- const matchedResult = resultsByPrimaryKey.get(entityInfo.primaryKeyValue);
12842
- const afterOperation = entityInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12843
- afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), entityInfo.input).setResult(matchedResult));
12844
- }
12845
- for (const input of inputsWithoutPrimaryKey) {
12846
- const matchedResult = resultsWithoutPrimaryKey[resultsWithoutPkIndex++];
12847
- afterEvents.push(new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), input).setResult(matchedResult));
12854
+ for (let i = 0;i < inputInfos.length; i++) {
12855
+ const inputInfo = inputInfos[i];
12856
+ const result = results[i];
12857
+ const afterOperation = inputInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12858
+ afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), inputInfo.input).setResult(result));
12848
12859
  }
12849
12860
  await Promise.all(afterEvents.map((event) => this.emitter.emitAsync(event)));
12850
12861
  return await Promise.all(results.map((result) => this.normalizeDetail(result)));
@@ -13102,5 +13113,5 @@ class MockMemoryRepository {
13102
13113
  }
13103
13114
  }
13104
13115
 
13105
- //# debugId=C034DAEABB73EDD864756E2164756E21
13116
+ //# debugId=CDABACEBE68C85EA64756E2164756E21
13106
13117
  //# sourceMappingURL=index.cjs.map