@punks/backend-entity-manager 0.0.255 → 0.0.257

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.
package/dist/cjs/index.js CHANGED
@@ -1006,6 +1006,36 @@ class EntityUpsertCommand {
1006
1006
  }
1007
1007
  }
1008
1008
 
1009
+ class EntitiesUpsertByCommand {
1010
+ constructor(services) {
1011
+ this.services = services;
1012
+ }
1013
+ async execute(params) {
1014
+ const { items } = await this.services.resolveSearchQuery().execute({
1015
+ filters: params.filter,
1016
+ });
1017
+ if (items.length === 0) {
1018
+ const createdItem = await this.services
1019
+ .resolveCreateCommand()
1020
+ .execute(params.data);
1021
+ // todo: parametrize id field
1022
+ const id = createdItem.id;
1023
+ return {
1024
+ id,
1025
+ };
1026
+ }
1027
+ if (items.length > 1) {
1028
+ throw new Error("Multiple items found");
1029
+ }
1030
+ // todo: parametrize id field
1031
+ const id = items[0].id;
1032
+ await this.services.resolveUpdateCommand().execute(id, params.data);
1033
+ return {
1034
+ id,
1035
+ };
1036
+ }
1037
+ }
1038
+
1009
1039
  class EntityVersionCommand {
1010
1040
  constructor(services) {
1011
1041
  this.services = services;
@@ -1063,6 +1093,9 @@ class EntityManager {
1063
1093
  get upsert() {
1064
1094
  return this.services.resolveUpsertCommand();
1065
1095
  }
1096
+ get upsertBy() {
1097
+ return this.services.resolveUpsertByCommand();
1098
+ }
1066
1099
  get delete() {
1067
1100
  return this.services.resolveDeleteCommand();
1068
1101
  }
@@ -1214,6 +1247,7 @@ const EntityServices = {
1214
1247
  IEntityCreateCommand: "IEntityCreateCommand",
1215
1248
  IEntityUpdateCommand: "IEntityUpdateCommand",
1216
1249
  IEntityUpsertCommand: "IEntityUpsertCommand",
1250
+ IEntityUpsertByCommand: "IEntityUpsertByCommand",
1217
1251
  IEntityMergeCommand: "IEntityMergeCommand",
1218
1252
  IEntityDeleteCommand: "IEntityDeleteCommand",
1219
1253
  IEntityVersionCommand: "IEntityVersionCommand",
@@ -1385,6 +1419,9 @@ class EntityServiceLocator {
1385
1419
  resolveUpsertCommand() {
1386
1420
  return this.services.resolveUpsertCommand(this.entityName);
1387
1421
  }
1422
+ resolveUpsertByCommand() {
1423
+ return this.services.resolveUpsertByCommand(this.entityName);
1424
+ }
1388
1425
  resolveDeleteCommand() {
1389
1426
  return this.services.resolveDeleteCommand(this.entityName);
1390
1427
  }
@@ -1750,6 +1787,12 @@ class EntitiesServiceLocator {
1750
1787
  registerUpsertCommand(entityName, instance) {
1751
1788
  this.provider.registerEntityService(EntityServices.Commands.IEntityUpsertCommand, entityName, instance);
1752
1789
  }
1790
+ registerUpsertByCommand(entityName, instance) {
1791
+ this.provider.registerEntityService(EntityServices.Commands.IEntityUpsertByCommand, entityName, instance);
1792
+ }
1793
+ resolveUpsertByCommand(entityName) {
1794
+ return this.provider.resolveEntityService(EntityServices.Commands.IEntityUpsertByCommand, entityName);
1795
+ }
1753
1796
  resolveDeleteItemsCommand(entityName) {
1754
1797
  return this.provider.resolveEntityService(EntityServices.Commands.IEntitiesDeleteCommand, entityName);
1755
1798
  }
@@ -2434,6 +2477,7 @@ class EntityManagerServiceCollection {
2434
2477
  return this;
2435
2478
  }
2436
2479
  mapUpdate() {
2480
+ this.locator.registerUpsertByCommand(this.entityName, new EntitiesUpsertByCommand(this.resolver));
2437
2481
  this.locator.registerUpsertCommand(this.entityName, new EntityUpsertCommand(this.resolver));
2438
2482
  this.locator.registerUpsertAction(this.entityName, new EntityUpsertAction(this.resolver));
2439
2483
  this.locator.registerUpdateCommand(this.entityName, new EntityUpdateCommand(this.resolver));