@punks/backend-entity-manager 0.0.224 → 0.0.226
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 +39 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/abstractions/converters.d.ts +3 -2
- package/dist/cjs/types/actions/create.d.ts +1 -0
- package/dist/cjs/types/actions/update.d.ts +1 -0
- package/dist/cjs/types/actions/upsert.d.ts +1 -0
- package/dist/cjs/types/services/events.d.ts +2 -0
- package/dist/esm/index.js +39 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/abstractions/converters.d.ts +3 -2
- package/dist/esm/types/actions/create.d.ts +1 -0
- package/dist/esm/types/actions/update.d.ts +1 -0
- package/dist/esm/types/actions/upsert.d.ts +1 -0
- package/dist/esm/types/services/events.d.ts +2 -0
- package/dist/index.d.ts +2 -2
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DeepPartial } from "../types";
|
|
2
|
+
import { IAuthenticationContext } from "./authentication";
|
|
2
3
|
export interface IEntitiesSearchActionConverter<TEntity, TListItemDto> {
|
|
3
4
|
toListItemDto(entity: TEntity): TListItemDto;
|
|
4
5
|
}
|
|
@@ -6,12 +7,12 @@ export interface IEntityGetActionConverter<TEntity, TEntityDto> {
|
|
|
6
7
|
toEntityDto(entity: TEntity): TEntityDto;
|
|
7
8
|
}
|
|
8
9
|
export interface IEntityCreateActionConverter<TEntity, TEntityDto, TEntityCreateDto> {
|
|
9
|
-
createDtoToEntity(data: TEntityCreateDto): DeepPartial<TEntity>;
|
|
10
|
+
createDtoToEntity(data: TEntityCreateDto, context?: IAuthenticationContext<unknown>): DeepPartial<TEntity>;
|
|
10
11
|
toEntityDto(entity: TEntity): TEntityDto;
|
|
11
12
|
}
|
|
12
13
|
export interface IEntityUpdateActionConverter<TEntity, TEntityDto, TEntityUpdateDto> {
|
|
13
14
|
toEntityDto(entity: TEntity): TEntityDto;
|
|
14
|
-
updateDtoToEntity(data: TEntityUpdateDto): DeepPartial<TEntity>;
|
|
15
|
+
updateDtoToEntity(data: TEntityUpdateDto, context?: IAuthenticationContext<unknown>): DeepPartial<TEntity>;
|
|
15
16
|
}
|
|
16
17
|
export interface IEntityConverter<TEntity, TEntityDto, TListItemDto, TEntityCreateDto, TEntityUpdateDto> extends IEntitiesSearchActionConverter<TEntity, TListItemDto>, IEntityGetActionConverter<TEntity, TEntityDto>, IEntityCreateActionConverter<TEntity, TEntityDto, TEntityCreateDto>, IEntityUpdateActionConverter<TEntity, TEntityDto, TEntityUpdateDto> {
|
|
17
18
|
}
|
|
@@ -3,9 +3,11 @@ import { EntityServiceLocator } from "../providers/services";
|
|
|
3
3
|
export declare class EntityEventsManager<TEntity, TEntityId> implements IEntityEventsManager<TEntity, TEntityId> {
|
|
4
4
|
private readonly entityName;
|
|
5
5
|
private readonly services;
|
|
6
|
+
private readonly logger;
|
|
6
7
|
constructor(entityName: string, services: EntityServiceLocator<TEntity, TEntityId>);
|
|
7
8
|
processEntityCreatedEvent(entity: TEntity): Promise<void>;
|
|
8
9
|
processEntityUpdatedEvent(entity: TEntity): Promise<void>;
|
|
9
10
|
processEntityDeletedEvent(id: TEntityId): Promise<void>;
|
|
10
11
|
private emitEntityEvent;
|
|
12
|
+
private buildEventName;
|
|
11
13
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -362,7 +362,9 @@ class EntityCreateAction {
|
|
|
362
362
|
async execute(input) {
|
|
363
363
|
this.logger.debug("Create action started", { input });
|
|
364
364
|
const converter = this.services.resolveConverter();
|
|
365
|
-
const
|
|
365
|
+
const context = await this.getContext();
|
|
366
|
+
const createInput = converter?.createDtoToEntity(input, context) ??
|
|
367
|
+
input;
|
|
366
368
|
const ref = await this.services
|
|
367
369
|
.resolveCreateCommand()
|
|
368
370
|
.execute(createInput);
|
|
@@ -376,6 +378,14 @@ class EntityCreateAction {
|
|
|
376
378
|
this.logger.debug("Create action completed", { input, result });
|
|
377
379
|
return result;
|
|
378
380
|
}
|
|
381
|
+
async getContext() {
|
|
382
|
+
const authorization = this.services.resolveAuthorizationMiddleware();
|
|
383
|
+
if (!authorization) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const contextService = this.services.resolveAuthenticationContextProvider();
|
|
387
|
+
return await contextService?.getContext();
|
|
388
|
+
}
|
|
379
389
|
}
|
|
380
390
|
|
|
381
391
|
class EntityDeleteAction {
|
|
@@ -491,7 +501,9 @@ class EntityUpdateAction {
|
|
|
491
501
|
async execute(id, input) {
|
|
492
502
|
this.logger.debug("Update action started", { id, input });
|
|
493
503
|
const converter = this.services.resolveConverter();
|
|
494
|
-
const
|
|
504
|
+
const context = await this.getContext();
|
|
505
|
+
const updateInput = converter?.updateDtoToEntity(input, context) ??
|
|
506
|
+
input;
|
|
495
507
|
await this.services
|
|
496
508
|
.resolveUpdateCommand()
|
|
497
509
|
.execute(id, updateInput);
|
|
@@ -503,6 +515,14 @@ class EntityUpdateAction {
|
|
|
503
515
|
this.logger.debug("Update action started", { id, input, result });
|
|
504
516
|
return result;
|
|
505
517
|
}
|
|
518
|
+
async getContext() {
|
|
519
|
+
const authorization = this.services.resolveAuthorizationMiddleware();
|
|
520
|
+
if (!authorization) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const contextService = this.services.resolveAuthenticationContextProvider();
|
|
524
|
+
return await contextService?.getContext();
|
|
525
|
+
}
|
|
506
526
|
}
|
|
507
527
|
|
|
508
528
|
class EntityUpsertAction {
|
|
@@ -513,7 +533,9 @@ class EntityUpsertAction {
|
|
|
513
533
|
async execute(id, input) {
|
|
514
534
|
this.logger.debug("Upsert action started", { id, input });
|
|
515
535
|
const converter = this.services.resolveConverter();
|
|
516
|
-
const
|
|
536
|
+
const context = await this.getContext();
|
|
537
|
+
const updateInput = converter?.updateDtoToEntity(input, context) ??
|
|
538
|
+
input;
|
|
517
539
|
await this.services
|
|
518
540
|
.resolveUpsertCommand()
|
|
519
541
|
.execute(id, updateInput);
|
|
@@ -525,6 +547,14 @@ class EntityUpsertAction {
|
|
|
525
547
|
this.logger.debug("Upsert action started", { id, input, result });
|
|
526
548
|
return result;
|
|
527
549
|
}
|
|
550
|
+
async getContext() {
|
|
551
|
+
const authorization = this.services.resolveAuthorizationMiddleware();
|
|
552
|
+
if (!authorization) {
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
const contextService = this.services.resolveAuthenticationContextProvider();
|
|
556
|
+
return await contextService?.getContext();
|
|
557
|
+
}
|
|
528
558
|
}
|
|
529
559
|
|
|
530
560
|
class EntityVersionsSearchAction {
|
|
@@ -2124,6 +2154,7 @@ class EntityEventsManager {
|
|
|
2124
2154
|
constructor(entityName, services) {
|
|
2125
2155
|
this.entityName = entityName;
|
|
2126
2156
|
this.services = services;
|
|
2157
|
+
this.logger = Log.getLogger("EntityEventsManager");
|
|
2127
2158
|
}
|
|
2128
2159
|
async processEntityCreatedEvent(entity) {
|
|
2129
2160
|
this.services.resolveReplicaSyncManager().syncReplicas(entity);
|
|
@@ -2151,7 +2182,11 @@ class EntityEventsManager {
|
|
|
2151
2182
|
if (!eventEmitter) {
|
|
2152
2183
|
return;
|
|
2153
2184
|
}
|
|
2154
|
-
|
|
2185
|
+
this.logger.debug(`Event emitted: ${this.buildEventName(event)}`, payload);
|
|
2186
|
+
await eventEmitter.emit(this.buildEventName(event), payload);
|
|
2187
|
+
}
|
|
2188
|
+
buildEventName(event) {
|
|
2189
|
+
return `${this.entityName}.${event}`;
|
|
2155
2190
|
}
|
|
2156
2191
|
}
|
|
2157
2192
|
|