@punks/backend-entity-manager 0.0.137 → 0.0.138

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
@@ -524,11 +524,13 @@ class EntityCreateCommand {
524
524
  }
525
525
  const contextService = this.services.resolveAuthenticationContextProvider();
526
526
  const context = await contextService?.getContext();
527
+ const snapshotService = this.services.resolveSnapshotService();
528
+ const snapshot = (await snapshotService?.getSnapshot(id)) ?? entity;
527
529
  await versioning.createVersion({
528
530
  operationType: exports.EntityVersionOperation.Create,
529
531
  entityId: id,
530
532
  entityType: this.services.getEntityName(),
531
- data: entity,
533
+ data: snapshot,
532
534
  modifiedByUserId: context?.userId,
533
535
  });
534
536
  }
@@ -578,10 +580,13 @@ class EntityDeleteCommand {
578
580
  }
579
581
  const contextService = this.services.resolveAuthenticationContextProvider();
580
582
  const context = await contextService?.getContext();
583
+ const snapshotService = this.services.resolveSnapshotService();
584
+ const snapshot = await snapshotService?.getSnapshot(id);
581
585
  await versioning.createVersion({
582
586
  operationType: exports.EntityVersionOperation.Delete,
583
587
  entityId: id,
584
588
  entityType: this.services.getEntityName(),
589
+ data: snapshot,
585
590
  modifiedByUserId: context?.userId,
586
591
  });
587
592
  }
@@ -821,11 +826,13 @@ class EntityUpdateCommand {
821
826
  }
822
827
  const contextService = this.services.resolveAuthenticationContextProvider();
823
828
  const context = await contextService?.getContext();
829
+ const snapshotService = this.services.resolveSnapshotService();
830
+ const snapshot = (await snapshotService?.getSnapshot(id)) ?? entity;
824
831
  await versioning.createVersion({
825
832
  operationType: exports.EntityVersionOperation.Update,
826
833
  entityId: id,
827
834
  entityType: this.services.getEntityName(),
828
- data: entity,
835
+ data: snapshot,
829
836
  modifiedByUserId: context?.userId,
830
837
  });
831
838
  }
@@ -888,11 +895,13 @@ class EntityUpsertCommand {
888
895
  }
889
896
  const contextService = this.services.resolveAuthenticationContextProvider();
890
897
  const context = await contextService?.getContext();
898
+ const snapshotService = this.services.resolveSnapshotService();
899
+ const snapshot = (await snapshotService?.getSnapshot(id)) ?? entity;
891
900
  await versioning.createVersion({
892
901
  operationType: exports.EntityVersionOperation.Upsert,
893
902
  entityId: id,
894
903
  entityType: this.services.getEntityName(),
895
- data: entity,
904
+ data: snapshot,
896
905
  modifiedByUserId: context?.userId,
897
906
  });
898
907
  }
@@ -1094,6 +1103,7 @@ const EntityServices = {
1094
1103
  IEntityAdapter: "IEntityAdapter",
1095
1104
  IEntityConverter: "IEntityConverter",
1096
1105
  IEntitySerializer: "IEntitySerializer",
1106
+ IEntitySnapshotService: "IEntitySnapshotService",
1097
1107
  },
1098
1108
  Connectors: {
1099
1109
  IConnectorsConfiguration: "IConnectorsConfiguration",
@@ -1209,6 +1219,9 @@ class EntityServiceLocator {
1209
1219
  resolveSerializer() {
1210
1220
  return this.services.resolveSerializer(this.entityName);
1211
1221
  }
1222
+ resolveSnapshotService() {
1223
+ return this.services.resolveSnapshotService(this.entityName);
1224
+ }
1212
1225
  resolveAdapter() {
1213
1226
  return this.services.resolveAdapter(this.entityName);
1214
1227
  }
@@ -1479,6 +1492,14 @@ class EntitiesServiceLocator {
1479
1492
  resolveSerializer(entityName) {
1480
1493
  return this.provider.resolveEntityService(EntityServices.Converters.IEntitySerializer, entityName);
1481
1494
  }
1495
+ registerSnapshotService(entityName, instance) {
1496
+ this.provider.registerEntityService(EntityServices.Converters.IEntitySnapshotService, entityName, instance);
1497
+ }
1498
+ resolveSnapshotService(entityName) {
1499
+ return this.provider.resolveEntityService(EntityServices.Converters.IEntitySnapshotService, entityName, {
1500
+ optional: true,
1501
+ });
1502
+ }
1482
1503
  resolveAuthorizationMiddleware(entityName) {
1483
1504
  return this.provider.resolveEntityService(EntityServices.Authorization.IEntityAuthorizationMiddleware, entityName, {
1484
1505
  optional: true,
@@ -2209,6 +2230,10 @@ class EntityManagerServiceCollection {
2209
2230
  this.locator.registerSerializer(this.entityName, serializer);
2210
2231
  return this;
2211
2232
  }
2233
+ addSnapshotService(service) {
2234
+ this.locator.registerSnapshotService(this.entityName, service);
2235
+ return this;
2236
+ }
2212
2237
  addConverter(converter) {
2213
2238
  this.locator.registerConverter(this.entityName, converter);
2214
2239
  return this;
@@ -2299,6 +2324,7 @@ const EntityManagerSymbols = {
2299
2324
  EntityConnector: Symbol.for("WP:ENTITY_CONNECTOR"),
2300
2325
  EntityConverter: Symbol.for("WP:ENTITY_CONVERTER"),
2301
2326
  EntitySerializer: Symbol.for("WP:ENTITY_SERIALIZER"),
2327
+ EntitySnapshotService: Symbol.for("WP:ENTITY_SNAPSHOT_SERVICE"),
2302
2328
  EntityManager: Symbol.for("WP:ENTITY_MANAGER"),
2303
2329
  EntityQueryBuilder: Symbol.for("WP:ENTITY_QUERY_BUILDER"),
2304
2330
  EntitySeeder: Symbol.for("WP:ENTITY_SEEDER"),
@@ -2389,6 +2415,11 @@ const WpEntitySerializer = (entityName, props = {}) => common.applyDecorators(co
2389
2415
  ...props,
2390
2416
  }));
2391
2417
 
2418
+ const WpEntitySnapshotService = (entityName, props = {}) => common.applyDecorators(common.Injectable(), common.SetMetadata(EntityManagerSymbols.EntitySnapshotService, {
2419
+ entityName,
2420
+ ...props,
2421
+ }));
2422
+
2392
2423
  const WpEventsTracker = (props = {}) => common.applyDecorators(common.Injectable(), common.SetMetadata(EntityManagerSymbols.EventsTracker, {
2393
2424
  ...props,
2394
2425
  }));
@@ -2731,7 +2762,7 @@ exports.EntityManagerRegistry = class EntityManagerRegistry {
2731
2762
  .registerEventEmitter(eventEmitter);
2732
2763
  }
2733
2764
  }
2734
- async registerDiscoveredEntity(app, { adapter, entityName, entity, converter, queryBuilder, repository, authMiddleware, serializer, settings, }) {
2765
+ async registerDiscoveredEntity(app, { adapter, entityName, entity, converter, queryBuilder, repository, authMiddleware, serializer, snapshotService, settings, }) {
2735
2766
  if (!repository.discoveredClass.injectType) {
2736
2767
  throw new Error(`No inject type found for entity repository: ${entityName}`);
2737
2768
  }
@@ -2762,6 +2793,12 @@ exports.EntityManagerRegistry = class EntityManagerRegistry {
2762
2793
  if (adapterInstance) {
2763
2794
  registration.addAdapter(adapterInstance);
2764
2795
  }
2796
+ const snapshotServiceInstance = snapshotService?.discoveredClass.injectType
2797
+ ? (await app.resolve(snapshotService.discoveredClass.injectType))
2798
+ : undefined;
2799
+ if (snapshotServiceInstance) {
2800
+ registration.addSnapshotService(snapshotServiceInstance);
2801
+ }
2765
2802
  const authMiddlewareInstance = authMiddleware?.discoveredClass.injectType
2766
2803
  ? (await app.resolve(authMiddleware.discoveredClass.injectType))
2767
2804
  : undefined;
@@ -21397,12 +21434,14 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
21397
21434
  const queryBuilders = await this.discoverQueryBuilders();
21398
21435
  const converters = await this.discoverConverters();
21399
21436
  const serializers = await this.discoverSerializers();
21437
+ const snapshotServices = await this.discoverSnapshotService();
21400
21438
  const adapters = await this.discoverAdapters();
21401
21439
  const auth = await this.discoverAuthMiddlewares();
21402
21440
  const repositoriesDict = backendCore.toDict(repositories, (q) => q.meta.entityName);
21403
21441
  const queryBuilderDict = backendCore.toDict(queryBuilders, (q) => q.meta.entityName);
21404
21442
  const converterDict = backendCore.toDict(converters, (c) => c.meta.entityName);
21405
21443
  const serializerDict = backendCore.toDict(serializers, (s) => s.meta.entityName);
21444
+ const snapshotServicesDict = backendCore.toDict(snapshotServices, (s) => s.meta.entityName);
21406
21445
  const adapterDict = backendCore.toDict(adapters, (a) => a.meta.entityName);
21407
21446
  const authDict = backendCore.toDict(auth, (a) => a.meta.entityName);
21408
21447
  const sortedEntities = lodash.exports.orderBy(entities, (x) => x.meta.name);
@@ -21427,6 +21466,7 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
21427
21466
  const adapter = adapterDict[entityName];
21428
21467
  const authMiddleware = authDict[entityName];
21429
21468
  const serializer = serializerDict[entityName];
21469
+ const snapshotService = snapshotServicesDict[entityName];
21430
21470
  await this.registry.registerDiscoveredEntity(app, {
21431
21471
  entityName,
21432
21472
  entity,
@@ -21435,6 +21475,7 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
21435
21475
  queryBuilder,
21436
21476
  converter,
21437
21477
  serializer,
21478
+ snapshotService,
21438
21479
  authMiddleware,
21439
21480
  settings: staticProviders.settings,
21440
21481
  });
@@ -21444,6 +21485,7 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
21444
21485
  - Adapter: ${adapter?.discoveredClass.injectType?.name ?? ""}
21445
21486
  - Converter: ${converter?.discoveredClass.injectType?.name ?? ""}
21446
21487
  - Serializer: ${serializer?.discoveredClass.injectType?.name ?? ""}
21488
+ - SnapshotService: ${snapshotService?.discoveredClass.injectType?.name ?? ""}
21447
21489
  - Auth middleware: ${authMiddleware?.discoveredClass.injectType?.name ?? ""}
21448
21490
 
21449
21491
  `);
@@ -21586,6 +21628,9 @@ exports.EntityManagerInitializer = EntityManagerInitializer_1 = class EntityMana
21586
21628
  async discoverSerializers() {
21587
21629
  return await this.discover.providersWithMetaAtKey(EntityManagerSymbols.EntitySerializer);
21588
21630
  }
21631
+ async discoverSnapshotService() {
21632
+ return await this.discover.providersWithMetaAtKey(EntityManagerSymbols.EntitySnapshotService);
21633
+ }
21589
21634
  async discoverAdapters() {
21590
21635
  return await this.discover.providersWithMetaAtKey(EntityManagerSymbols.EntityAdapter);
21591
21636
  }
@@ -28168,6 +28213,7 @@ exports.WpEntityQueryBuilder = WpEntityQueryBuilder;
28168
28213
  exports.WpEntityRepository = WpEntityRepository;
28169
28214
  exports.WpEntitySeeder = WpEntitySeeder;
28170
28215
  exports.WpEntitySerializer = WpEntitySerializer;
28216
+ exports.WpEntitySnapshotService = WpEntitySnapshotService;
28171
28217
  exports.WpEntityVersioningProvider = WpEntityVersioningProvider;
28172
28218
  exports.WpEventsTracker = WpEventsTracker;
28173
28219
  exports.WpFileProvider = WpFileProvider;