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

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.
@@ -207,12 +207,16 @@ var ModelQueryEvent;
207
207
  })(ModelQueryEvent ||= {});
208
208
  var ModelMutationAction;
209
209
  ((ModelMutationAction2) => {
210
+ ModelMutationAction2["Create"] = "create";
210
211
  ModelMutationAction2["BeforeCreate"] = "beforeCreate";
211
212
  ModelMutationAction2["AfterCreate"] = "afterCreate";
213
+ ModelMutationAction2["Update"] = "update";
212
214
  ModelMutationAction2["BeforeUpdate"] = "beforeUpdate";
213
215
  ModelMutationAction2["AfterUpdate"] = "afterUpdate";
216
+ ModelMutationAction2["Remove"] = "remove";
214
217
  ModelMutationAction2["BeforeRemove"] = "beforeRemove";
215
218
  ModelMutationAction2["AfterRemove"] = "afterRemove";
219
+ ModelMutationAction2["Restore"] = "restore";
216
220
  ModelMutationAction2["BeforeRestore"] = "beforeRestore";
217
221
  ModelMutationAction2["AfterRestore"] = "afterRestore";
218
222
  })(ModelMutationAction ||= {});
@@ -12676,7 +12680,7 @@ class ModelService extends ReadOnlyModelService {
12676
12680
  constructor(args) {
12677
12681
  super(args);
12678
12682
  }
12679
- async normalizeInput(input) {
12683
+ async normalizeInput(input, args) {
12680
12684
  return input;
12681
12685
  }
12682
12686
  async remove(lookup, options) {
@@ -12696,7 +12700,9 @@ class ModelService extends ReadOnlyModelService {
12696
12700
  return await this.normalizeSummary(result);
12697
12701
  }
12698
12702
  async create(input, options) {
12699
- const normalizedInput = await this.normalizeInput(input);
12703
+ const normalizedInput = await this.normalizeInput(input, {
12704
+ descriptor: this.getDescriptor("create" /* Create */)
12705
+ });
12700
12706
  const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
12701
12707
  await this.emitter.emitAsync(beforeCreateEvent);
12702
12708
  const result = await this.repository.create(normalizedInput, options);
@@ -12705,7 +12711,11 @@ class ModelService extends ReadOnlyModelService {
12705
12711
  return await this.normalizeDetail(result);
12706
12712
  }
12707
12713
  async update(lookup, input, options) {
12708
- const normalizedInput = await this.normalizeInput(input);
12714
+ const existing = await this.repository.load(lookup, options);
12715
+ const normalizedInput = await this.normalizeInput(input, {
12716
+ existing,
12717
+ descriptor: this.getDescriptor("update" /* Update */)
12718
+ });
12709
12719
  const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
12710
12720
  await this.emitter.emitAsync(beforeUpdateEvent);
12711
12721
  const result = await this.repository.update(lookup, normalizedInput, options);
@@ -12714,25 +12724,33 @@ class ModelService extends ReadOnlyModelService {
12714
12724
  return await this.normalizeDetail(result);
12715
12725
  }
12716
12726
  async upsert(input, options) {
12717
- const normalizedInput = await this.normalizeInput(input);
12718
- const primaryKeyValue = this.getPrimaryKeyValue(normalizedInput);
12727
+ const primaryKeyValue = this.getPrimaryKeyValue(input);
12728
+ let operation;
12719
12729
  let beforeOperation;
12720
12730
  let afterOperation;
12731
+ let existingItem = undefined;
12721
12732
  if (primaryKeyValue === undefined) {
12733
+ operation = "create" /* Create */;
12722
12734
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12723
12735
  afterOperation = "afterCreate" /* AfterCreate */;
12724
12736
  } else {
12725
- const existingItem = await this.load({
12737
+ existingItem = await this.load({
12726
12738
  [this.entityMetadata.primaryKey]: primaryKeyValue
12727
12739
  }, options);
12728
12740
  if (existingItem) {
12741
+ operation = "update" /* Update */;
12729
12742
  beforeOperation = "beforeUpdate" /* BeforeUpdate */;
12730
12743
  afterOperation = "afterUpdate" /* AfterUpdate */;
12731
12744
  } else {
12745
+ operation = "create" /* Create */;
12732
12746
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12733
12747
  afterOperation = "afterCreate" /* AfterCreate */;
12734
12748
  }
12735
12749
  }
12750
+ const normalizedInput = await this.normalizeInput(input, {
12751
+ descriptor: this.getDescriptor(operation),
12752
+ existing: existingItem
12753
+ });
12736
12754
  const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
12737
12755
  await this.emitter.emitAsync(beforeUpsertEvent);
12738
12756
  const result = await this.repository.upsert(normalizedInput, options);
@@ -12744,68 +12762,61 @@ class ModelService extends ReadOnlyModelService {
12744
12762
  if (inputs.length === 0) {
12745
12763
  return [];
12746
12764
  }
12747
- const normalizedInputs = await Promise.all(inputs.map((input) => this.normalizeInput(input)));
12748
- const entityInfoMap = new Map;
12749
- const inputsWithoutPrimaryKey = [];
12750
- for (const input of normalizedInputs) {
12765
+ const inputInfos = [];
12766
+ const uniqueLookups = new Map;
12767
+ for (let i = 0;i < inputs.length; i++) {
12768
+ const input = inputs[i];
12751
12769
  const primaryKeyValue = this.getPrimaryKeyValue(input);
12770
+ const inputInfo = {
12771
+ input,
12772
+ index: i,
12773
+ primaryKeyValue
12774
+ };
12775
+ inputInfos.push(inputInfo);
12752
12776
  if (primaryKeyValue !== undefined) {
12753
- const entityInfo = {
12754
- input,
12755
- primaryKeyValue,
12756
- lookup: {
12757
- [this.entityMetadata.primaryKey]: primaryKeyValue
12758
- }
12759
- };
12760
- entityInfoMap.set(primaryKeyValue, entityInfo);
12761
- } else {
12762
- inputsWithoutPrimaryKey.push(input);
12777
+ uniqueLookups.set(primaryKeyValue, {
12778
+ [this.entityMetadata.primaryKey]: primaryKeyValue
12779
+ });
12763
12780
  }
12764
12781
  }
12765
- const lookups = Array.from(entityInfoMap.values()).map((info) => info.lookup);
12766
- if (lookups.length > 0) {
12782
+ const existingEntitiesMap = new Map;
12783
+ if (uniqueLookups.size > 0) {
12784
+ const lookups = Array.from(uniqueLookups.values());
12767
12785
  const existingEntities = await this.loadMany(lookups, options);
12768
12786
  existingEntities.forEach((entity) => {
12769
12787
  if (entity) {
12770
12788
  const pkValue = this.getPrimaryKeyValue(entity);
12771
- if (pkValue !== undefined && entityInfoMap.has(pkValue)) {
12772
- const entityInfo = entityInfoMap.get(pkValue);
12773
- entityInfo.existingEntity = entity;
12789
+ if (pkValue !== undefined) {
12790
+ existingEntitiesMap.set(pkValue, entity);
12774
12791
  }
12775
12792
  }
12776
12793
  });
12777
12794
  }
12795
+ const normalizationPromises = inputInfos.map(async (inputInfo) => {
12796
+ if (inputInfo.primaryKeyValue !== undefined) {
12797
+ inputInfo.existingEntity = existingEntitiesMap.get(inputInfo.primaryKeyValue);
12798
+ }
12799
+ inputInfo.operation = inputInfo.existingEntity ? "beforeUpdate" /* BeforeUpdate */ : "beforeCreate" /* BeforeCreate */;
12800
+ const normalizedInput = await this.normalizeInput(inputInfo.input, {
12801
+ existing: inputInfo.existingEntity,
12802
+ descriptor: this.getDescriptor(inputInfo.existingEntity ? "update" /* Update */ : "create" /* Create */)
12803
+ });
12804
+ inputInfo.input = normalizedInput;
12805
+ return normalizedInput;
12806
+ });
12807
+ const normalizedInputs = await Promise.all(normalizationPromises);
12778
12808
  const beforeEvents = [];
12779
- for (const entityInfo of entityInfoMap.values()) {
12780
- const operation = entityInfo.existingEntity ? "beforeUpdate" /* BeforeUpdate */ : "beforeCreate" /* BeforeCreate */;
12781
- entityInfo.operation = operation;
12782
- beforeEvents.push(new MutationEvent(this.getDescriptor(operation), entityInfo.input));
12783
- }
12784
- for (const input of inputsWithoutPrimaryKey) {
12785
- beforeEvents.push(new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), input));
12809
+ for (const inputInfo of inputInfos) {
12810
+ beforeEvents.push(new MutationEvent(this.getDescriptor(inputInfo.operation), inputInfo.input));
12786
12811
  }
12787
12812
  await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
12788
12813
  const results = await this.repository.bulkUpsert(normalizedInputs, options);
12789
- const resultsByPrimaryKey = new Map;
12790
- const resultsWithoutPrimaryKey = [];
12791
- for (const result of results) {
12792
- const pkValue = this.getPrimaryKeyValue(result);
12793
- if (pkValue !== undefined) {
12794
- resultsByPrimaryKey.set(pkValue, result);
12795
- } else {
12796
- resultsWithoutPrimaryKey.push(result);
12797
- }
12798
- }
12799
12814
  const afterEvents = [];
12800
- let resultsWithoutPkIndex = 0;
12801
- for (const entityInfo of entityInfoMap.values()) {
12802
- const matchedResult = resultsByPrimaryKey.get(entityInfo.primaryKeyValue);
12803
- const afterOperation = entityInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12804
- afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), entityInfo.input).setResult(matchedResult));
12805
- }
12806
- for (const input of inputsWithoutPrimaryKey) {
12807
- const matchedResult = resultsWithoutPrimaryKey[resultsWithoutPkIndex++];
12808
- afterEvents.push(new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), input).setResult(matchedResult));
12815
+ for (let i = 0;i < inputInfos.length; i++) {
12816
+ const inputInfo = inputInfos[i];
12817
+ const result = results[i];
12818
+ const afterOperation = inputInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12819
+ afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), inputInfo.input).setResult(result));
12809
12820
  }
12810
12821
  await Promise.all(afterEvents.map((event) => this.emitter.emitAsync(event)));
12811
12822
  return await Promise.all(results.map((result) => this.normalizeDetail(result)));
@@ -13081,5 +13092,5 @@ export {
13081
13092
  BaseModelService
13082
13093
  };
13083
13094
 
13084
- //# debugId=C40E3C9988109C9B64756E2164756E21
13095
+ //# debugId=90127405F5173D2664756E2164756E21
13085
13096
  //# sourceMappingURL=index.js.map