@punks/backend-entity-manager 0.0.139 → 0.0.141

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
@@ -528,16 +528,10 @@ class EntityCreateCommand {
528
528
  if (!versioning) {
529
529
  return;
530
530
  }
531
- const contextService = this.services.resolveAuthenticationContextProvider();
532
- const context = await contextService?.getContext();
533
- const snapshotService = this.services.resolveSnapshotService();
534
- const snapshot = (await snapshotService?.getSnapshot(id)) ?? entity;
535
- await versioning.createVersion({
536
- operationType: exports.EntityVersionOperation.Create,
537
- entityId: id,
538
- entityType: this.services.getEntityName(),
539
- data: snapshot,
540
- modifiedByUserId: context?.userId,
531
+ await this.services.resolveVersionCommand().execute({
532
+ id,
533
+ entity,
534
+ operation: exports.EntityVersionOperation.Create,
541
535
  });
542
536
  }
543
537
  isVersioningEnabled() {
@@ -580,20 +574,9 @@ class EntityDeleteCommand {
580
574
  if (!this.isVersioningEnabled()) {
581
575
  return;
582
576
  }
583
- const versioning = this.services.resolveVersioningProvider();
584
- if (!versioning) {
585
- return;
586
- }
587
- const contextService = this.services.resolveAuthenticationContextProvider();
588
- const context = await contextService?.getContext();
589
- const snapshotService = this.services.resolveSnapshotService();
590
- const snapshot = await snapshotService?.getSnapshot(id);
591
- await versioning.createVersion({
592
- operationType: exports.EntityVersionOperation.Delete,
593
- entityId: id,
594
- entityType: this.services.getEntityName(),
595
- data: snapshot,
596
- modifiedByUserId: context?.userId,
577
+ await this.services.resolveVersionCommand().execute({
578
+ id,
579
+ operation: exports.EntityVersionOperation.Delete,
597
580
  });
598
581
  }
599
582
  isVersioningEnabled() {
@@ -826,20 +809,10 @@ class EntityUpdateCommand {
826
809
  if (!this.isVersioningEnabled()) {
827
810
  return;
828
811
  }
829
- const versioning = this.services.resolveVersioningProvider();
830
- if (!versioning) {
831
- return;
832
- }
833
- const contextService = this.services.resolveAuthenticationContextProvider();
834
- const context = await contextService?.getContext();
835
- const snapshotService = this.services.resolveSnapshotService();
836
- const snapshot = (await snapshotService?.getSnapshot(id)) ?? entity;
837
- await versioning.createVersion({
838
- operationType: exports.EntityVersionOperation.Update,
839
- entityId: id,
840
- entityType: this.services.getEntityName(),
841
- data: snapshot,
842
- modifiedByUserId: context?.userId,
812
+ await this.services.resolveVersionCommand().execute({
813
+ id,
814
+ entity,
815
+ operation: exports.EntityVersionOperation.Update,
843
816
  });
844
817
  }
845
818
  isVersioningEnabled() {
@@ -895,25 +868,38 @@ class EntityUpsertCommand {
895
868
  if (!this.isVersioningEnabled()) {
896
869
  return;
897
870
  }
898
- const versioning = this.services.resolveVersioningProvider();
899
- if (!versioning) {
900
- return;
871
+ await this.services.resolveVersionCommand().execute({
872
+ id,
873
+ entity,
874
+ operation: exports.EntityVersionOperation.Upsert,
875
+ });
876
+ }
877
+ isVersioningEnabled() {
878
+ return (this.services.resolveEntityConfiguration().versioning?.enabled ?? false);
879
+ }
880
+ }
881
+
882
+ class EntityVersionCommand {
883
+ constructor(services) {
884
+ this.services = services;
885
+ }
886
+ async execute({ id, operation, entity, }) {
887
+ const versioningProvider = this.services.resolveVersioningProvider();
888
+ if (!versioningProvider) {
889
+ throw new Error("Versioning provider is not configured");
901
890
  }
902
891
  const contextService = this.services.resolveAuthenticationContextProvider();
903
892
  const context = await contextService?.getContext();
904
893
  const snapshotService = this.services.resolveSnapshotService();
905
894
  const snapshot = (await snapshotService?.getSnapshot(id)) ?? entity;
906
- await versioning.createVersion({
907
- operationType: exports.EntityVersionOperation.Upsert,
895
+ await versioningProvider.createVersion({
896
+ operationType: operation,
908
897
  entityId: id,
909
898
  entityType: this.services.getEntityName(),
910
899
  data: snapshot,
911
900
  modifiedByUserId: context?.userId,
912
901
  });
913
902
  }
914
- isVersioningEnabled() {
915
- return (this.services.resolveEntityConfiguration().versioning?.enabled ?? false);
916
- }
917
903
  }
918
904
 
919
905
  class EntityManager {
@@ -938,6 +924,9 @@ class EntityManager {
938
924
  get find() {
939
925
  return this.services.resolveFindQuery();
940
926
  }
927
+ get version() {
928
+ return this.services.resolveVersionCommand();
929
+ }
941
930
  get create() {
942
931
  return this.services.resolveCreateCommand();
943
932
  }
@@ -1100,6 +1089,7 @@ const EntityServices = {
1100
1089
  IEntityUpsertCommand: "IEntityUpsertCommand",
1101
1090
  IEntityMergeCommand: "IEntityMergeCommand",
1102
1091
  IEntityDeleteCommand: "IEntityDeleteCommand",
1092
+ IEntityVersionCommand: "IEntityVersionCommand",
1103
1093
  IEntitiesDeleteCommand: "IEntitiesDeleteCommand",
1104
1094
  IEntitiesExportCommand: "IEntitiesExportCommand",
1105
1095
  IEntitiesImportCommand: "IEntitiesImportCommand",
@@ -1267,6 +1257,9 @@ class EntityServiceLocator {
1267
1257
  resolveDeleteItemsCommand() {
1268
1258
  return this.services.resolveDeleteItemsCommand(this.entityName);
1269
1259
  }
1260
+ resolveVersionCommand() {
1261
+ return this.services.resolveVersionCommand(this.entityName);
1262
+ }
1270
1263
  resolveSampleDownloadCommand() {
1271
1264
  return this.services.resolveSampleDownloadCommand(this.entityName);
1272
1265
  }
@@ -1580,6 +1573,12 @@ class EntitiesServiceLocator {
1580
1573
  registerDeleteCommand(entityName, instance) {
1581
1574
  this.provider.registerEntityService(EntityServices.Commands.IEntityDeleteCommand, entityName, instance);
1582
1575
  }
1576
+ resolveVersionCommand(entityName) {
1577
+ return this.provider.resolveEntityService(EntityServices.Commands.IEntityVersionCommand, entityName);
1578
+ }
1579
+ registerVersionCommand(entityName, instance) {
1580
+ this.provider.registerEntityService(EntityServices.Commands.IEntityVersionCommand, entityName, instance);
1581
+ }
1583
1582
  resolveExportCommand(entityName) {
1584
1583
  return this.provider.resolveEntityService(EntityServices.Commands.IEntitiesExportCommand, entityName);
1585
1584
  }
@@ -2206,6 +2205,7 @@ class EntityManagerServiceCollection {
2206
2205
  return this;
2207
2206
  }
2208
2207
  mapImportExport(settings) {
2208
+ this.locator.registerVersionCommand(this.entityName, new EntityVersionCommand(this.resolver));
2209
2209
  this.locator.registerExportCommand(this.entityName, new EntitiesExportCommand(this.resolver, settings));
2210
2210
  this.locator.registerExportAction(this.entityName, new EntitiesExportAction(this.resolver));
2211
2211
  this.locator.registerImportCommand(this.entityName, new EntitiesImportCommand(this.resolver, settings));