@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.
@@ -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 ||= {});
@@ -12661,31 +12665,16 @@ class BaseModelService {
12661
12665
  }
12662
12666
  return input[this.entityMetadata.primaryKey];
12663
12667
  }
12668
+ getSchema() {
12669
+ return this.schema;
12670
+ }
12664
12671
  }
12665
12672
  // src/domain/services/read-only-model-service.ts
12666
12673
  class ReadOnlyModelService extends BaseModelService {
12667
12674
  async normalizeDetail(detail) {
12668
- const detailModel = this.schema.definition.detail;
12669
- if (detailModel) {
12670
- const validation = await detailModel.validate(detail, { strict: false });
12671
- if (validation.issues) {
12672
- console.warn(`${detailModel.labels.singularLabel} shape did not match the expected schema`, validation);
12673
- } else {
12674
- return validation.value;
12675
- }
12676
- }
12677
12675
  return detail;
12678
12676
  }
12679
12677
  async normalizeSummary(summary) {
12680
- const summaryModel = this.schema.definition.summary;
12681
- if (summaryModel) {
12682
- const validation = await summaryModel.validate(summary, { strict: false });
12683
- if (validation.issues) {
12684
- console.warn(`${summaryModel.labels.singularLabel} shape did not match the expected schema`);
12685
- } else {
12686
- return validation.value;
12687
- }
12688
- }
12689
12678
  return summary;
12690
12679
  }
12691
12680
  async load(lookup, options) {
@@ -12730,7 +12719,7 @@ class ModelService extends ReadOnlyModelService {
12730
12719
  constructor(args) {
12731
12720
  super(args);
12732
12721
  }
12733
- async normalizeInput(input) {
12722
+ async normalizeInput(input, args) {
12734
12723
  return input;
12735
12724
  }
12736
12725
  async remove(lookup, options) {
@@ -12750,7 +12739,9 @@ class ModelService extends ReadOnlyModelService {
12750
12739
  return await this.normalizeSummary(result);
12751
12740
  }
12752
12741
  async create(input, options) {
12753
- const normalizedInput = await this.normalizeInput(input);
12742
+ const normalizedInput = await this.normalizeInput(input, {
12743
+ descriptor: this.getDescriptor("create" /* Create */)
12744
+ });
12754
12745
  const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
12755
12746
  await this.emitter.emitAsync(beforeCreateEvent);
12756
12747
  const result = await this.repository.create(normalizedInput, options);
@@ -12759,7 +12750,11 @@ class ModelService extends ReadOnlyModelService {
12759
12750
  return await this.normalizeDetail(result);
12760
12751
  }
12761
12752
  async update(lookup, input, options) {
12762
- 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
+ });
12763
12758
  const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
12764
12759
  await this.emitter.emitAsync(beforeUpdateEvent);
12765
12760
  const result = await this.repository.update(lookup, normalizedInput, options);
@@ -12768,25 +12763,33 @@ class ModelService extends ReadOnlyModelService {
12768
12763
  return await this.normalizeDetail(result);
12769
12764
  }
12770
12765
  async upsert(input, options) {
12771
- const normalizedInput = await this.normalizeInput(input);
12772
- const primaryKeyValue = this.getPrimaryKeyValue(normalizedInput);
12766
+ const primaryKeyValue = this.getPrimaryKeyValue(input);
12767
+ let operation;
12773
12768
  let beforeOperation;
12774
12769
  let afterOperation;
12770
+ let existingItem = undefined;
12775
12771
  if (primaryKeyValue === undefined) {
12772
+ operation = "create" /* Create */;
12776
12773
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12777
12774
  afterOperation = "afterCreate" /* AfterCreate */;
12778
12775
  } else {
12779
- const existingItem = await this.load({
12776
+ existingItem = await this.load({
12780
12777
  [this.entityMetadata.primaryKey]: primaryKeyValue
12781
12778
  }, options);
12782
12779
  if (existingItem) {
12780
+ operation = "update" /* Update */;
12783
12781
  beforeOperation = "beforeUpdate" /* BeforeUpdate */;
12784
12782
  afterOperation = "afterUpdate" /* AfterUpdate */;
12785
12783
  } else {
12784
+ operation = "create" /* Create */;
12786
12785
  beforeOperation = "beforeCreate" /* BeforeCreate */;
12787
12786
  afterOperation = "afterCreate" /* AfterCreate */;
12788
12787
  }
12789
12788
  }
12789
+ const normalizedInput = await this.normalizeInput(input, {
12790
+ descriptor: this.getDescriptor(operation),
12791
+ existing: existingItem
12792
+ });
12790
12793
  const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
12791
12794
  await this.emitter.emitAsync(beforeUpsertEvent);
12792
12795
  const result = await this.repository.upsert(normalizedInput, options);
@@ -12798,68 +12801,61 @@ class ModelService extends ReadOnlyModelService {
12798
12801
  if (inputs.length === 0) {
12799
12802
  return [];
12800
12803
  }
12801
- const normalizedInputs = await Promise.all(inputs.map((input) => this.normalizeInput(input)));
12802
- const entityInfoMap = new Map;
12803
- const inputsWithoutPrimaryKey = [];
12804
- 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];
12805
12808
  const primaryKeyValue = this.getPrimaryKeyValue(input);
12809
+ const inputInfo = {
12810
+ input,
12811
+ index: i,
12812
+ primaryKeyValue
12813
+ };
12814
+ inputInfos.push(inputInfo);
12806
12815
  if (primaryKeyValue !== undefined) {
12807
- const entityInfo = {
12808
- input,
12809
- primaryKeyValue,
12810
- lookup: {
12811
- [this.entityMetadata.primaryKey]: primaryKeyValue
12812
- }
12813
- };
12814
- entityInfoMap.set(primaryKeyValue, entityInfo);
12815
- } else {
12816
- inputsWithoutPrimaryKey.push(input);
12816
+ uniqueLookups.set(primaryKeyValue, {
12817
+ [this.entityMetadata.primaryKey]: primaryKeyValue
12818
+ });
12817
12819
  }
12818
12820
  }
12819
- const lookups = Array.from(entityInfoMap.values()).map((info) => info.lookup);
12820
- if (lookups.length > 0) {
12821
+ const existingEntitiesMap = new Map;
12822
+ if (uniqueLookups.size > 0) {
12823
+ const lookups = Array.from(uniqueLookups.values());
12821
12824
  const existingEntities = await this.loadMany(lookups, options);
12822
12825
  existingEntities.forEach((entity) => {
12823
12826
  if (entity) {
12824
12827
  const pkValue = this.getPrimaryKeyValue(entity);
12825
- if (pkValue !== undefined && entityInfoMap.has(pkValue)) {
12826
- const entityInfo = entityInfoMap.get(pkValue);
12827
- entityInfo.existingEntity = entity;
12828
+ if (pkValue !== undefined) {
12829
+ existingEntitiesMap.set(pkValue, entity);
12828
12830
  }
12829
12831
  }
12830
12832
  });
12831
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);
12832
12847
  const beforeEvents = [];
12833
- for (const entityInfo of entityInfoMap.values()) {
12834
- const operation = entityInfo.existingEntity ? "beforeUpdate" /* BeforeUpdate */ : "beforeCreate" /* BeforeCreate */;
12835
- entityInfo.operation = operation;
12836
- beforeEvents.push(new MutationEvent(this.getDescriptor(operation), entityInfo.input));
12837
- }
12838
- for (const input of inputsWithoutPrimaryKey) {
12839
- 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));
12840
12850
  }
12841
12851
  await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
12842
12852
  const results = await this.repository.bulkUpsert(normalizedInputs, options);
12843
- const resultsByPrimaryKey = new Map;
12844
- const resultsWithoutPrimaryKey = [];
12845
- for (const result of results) {
12846
- const pkValue = this.getPrimaryKeyValue(result);
12847
- if (pkValue !== undefined) {
12848
- resultsByPrimaryKey.set(pkValue, result);
12849
- } else {
12850
- resultsWithoutPrimaryKey.push(result);
12851
- }
12852
- }
12853
12853
  const afterEvents = [];
12854
- let resultsWithoutPkIndex = 0;
12855
- for (const entityInfo of entityInfoMap.values()) {
12856
- const matchedResult = resultsByPrimaryKey.get(entityInfo.primaryKeyValue);
12857
- const afterOperation = entityInfo.operation === "beforeCreate" /* BeforeCreate */ ? "afterCreate" /* AfterCreate */ : "afterUpdate" /* AfterUpdate */;
12858
- afterEvents.push(new MutationEvent(this.getDescriptor(afterOperation), entityInfo.input).setResult(matchedResult));
12859
- }
12860
- for (const input of inputsWithoutPrimaryKey) {
12861
- const matchedResult = resultsWithoutPrimaryKey[resultsWithoutPkIndex++];
12862
- 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));
12863
12859
  }
12864
12860
  await Promise.all(afterEvents.map((event) => this.emitter.emitAsync(event)));
12865
12861
  return await Promise.all(results.map((result) => this.normalizeDetail(result)));
@@ -13117,5 +13113,5 @@ class MockMemoryRepository {
13117
13113
  }
13118
13114
  }
13119
13115
 
13120
- //# debugId=02E485F6A311A3CD64756E2164756E21
13116
+ //# debugId=CDABACEBE68C85EA64756E2164756E21
13121
13117
  //# sourceMappingURL=index.cjs.map