@declaro/data 2.0.0-beta.96 → 2.0.0-beta.98

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.
@@ -11211,6 +11211,9 @@ class ModelService extends ReadOnlyModelService {
11211
11211
  constructor(args) {
11212
11212
  super(args);
11213
11213
  }
11214
+ async normalizeInput(input) {
11215
+ return input;
11216
+ }
11214
11217
  async remove(lookup, options) {
11215
11218
  const beforeRemoveEvent = new MutationEvent(this.getDescriptor("beforeRemove" /* BeforeRemove */), lookup);
11216
11219
  await this.emitter.emitAsync(beforeRemoveEvent);
@@ -11228,23 +11231,26 @@ class ModelService extends ReadOnlyModelService {
11228
11231
  return result;
11229
11232
  }
11230
11233
  async create(input, options) {
11231
- const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), input);
11234
+ const normalizedInput = await this.normalizeInput(input);
11235
+ const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
11232
11236
  await this.emitter.emitAsync(beforeCreateEvent);
11233
- const result = await this.repository.create(input, options);
11234
- const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), input).setResult(result);
11237
+ const result = await this.repository.create(normalizedInput, options);
11238
+ const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), normalizedInput).setResult(result);
11235
11239
  await this.emitter.emitAsync(afterCreateEvent);
11236
11240
  return result;
11237
11241
  }
11238
11242
  async update(lookup, input, options) {
11239
- const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), input);
11243
+ const normalizedInput = await this.normalizeInput(input);
11244
+ const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
11240
11245
  await this.emitter.emitAsync(beforeUpdateEvent);
11241
- const result = await this.repository.update(lookup, input, options);
11242
- const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */), input).setResult(result);
11246
+ const result = await this.repository.update(lookup, normalizedInput, options);
11247
+ const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */), normalizedInput).setResult(result);
11243
11248
  await this.emitter.emitAsync(afterUpdateEvent);
11244
11249
  return result;
11245
11250
  }
11246
11251
  async upsert(input, options) {
11247
- const primaryKeyValue = this.getPrimaryKeyValue(input);
11252
+ const normalizedInput = await this.normalizeInput(input);
11253
+ const primaryKeyValue = this.getPrimaryKeyValue(normalizedInput);
11248
11254
  let beforeOperation;
11249
11255
  let afterOperation;
11250
11256
  if (primaryKeyValue === undefined) {
@@ -11262,10 +11268,10 @@ class ModelService extends ReadOnlyModelService {
11262
11268
  afterOperation = "afterCreate" /* AfterCreate */;
11263
11269
  }
11264
11270
  }
11265
- const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), input);
11271
+ const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
11266
11272
  await this.emitter.emitAsync(beforeUpsertEvent);
11267
- const result = await this.repository.upsert(input, options);
11268
- const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation), input).setResult(result);
11273
+ const result = await this.repository.upsert(normalizedInput, options);
11274
+ const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation), normalizedInput).setResult(result);
11269
11275
  await this.emitter.emitAsync(afterUpsertEvent);
11270
11276
  return result;
11271
11277
  }
@@ -11273,9 +11279,10 @@ class ModelService extends ReadOnlyModelService {
11273
11279
  if (inputs.length === 0) {
11274
11280
  return [];
11275
11281
  }
11282
+ const normalizedInputs = await Promise.all(inputs.map((input) => this.normalizeInput(input)));
11276
11283
  const entityInfoMap = new Map;
11277
11284
  const inputsWithoutPrimaryKey = [];
11278
- for (const input of inputs) {
11285
+ for (const input of normalizedInputs) {
11279
11286
  const primaryKeyValue = this.getPrimaryKeyValue(input);
11280
11287
  if (primaryKeyValue !== undefined) {
11281
11288
  const entityInfo = {
@@ -11313,7 +11320,7 @@ class ModelService extends ReadOnlyModelService {
11313
11320
  beforeEvents.push(new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), input));
11314
11321
  }
11315
11322
  await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
11316
- const results = await this.repository.bulkUpsert(inputs, options);
11323
+ const results = await this.repository.bulkUpsert(normalizedInputs, options);
11317
11324
  const resultsByPrimaryKey = new Map;
11318
11325
  const resultsWithoutPrimaryKey = [];
11319
11326
  for (const result of results) {
@@ -11474,9 +11481,8 @@ class MockMemoryRepository {
11474
11481
  if (primaryKeyValue && this.data.has(primaryKeyValue)) {
11475
11482
  throw new Error("Item with the same primary key already exists");
11476
11483
  }
11477
- const payload = {
11478
- ...input
11479
- };
11484
+ const baseData = {};
11485
+ const payload = this.assignInput(baseData, input);
11480
11486
  if (!payload[this.entityMetadata.primaryKey]) {
11481
11487
  payload[this.entityMetadata.primaryKey] = await this.generatePrimaryKey();
11482
11488
  }
@@ -11495,7 +11501,7 @@ class MockMemoryRepository {
11495
11501
  if (!existingItem) {
11496
11502
  throw new Error("Item not found");
11497
11503
  }
11498
- const updatedItem = Object.assign({}, existingItem, input);
11504
+ const updatedItem = this.assignInput(existingItem, input);
11499
11505
  this.data.set(primaryKeyValue, updatedItem);
11500
11506
  return updatedItem;
11501
11507
  }
@@ -11505,11 +11511,11 @@ class MockMemoryRepository {
11505
11511
  }
11506
11512
  async upsert(input, options) {
11507
11513
  const primaryKeyValue = input[this.entityMetadata.primaryKey];
11508
- let existingItem = undefined;
11514
+ let existingItem = {};
11509
11515
  if (primaryKeyValue) {
11510
11516
  existingItem = this.data.get(primaryKeyValue) ?? {};
11511
11517
  }
11512
- const updatedItem = Object.assign({}, existingItem, input);
11518
+ const updatedItem = this.assignInput(existingItem, input);
11513
11519
  if (!updatedItem[this.entityMetadata.primaryKey]) {
11514
11520
  updatedItem[this.entityMetadata.primaryKey] = await this.generatePrimaryKey();
11515
11521
  }
@@ -11528,6 +11534,13 @@ class MockMemoryRepository {
11528
11534
  }
11529
11535
  });
11530
11536
  }
11537
+ assignInput(existingData, input) {
11538
+ if (typeof this.args.assign === "function") {
11539
+ return this.args.assign(existingData, input);
11540
+ } else {
11541
+ return Object.assign({}, existingData, input);
11542
+ }
11543
+ }
11531
11544
  async generatePrimaryKey() {
11532
11545
  const lookupModel = this.args.schema.definition.lookup;
11533
11546
  const lookupMeta = await lookupModel.toJSONSchema();
@@ -11543,5 +11556,5 @@ class MockMemoryRepository {
11543
11556
  }
11544
11557
  }
11545
11558
 
11546
- //# debugId=A7E2932888C3A17164756E2164756E21
11559
+ //# debugId=2ABA230E346B7A7A64756E2164756E21
11547
11560
  //# sourceMappingURL=index.cjs.map