@declaro/data 2.0.0-beta.97 → 2.0.0-beta.99

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.
Files changed (37) hide show
  1. package/dist/browser/index.js +8 -8
  2. package/dist/browser/index.js.map +4 -4
  3. package/dist/node/index.cjs +78 -29
  4. package/dist/node/index.cjs.map +4 -4
  5. package/dist/node/index.js +78 -29
  6. package/dist/node/index.js.map +4 -4
  7. package/dist/ts/domain/services/model-service.d.ts +8 -0
  8. package/dist/ts/domain/services/model-service.d.ts.map +1 -1
  9. package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts +2 -0
  10. package/dist/ts/test/mock/repositories/mock-memory-repository.assign.test.d.ts.map +1 -0
  11. package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts +2 -0
  12. package/dist/ts/test/mock/repositories/mock-memory-repository.basic.test.d.ts.map +1 -0
  13. package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts +2 -0
  14. package/dist/ts/test/mock/repositories/mock-memory-repository.bulk-upsert.test.d.ts.map +1 -0
  15. package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts +2 -0
  16. package/dist/ts/test/mock/repositories/mock-memory-repository.count.test.d.ts.map +1 -0
  17. package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts +1 -0
  18. package/dist/ts/test/mock/repositories/mock-memory-repository.custom-lookup.test.d.ts.map +1 -0
  19. package/dist/ts/test/mock/repositories/mock-memory-repository.d.ts.map +1 -1
  20. package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts +2 -0
  21. package/dist/ts/test/mock/repositories/mock-memory-repository.search.test.d.ts.map +1 -0
  22. package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts +2 -0
  23. package/dist/ts/test/mock/repositories/mock-memory-repository.upsert.test.d.ts.map +1 -0
  24. package/package.json +5 -5
  25. package/src/domain/services/model-service.test.ts +245 -0
  26. package/src/domain/services/model-service.ts +37 -14
  27. package/src/test/mock/repositories/mock-memory-repository.assign.test.ts +215 -0
  28. package/src/test/mock/repositories/mock-memory-repository.basic.test.ts +129 -0
  29. package/src/test/mock/repositories/mock-memory-repository.bulk-upsert.test.ts +159 -0
  30. package/src/test/mock/repositories/mock-memory-repository.count.test.ts +98 -0
  31. package/src/test/mock/repositories/mock-memory-repository.custom-lookup.test.ts +0 -0
  32. package/src/test/mock/repositories/mock-memory-repository.search.test.ts +265 -0
  33. package/src/test/mock/repositories/mock-memory-repository.ts +67 -16
  34. package/src/test/mock/repositories/mock-memory-repository.upsert.test.ts +108 -0
  35. package/dist/ts/test/mock/repositories/mock-memory-repository.test.d.ts +0 -2
  36. package/dist/ts/test/mock/repositories/mock-memory-repository.test.d.ts.map +0 -1
  37. package/src/test/mock/repositories/mock-memory-repository.test.ts +0 -919
@@ -11172,6 +11172,9 @@ class ModelService extends ReadOnlyModelService {
11172
11172
  constructor(args) {
11173
11173
  super(args);
11174
11174
  }
11175
+ async normalizeInput(input) {
11176
+ return input;
11177
+ }
11175
11178
  async remove(lookup, options) {
11176
11179
  const beforeRemoveEvent = new MutationEvent(this.getDescriptor("beforeRemove" /* BeforeRemove */), lookup);
11177
11180
  await this.emitter.emitAsync(beforeRemoveEvent);
@@ -11189,23 +11192,26 @@ class ModelService extends ReadOnlyModelService {
11189
11192
  return result;
11190
11193
  }
11191
11194
  async create(input, options) {
11192
- const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), input);
11195
+ const normalizedInput = await this.normalizeInput(input);
11196
+ const beforeCreateEvent = new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), normalizedInput);
11193
11197
  await this.emitter.emitAsync(beforeCreateEvent);
11194
- const result = await this.repository.create(input, options);
11195
- const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), input).setResult(result);
11198
+ const result = await this.repository.create(normalizedInput, options);
11199
+ const afterCreateEvent = new MutationEvent(this.getDescriptor("afterCreate" /* AfterCreate */), normalizedInput).setResult(result);
11196
11200
  await this.emitter.emitAsync(afterCreateEvent);
11197
11201
  return result;
11198
11202
  }
11199
11203
  async update(lookup, input, options) {
11200
- const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), input);
11204
+ const normalizedInput = await this.normalizeInput(input);
11205
+ const beforeUpdateEvent = new MutationEvent(this.getDescriptor("beforeUpdate" /* BeforeUpdate */), normalizedInput);
11201
11206
  await this.emitter.emitAsync(beforeUpdateEvent);
11202
- const result = await this.repository.update(lookup, input, options);
11203
- const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */), input).setResult(result);
11207
+ const result = await this.repository.update(lookup, normalizedInput, options);
11208
+ const afterUpdateEvent = new MutationEvent(this.getDescriptor("afterUpdate" /* AfterUpdate */), normalizedInput).setResult(result);
11204
11209
  await this.emitter.emitAsync(afterUpdateEvent);
11205
11210
  return result;
11206
11211
  }
11207
11212
  async upsert(input, options) {
11208
- const primaryKeyValue = this.getPrimaryKeyValue(input);
11213
+ const normalizedInput = await this.normalizeInput(input);
11214
+ const primaryKeyValue = this.getPrimaryKeyValue(normalizedInput);
11209
11215
  let beforeOperation;
11210
11216
  let afterOperation;
11211
11217
  if (primaryKeyValue === undefined) {
@@ -11223,10 +11229,10 @@ class ModelService extends ReadOnlyModelService {
11223
11229
  afterOperation = "afterCreate" /* AfterCreate */;
11224
11230
  }
11225
11231
  }
11226
- const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), input);
11232
+ const beforeUpsertEvent = new MutationEvent(this.getDescriptor(beforeOperation), normalizedInput);
11227
11233
  await this.emitter.emitAsync(beforeUpsertEvent);
11228
- const result = await this.repository.upsert(input, options);
11229
- const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation), input).setResult(result);
11234
+ const result = await this.repository.upsert(normalizedInput, options);
11235
+ const afterUpsertEvent = new MutationEvent(this.getDescriptor(afterOperation), normalizedInput).setResult(result);
11230
11236
  await this.emitter.emitAsync(afterUpsertEvent);
11231
11237
  return result;
11232
11238
  }
@@ -11234,9 +11240,10 @@ class ModelService extends ReadOnlyModelService {
11234
11240
  if (inputs.length === 0) {
11235
11241
  return [];
11236
11242
  }
11243
+ const normalizedInputs = await Promise.all(inputs.map((input) => this.normalizeInput(input)));
11237
11244
  const entityInfoMap = new Map;
11238
11245
  const inputsWithoutPrimaryKey = [];
11239
- for (const input of inputs) {
11246
+ for (const input of normalizedInputs) {
11240
11247
  const primaryKeyValue = this.getPrimaryKeyValue(input);
11241
11248
  if (primaryKeyValue !== undefined) {
11242
11249
  const entityInfo = {
@@ -11274,7 +11281,7 @@ class ModelService extends ReadOnlyModelService {
11274
11281
  beforeEvents.push(new MutationEvent(this.getDescriptor("beforeCreate" /* BeforeCreate */), input));
11275
11282
  }
11276
11283
  await Promise.all(beforeEvents.map((event) => this.emitter.emitAsync(event)));
11277
- const results = await this.repository.bulkUpsert(inputs, options);
11284
+ const results = await this.repository.bulkUpsert(normalizedInputs, options);
11278
11285
  const resultsByPrimaryKey = new Map;
11279
11286
  const resultsWithoutPrimaryKey = [];
11280
11287
  for (const result of results) {
@@ -11364,8 +11371,19 @@ class MockMemoryRepository {
11364
11371
  if (!this.entityMetadata?.primaryKey) {
11365
11372
  throw new Error("Primary key is not defined in the schema metadata");
11366
11373
  }
11367
- const items = await Promise.all(inputs.map((input) => this.data.get(input[this.entityMetadata.primaryKey])));
11368
- return items;
11374
+ const results = [];
11375
+ for (const input of inputs) {
11376
+ let item;
11377
+ if (typeof this.args.lookup === "function") {
11378
+ item = Array.from(this.data.values()).find((data) => this.args.lookup(data, input));
11379
+ } else {
11380
+ item = this.data.get(input[this.entityMetadata.primaryKey]);
11381
+ }
11382
+ if (item) {
11383
+ results.push(item);
11384
+ }
11385
+ }
11386
+ return results;
11369
11387
  }
11370
11388
  async search(input, options) {
11371
11389
  const pagination = options?.pagination || { page: 1, pageSize: 25 };
@@ -11407,24 +11425,44 @@ class MockMemoryRepository {
11407
11425
  if (!this.entityMetadata?.primaryKey) {
11408
11426
  throw new Error("Primary key is not defined in the schema metadata");
11409
11427
  }
11410
- const item = await this.data.get(lookup[this.entityMetadata.primaryKey]);
11428
+ let item;
11429
+ let itemKey;
11430
+ if (typeof this.args.lookup === "function") {
11431
+ item = Array.from(this.data.values()).find((data) => this.args.lookup(data, lookup));
11432
+ if (item) {
11433
+ itemKey = item[this.entityMetadata.primaryKey];
11434
+ }
11435
+ } else {
11436
+ itemKey = lookup[this.entityMetadata.primaryKey];
11437
+ item = this.data.get(itemKey);
11438
+ }
11411
11439
  if (!item) {
11412
11440
  throw new Error("Item not found");
11413
11441
  }
11414
- this.trash.set(lookup[this.entityMetadata.primaryKey], item);
11415
- this.data.delete(lookup[this.entityMetadata.primaryKey]);
11442
+ this.trash.set(itemKey, item);
11443
+ this.data.delete(itemKey);
11416
11444
  return item;
11417
11445
  }
11418
11446
  async restore(lookup) {
11419
11447
  if (!this.entityMetadata?.primaryKey) {
11420
11448
  throw new Error("Primary key is not defined in the schema metadata");
11421
11449
  }
11422
- const item = await this.trash.get(lookup[this.entityMetadata.primaryKey]);
11450
+ let item;
11451
+ let itemKey;
11452
+ if (typeof this.args.lookup === "function") {
11453
+ item = Array.from(this.trash.values()).find((data) => this.args.lookup(data, lookup));
11454
+ if (item) {
11455
+ itemKey = item[this.entityMetadata.primaryKey];
11456
+ }
11457
+ } else {
11458
+ itemKey = lookup[this.entityMetadata.primaryKey];
11459
+ item = this.trash.get(itemKey);
11460
+ }
11423
11461
  if (!item) {
11424
11462
  throw new Error("Item not found in trash");
11425
11463
  }
11426
- this.trash.delete(lookup[this.entityMetadata.primaryKey]);
11427
- this.data.set(lookup[this.entityMetadata.primaryKey], item);
11464
+ this.trash.delete(itemKey);
11465
+ this.data.set(itemKey, item);
11428
11466
  return item;
11429
11467
  }
11430
11468
  async create(input) {
@@ -11447,16 +11485,27 @@ class MockMemoryRepository {
11447
11485
  if (!this.entityMetadata?.primaryKey) {
11448
11486
  throw new Error("Primary key is not defined in the schema metadata");
11449
11487
  }
11450
- const primaryKeyValue = lookup[this.entityMetadata.primaryKey];
11451
- if (!primaryKeyValue) {
11452
- throw new Error("Primary key value must be provided");
11453
- }
11454
- const existingItem = this.data.get(primaryKeyValue);
11455
- if (!existingItem) {
11456
- throw new Error("Item not found");
11488
+ let existingItem;
11489
+ let itemKey;
11490
+ if (typeof this.args.lookup === "function") {
11491
+ existingItem = Array.from(this.data.values()).find((data) => this.args.lookup(data, lookup));
11492
+ if (existingItem) {
11493
+ itemKey = existingItem[this.entityMetadata.primaryKey];
11494
+ } else {
11495
+ throw new Error("Item not found");
11496
+ }
11497
+ } else {
11498
+ itemKey = lookup[this.entityMetadata.primaryKey];
11499
+ if (!itemKey) {
11500
+ throw new Error("Primary key value must be provided");
11501
+ }
11502
+ existingItem = this.data.get(itemKey);
11503
+ if (!existingItem) {
11504
+ throw new Error("Item not found");
11505
+ }
11457
11506
  }
11458
11507
  const updatedItem = this.assignInput(existingItem, input);
11459
- this.data.set(primaryKeyValue, updatedItem);
11508
+ this.data.set(itemKey, updatedItem);
11460
11509
  return updatedItem;
11461
11510
  }
11462
11511
  async count(search, options) {
@@ -11528,5 +11577,5 @@ export {
11528
11577
  BaseModelService
11529
11578
  };
11530
11579
 
11531
- //# debugId=79EE3AB3B0DB0FAB64756E2164756E21
11580
+ //# debugId=F1F1804B450A753464756E2164756E21
11532
11581
  //# sourceMappingURL=index.js.map