@declaro/data 2.0.0-beta.115 → 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.
@@ -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 ||= {});
@@ -12622,31 +12626,16 @@ class BaseModelService {
12622
12626
  }
12623
12627
  return input[this.entityMetadata.primaryKey];
12624
12628
  }
12629
+ getSchema() {
12630
+ return this.schema;
12631
+ }
12625
12632
  }
12626
12633
  // src/domain/services/read-only-model-service.ts
12627
12634
  class ReadOnlyModelService extends BaseModelService {
12628
12635
  async normalizeDetail(detail) {
12629
- const detailModel = this.schema.definition.detail;
12630
- if (detailModel) {
12631
- const validation = await detailModel.validate(detail, { strict: false });
12632
- if (validation.issues) {
12633
- console.warn(`${detailModel.labels.singularLabel} shape did not match the expected schema`, validation);
12634
- } else {
12635
- return validation.value;
12636
- }
12637
- }
12638
12636
  return detail;
12639
12637
  }
12640
12638
  async normalizeSummary(summary) {
12641
- const summaryModel = this.schema.definition.summary;
12642
- if (summaryModel) {
12643
- const validation = await summaryModel.validate(summary, { strict: false });
12644
- if (validation.issues) {
12645
- console.warn(`${summaryModel.labels.singularLabel} shape did not match the expected schema`);
12646
- } else {
12647
- return validation.value;
12648
- }
12649
- }
12650
12639
  return summary;
12651
12640
  }
12652
12641
  async load(lookup, options) {
@@ -12691,7 +12680,7 @@ class ModelService extends ReadOnlyModelService {
12691
12680
  constructor(args) {
12692
12681
  super(args);
12693
12682
  }
12694
- async normalizeInput(input) {
12683
+ async normalizeInput(input, args) {
12695
12684
  return input;
12696
12685
  }
12697
12686
  async remove(lookup, options) {
@@ -12711,7 +12700,9 @@ class ModelService extends ReadOnlyModelService {
12711
12700
  return await this.normalizeSummary(result);
12712
12701
  }
12713
12702
  async create(input, options) {
12714
- const normalizedInput = await this.normalizeInput(input);
12703
+ const normalizedInput = await this.normalizeInput(input, {
12704
+ descriptor: this.getDescriptor("create" /* Create */)
12705
+ });
12715
12706
  const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
12716
12707
  await this.emitter.emitAsync(beforeCreateEvent);
12717
12708
  const result = await this.repository.create(normalizedInput, options);
@@ -12720,7 +12711,11 @@ class ModelService extends ReadOnlyModelService {
12720
12711
  return await this.normalizeDetail(result);
12721
12712
  }
12722
12713
  async update(lookup, input, options) {
12723
- 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
+ });
12724
12719
  const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
12725
12720
  await this.emitter.emitAsync(beforeUpdateEvent);
12726
12721
  const result = await this.repository.update(lookup, normalizedInput, options);
@@ -12729,25 +12724,33 @@ class ModelService extends ReadOnlyModelService {
12729
12724
  return await this.normalizeDetail(result);
12730
12725
  }
12731
12726
  async upsert(input, options) {
12732
- const normalizedInput = await this.normalizeInput(input);
12733
- const primaryKeyValue = this.getPrimaryKeyValue(normalizedInput);
12727
+ const primaryKeyValue = this.getPrimaryKeyValue(input);
12728
+ let operation;
12734
12729
  let beforeOperation;
12735
12730
  let afterOperation;
12731
+ let existingItem = undefined;
12736
12732
  if (primaryKeyValue === undefined) {
12733
+ operation = "create" /* Create */;
12737
12734
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12738
12735
  afterOperation = "afterCreate" /* AfterCreate */;
12739
12736
  } else {
12740
- const existingItem = await this.load({
12737
+ existingItem = await this.load({
12741
12738
  [this.entityMetadata.primaryKey]: primaryKeyValue
12742
12739
  }, options);
12743
12740
  if (existingItem) {
12741
+ operation = "update" /* Update */;
12744
12742
  beforeOperation = "beforeUpdate" /* BeforeUpdate */;
12745
12743
  afterOperation = "afterUpdate" /* AfterUpdate */;
12746
12744
  } else {
12745
+ operation = "create" /* Create */;
12747
12746
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12748
12747
  afterOperation = "afterCreate" /* AfterCreate */;
12749
12748
  }
12750
12749
  }
12750
+ const normalizedInput = await this.normalizeInput(input, {
12751
+ descriptor: this.getDescriptor(operation),
12752
+ existing: existingItem
12753
+ });
12751
12754
  const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
12752
12755
  await this.emitter.emitAsync(beforeUpsertEvent);
12753
12756
  const result = await this.repository.upsert(normalizedInput, options);
@@ -12759,68 +12762,61 @@ class ModelService extends ReadOnlyModelService {
12759
12762
  if (inputs.length === 0) {
12760
12763
  return [];
12761
12764
  }
12762
- const normalizedInputs = await Promise.all(inputs.map((input) => this.normalizeInput(input)));
12763
- const entityInfoMap = new Map;
12764
- const inputsWithoutPrimaryKey = [];
12765
- 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];
12766
12769
  const primaryKeyValue = this.getPrimaryKeyValue(input);
12770
+ const inputInfo = {
12771
+ input,
12772
+ index: i,
12773
+ primaryKeyValue
12774
+ };
12775
+ inputInfos.push(inputInfo);
12767
12776
  if (primaryKeyValue !== undefined) {
12768
- const entityInfo = {
12769
- input,
12770
- primaryKeyValue,
12771
- lookup: {
12772
- [this.entityMetadata.primaryKey]: primaryKeyValue
12773
- }
12774
- };
12775
- entityInfoMap.set(primaryKeyValue, entityInfo);
12776
- } else {
12777
- inputsWithoutPrimaryKey.push(input);
12777
+ uniqueLookups.set(primaryKeyValue, {
12778
+ [this.entityMetadata.primaryKey]: primaryKeyValue
12779
+ });
12778
12780
  }
12779
12781
  }
12780
- const lookups = Array.from(entityInfoMap.values()).map((info) => info.lookup);
12781
- if (lookups.length > 0) {
12782
+ const existingEntitiesMap = new Map;
12783
+ if (uniqueLookups.size > 0) {
12784
+ const lookups = Array.from(uniqueLookups.values());
12782
12785
  const existingEntities = await this.loadMany(lookups, options);
12783
12786
  existingEntities.forEach((entity) => {
12784
12787
  if (entity) {
12785
12788
  const pkValue = this.getPrimaryKeyValue(entity);
12786
- if (pkValue !== undefined && entityInfoMap.has(pkValue)) {
12787
- const entityInfo = entityInfoMap.get(pkValue);
12788
- entityInfo.existingEntity = entity;
12789
+ if (pkValue !== undefined) {
12790
+ existingEntitiesMap.set(pkValue, entity);
12789
12791
  }
12790
12792
  }
12791
12793
  });
12792
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);
12793
12808
  const beforeEvents = [];
12794
- for (const entityInfo of entityInfoMap.values()) {
12795
- const operation = entityInfo.existingEntity ? "beforeUpdate" /* BeforeUpdate */ : "beforeCreate" /* BeforeCreate */;
12796
- entityInfo.operation = operation;
12797
- beforeEvents.push(new MutationEvent(this.getDescriptor(operation), entityInfo.input));
12798
- }
12799
- for (const input of inputsWithoutPrimaryKey) {
12800
- 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));
12801
12811
  }
12802
12812
  await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
12803
12813
  const results = await this.repository.bulkUpsert(normalizedInputs, options);
12804
- const resultsByPrimaryKey = new Map;
12805
- const resultsWithoutPrimaryKey = [];
12806
- for (const result of results) {
12807
- const pkValue = this.getPrimaryKeyValue(result);
12808
- if (pkValue !== undefined) {
12809
- resultsByPrimaryKey.set(pkValue, result);
12810
- } else {
12811
- resultsWithoutPrimaryKey.push(result);
12812
- }
12813
- }
12814
12814
  const afterEvents = [];
12815
- let resultsWithoutPkIndex = 0;
12816
- for (const entityInfo of entityInfoMap.values()) {
12817
- const matchedResult = resultsByPrimaryKey.get(entityInfo.primaryKeyValue);
12818
- const afterOperation = entityInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12819
- afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), entityInfo.input).setResult(matchedResult));
12820
- }
12821
- for (const input of inputsWithoutPrimaryKey) {
12822
- const matchedResult = resultsWithoutPrimaryKey[resultsWithoutPkIndex++];
12823
- 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));
12824
12820
  }
12825
12821
  await Promise.all(afterEvents.map((event) => this.emitter.emitAsync(event)));
12826
12822
  return await Promise.all(results.map((result) => this.normalizeDetail(result)));
@@ -13096,5 +13092,5 @@ export {
13096
13092
  BaseModelService
13097
13093
  };
13098
13094
 
13099
- //# debugId=71BB4C5702006B2764756E2164756E21
13095
+ //# debugId=90127405F5173D2664756E2164756E21
13100
13096
  //# sourceMappingURL=index.js.map