@ruiapp/rapid-core 0.1.52 → 0.1.54

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.
@@ -2,5 +2,5 @@ export default class EventManager<EventTypes extends Record<string, any[]>> {
2
2
  #private;
3
3
  constructor();
4
4
  on<K extends keyof EventTypes>(eventName: K, listener: (...args: EventTypes[K]) => void): void;
5
- emit<K extends keyof EventTypes>(eventName: K, ...args: EventTypes[K]): boolean;
5
+ emit<K extends keyof EventTypes>(eventName: K, ...args: EventTypes[K]): Promise<void>;
6
6
  }
package/dist/index.js CHANGED
@@ -726,8 +726,11 @@ class EventManager {
726
726
  on(eventName, listener) {
727
727
  this.#eventEmitter.on(eventName, listener);
728
728
  }
729
- emit(eventName, ...args) {
730
- return this.#eventEmitter.emit(eventName, ...args);
729
+ async emit(eventName, ...args) {
730
+ const listeners = this.#eventEmitter.listeners(eventName);
731
+ for (const listener of listeners) {
732
+ await listener(...args);
733
+ }
731
734
  }
732
735
  }
733
736
 
@@ -2110,6 +2113,7 @@ async function findEntities(server, dataAccessor, options) {
2110
2113
  }
2111
2114
  const entities = rows.map((item) => mapDbRowToEntity(server, model, item, options.keepNonPropertyFields));
2112
2115
  await server.emitEvent("entity.beforeResponse", {
2116
+ routerContext: options.routerContext,
2113
2117
  namespace: model.namespace,
2114
2118
  modelSingularCode: model.singularCode,
2115
2119
  baseModelSingularCode: model.base,
@@ -2404,6 +2408,7 @@ async function createEntity(server, dataAccessor, options, plugin) {
2404
2408
  const { entity } = options;
2405
2409
  await server.beforeCreateEntity(model, options);
2406
2410
  await server.emitEvent("entity.beforeCreate", {
2411
+ routerContext: options.routerContext,
2407
2412
  namespace: model.namespace,
2408
2413
  modelSingularCode: model.singularCode,
2409
2414
  baseModelSingularCode: model.base,
@@ -2544,6 +2549,7 @@ async function createEntity(server, dataAccessor, options, plugin) {
2544
2549
  }
2545
2550
  }
2546
2551
  await server.emitEvent("entity.create", {
2552
+ routerContext: options.routerContext,
2547
2553
  namespace: model.namespace,
2548
2554
  modelSingularCode: model.singularCode,
2549
2555
  baseModelSingularCode: model.base,
@@ -3181,7 +3187,7 @@ class RapidServer {
3181
3187
  async beforeUpdateEntity(model, options, currentEntity) {
3182
3188
  await this.#pluginManager.beforeUpdateEntity(model, options, currentEntity);
3183
3189
  }
3184
- #handleEntityEvent(eventName, sender, payload) {
3190
+ async #handleEntityEvent(eventName, sender, payload) {
3185
3191
  const { modelSingularCode, baseModelSingularCode } = payload;
3186
3192
  const entityWatchHandlerContext = {
3187
3193
  server: this,
@@ -3215,9 +3221,9 @@ class RapidServer {
3215
3221
  else if (eventName === "entity.beforeResponse") {
3216
3222
  emitter = this.#entityBeforeResponseEventEmitters;
3217
3223
  }
3218
- emitter.emit(modelSingularCode, entityWatchHandlerContext);
3224
+ await emitter.emit(modelSingularCode, entityWatchHandlerContext);
3219
3225
  if (baseModelSingularCode) {
3220
- emitter.emit(baseModelSingularCode, entityWatchHandlerContext);
3226
+ await emitter.emit(baseModelSingularCode, entityWatchHandlerContext);
3221
3227
  }
3222
3228
  }
3223
3229
  }
@@ -3756,6 +3762,7 @@ const code$m = "findCollectionEntities";
3756
3762
  async function handler$m(plugin, ctx, options) {
3757
3763
  await runCollectionEntityActionHandler(ctx, options, code$m, async (entityManager, input) => {
3758
3764
  input.filters = removeFiltersWithNullValue(input.filters);
3765
+ input.routerContext = ctx.routerContext;
3759
3766
  const entities = await entityManager.findEntities(input);
3760
3767
  const result = { list: entities };
3761
3768
  if (input.pagination && !input.pagination.withoutTotal) {
@@ -3819,6 +3826,7 @@ async function handler$j(plugin, ctx, options) {
3819
3826
  const entityManager = server.getEntityManager(options.singularCode);
3820
3827
  const output = await entityManager.createEntity({
3821
3828
  entity: input,
3829
+ routerContext: ctx.routerContext,
3822
3830
  }, plugin);
3823
3831
  ctx.output = output;
3824
3832
  }
@@ -3848,6 +3856,7 @@ async function handler$i(plugin, ctx, options) {
3848
3856
  const entityManager = server.getEntityManager(options.singularCode);
3849
3857
  const newEntity = await entityManager.createEntity({
3850
3858
  entity: mergedEntity,
3859
+ routerContext: ctx.routerContext,
3851
3860
  }, plugin);
3852
3861
  output.push(newEntity);
3853
3862
  }
@@ -3908,6 +3917,7 @@ async function handler$f(plugin, ctx, options) {
3908
3917
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3909
3918
  logger.debug(`Running ${code$f} handler...`, { defaultInput, fixedInput, mergedInput });
3910
3919
  const entityManager = server.getEntityManager(options.singularCode);
3920
+ mergedInput.routerContext = ctx.routerContext;
3911
3921
  await entityManager.addRelations(mergedInput, plugin);
3912
3922
  ctx.output = {};
3913
3923
  }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { RouteContext } from "./core/routeContext";
1
2
  import { IRpdServer, RapidPlugin } from "./core/server";
2
3
  import { CountRowOptions, FindRowOptions } from "./dataAccess/dataAccessTypes";
3
4
  export type RapidServerConfig = {
@@ -66,18 +67,21 @@ export type RpdServerEventTypes = {
66
67
  "entity.beforeResponse": [RapidPlugin, RpdEntityBeforeResponseEventPayload];
67
68
  };
68
69
  export interface RpdEntityBeforeCreateEventPayload {
70
+ routerContext?: RouteContext;
69
71
  namespace: string;
70
72
  modelSingularCode: string;
71
73
  baseModelSingularCode?: string;
72
74
  before: any;
73
75
  }
74
76
  export interface RpdEntityCreateEventPayload {
77
+ routerContext?: RouteContext;
75
78
  namespace: string;
76
79
  modelSingularCode: string;
77
80
  baseModelSingularCode?: string;
78
81
  after: any;
79
82
  }
80
83
  export interface RpdEntityBeforeUpdateEventPayload {
84
+ routerContext?: RouteContext;
81
85
  namespace: string;
82
86
  modelSingularCode: string;
83
87
  baseModelSingularCode?: string;
@@ -85,6 +89,7 @@ export interface RpdEntityBeforeUpdateEventPayload {
85
89
  changes: any;
86
90
  }
87
91
  export interface RpdEntityUpdateEventPayload {
92
+ routerContext?: RouteContext;
88
93
  namespace: string;
89
94
  modelSingularCode: string;
90
95
  baseModelSingularCode?: string;
@@ -93,18 +98,21 @@ export interface RpdEntityUpdateEventPayload {
93
98
  changes: any;
94
99
  }
95
100
  export interface RpdEntityBeforeDeleteEventPayload {
101
+ routerContext?: RouteContext;
96
102
  namespace: string;
97
103
  modelSingularCode: string;
98
104
  baseModelSingularCode?: string;
99
105
  before: any;
100
106
  }
101
107
  export interface RpdEntityDeleteEventPayload {
108
+ routerContext?: RouteContext;
102
109
  namespace: string;
103
110
  modelSingularCode: string;
104
111
  baseModelSingularCode?: string;
105
112
  before: any;
106
113
  }
107
114
  export interface RpdEntityAddRelationsEventPayload {
115
+ routerContext?: RouteContext;
108
116
  namespace: string;
109
117
  modelSingularCode: string;
110
118
  baseModelSingularCode?: string;
@@ -113,6 +121,7 @@ export interface RpdEntityAddRelationsEventPayload {
113
121
  relations: any[];
114
122
  }
115
123
  export interface RpdEntityRemoveRelationsEventPayload {
124
+ routerContext?: RouteContext;
116
125
  namespace: string;
117
126
  modelSingularCode: string;
118
127
  baseModelSingularCode?: string;
@@ -121,6 +130,7 @@ export interface RpdEntityRemoveRelationsEventPayload {
121
130
  relations: any[];
122
131
  }
123
132
  export interface RpdEntityBeforeResponseEventPayload {
133
+ routerContext?: RouteContext;
124
134
  namespace: string;
125
135
  modelSingularCode: string;
126
136
  baseModelSingularCode?: string;
@@ -335,6 +345,7 @@ export type EntityFilterOperators = EntityFilterRelationalOperators | EntityFilt
335
345
  export type EntityFilterOptions = FindEntityRelationalFilterOptions | FindEntitySetFilterOptions | FindEntityLogicalFilterOptions | FindEntityUnaryFilterOptions | FindEntityExistenceFilterOptions;
336
346
  export type EntityNonRelationPropertyFilterOptions = FindEntityRelationalFilterOptions | FindEntitySetFilterOptions | FindEntityUnaryFilterOptions;
337
347
  export interface FindEntityOptions {
348
+ routerContext?: RouteContext;
338
349
  filters?: EntityFilterOptions[];
339
350
  orderBy?: FindEntityOrderByOptions[];
340
351
  pagination?: FindEntityPaginationOptions;
@@ -381,22 +392,27 @@ export interface CountEntityResult {
381
392
  count: number;
382
393
  }
383
394
  export interface CreateEntityOptions {
395
+ routerContext?: RouteContext;
384
396
  entity: any;
385
397
  }
386
398
  export interface UpdateEntityOptions {
399
+ routerContext?: RouteContext;
387
400
  filters?: EntityFilterOptions[];
388
401
  entity: any;
389
402
  }
390
403
  export interface UpdateEntityByIdOptions {
404
+ routerContext?: RouteContext;
391
405
  id: any;
392
406
  entityToSave: any;
393
407
  operation?: any;
394
408
  stateProperties?: string[];
395
409
  }
396
410
  export interface DeleteEntityOptions {
411
+ routerContext?: RouteContext;
397
412
  filters?: EntityFilterOptions[];
398
413
  }
399
414
  export interface AddEntityRelationsOptions {
415
+ routerContext?: RouteContext;
400
416
  id: number;
401
417
  property: string;
402
418
  relations: {
@@ -405,6 +421,7 @@ export interface AddEntityRelationsOptions {
405
421
  }[];
406
422
  }
407
423
  export interface RemoveEntityRelationsOptions {
424
+ routerContext?: RouteContext;
408
425
  id: number;
409
426
  property: string;
410
427
  relations: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.1.52",
3
+ "version": "0.1.54",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,10 @@ export default class EventManager<EventTypes extends Record<string, any[]>> {
11
11
  this.#eventEmitter.on(eventName as string, listener);
12
12
  }
13
13
 
14
- emit<K extends keyof EventTypes>(eventName: K, ...args: EventTypes[K]) {
15
- return this.#eventEmitter.emit(eventName as string, ...args);
14
+ async emit<K extends keyof EventTypes>(eventName: K, ...args: EventTypes[K]) {
15
+ const listeners = this.#eventEmitter.listeners(eventName as string);
16
+ for (const listener of listeners) {
17
+ await listener(...args);
18
+ }
16
19
  }
17
20
  }
@@ -196,6 +196,7 @@ async function findEntities(server: IRpdServer, dataAccessor: IRpdDataAccessor,
196
196
  await server.emitEvent(
197
197
  "entity.beforeResponse",
198
198
  {
199
+ routerContext: options.routerContext,
199
200
  namespace: model.namespace,
200
201
  modelSingularCode: model.singularCode,
201
202
  baseModelSingularCode: model.base,
@@ -512,6 +513,7 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
512
513
  await server.emitEvent(
513
514
  "entity.beforeCreate",
514
515
  {
516
+ routerContext: options.routerContext,
515
517
  namespace: model.namespace,
516
518
  modelSingularCode: model.singularCode,
517
519
  baseModelSingularCode: model.base,
@@ -665,6 +667,7 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
665
667
  await server.emitEvent(
666
668
  "entity.create",
667
669
  {
670
+ routerContext: options.routerContext,
668
671
  namespace: model.namespace,
669
672
  modelSingularCode: model.singularCode,
670
673
  baseModelSingularCode: model.base,
@@ -1,4 +1,4 @@
1
- import { RunEntityActionHandlerOptions } from "~/types";
1
+ import { AddEntityRelationsOptions, RunEntityActionHandlerOptions } from "~/types";
2
2
  import { mergeInput } from "~/helpers/inputHelper";
3
3
  import { ActionHandlerContext } from "~/core/actionHandler";
4
4
  import { RapidPlugin } from "~/core/server";
@@ -9,10 +9,11 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
9
9
  const { logger, server, input } = ctx;
10
10
 
11
11
  const { defaultInput, fixedInput } = options;
12
- const mergedInput = mergeInput(defaultInput, input, fixedInput);
12
+ const mergedInput: AddEntityRelationsOptions = mergeInput(defaultInput, input, fixedInput);
13
13
  logger.debug(`Running ${code} handler...`, { defaultInput, fixedInput, mergedInput });
14
14
 
15
15
  const entityManager = server.getEntityManager(options.singularCode);
16
+ mergedInput.routerContext = ctx.routerContext;
16
17
  await entityManager.addRelations(mergedInput, plugin);
17
18
 
18
19
  ctx.output = {};
@@ -30,6 +30,7 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
30
30
  const newEntity = await entityManager.createEntity(
31
31
  {
32
32
  entity: mergedEntity,
33
+ routerContext: ctx.routerContext,
33
34
  },
34
35
  plugin,
35
36
  );
@@ -21,6 +21,7 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
21
21
  const output = await entityManager.createEntity(
22
22
  {
23
23
  entity: input,
24
+ routerContext: ctx.routerContext,
24
25
  },
25
26
  plugin,
26
27
  );
@@ -9,6 +9,7 @@ export const code = "findCollectionEntities";
9
9
  export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: RunEntityActionHandlerOptions) {
10
10
  await runCollectionEntityActionHandler(ctx, options, code, async (entityManager, input: FindEntityOptions) => {
11
11
  input.filters = removeFiltersWithNullValue(input.filters);
12
+ input.routerContext = ctx.routerContext;
12
13
  const entities = await entityManager.findEntities(input);
13
14
  const result: {
14
15
  list: any;
package/src/server.ts CHANGED
@@ -392,7 +392,7 @@ export class RapidServer implements IRpdServer {
392
392
  await this.#pluginManager.beforeUpdateEntity(model, options, currentEntity);
393
393
  }
394
394
 
395
- #handleEntityEvent(eventName: keyof RpdServerEventTypes, sender: RapidPlugin, payload: RpdEntityCreateEventPayload) {
395
+ async #handleEntityEvent(eventName: keyof RpdServerEventTypes, sender: RapidPlugin, payload: RpdEntityCreateEventPayload) {
396
396
  const { modelSingularCode, baseModelSingularCode } = payload;
397
397
  const entityWatchHandlerContext: EntityWatchHandlerContext<typeof eventName> = {
398
398
  server: this,
@@ -420,9 +420,9 @@ export class RapidServer implements IRpdServer {
420
420
  emitter = this.#entityBeforeResponseEventEmitters;
421
421
  }
422
422
 
423
- emitter.emit(modelSingularCode, entityWatchHandlerContext);
423
+ await emitter.emit(modelSingularCode, entityWatchHandlerContext);
424
424
  if (baseModelSingularCode) {
425
- emitter.emit(baseModelSingularCode, entityWatchHandlerContext);
425
+ await emitter.emit(baseModelSingularCode, entityWatchHandlerContext);
426
426
  }
427
427
  }
428
428
  }
package/src/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { RouteContext } from "./core/routeContext";
1
2
  import { IRpdServer, RapidPlugin } from "./core/server";
2
3
  import { CountRowOptions, FindRowOptions } from "./dataAccess/dataAccessTypes";
3
4
 
@@ -79,6 +80,7 @@ export type RpdServerEventTypes = {
79
80
  };
80
81
 
81
82
  export interface RpdEntityBeforeCreateEventPayload {
83
+ routerContext?: RouteContext;
82
84
  namespace: string;
83
85
  modelSingularCode: string;
84
86
  baseModelSingularCode?: string;
@@ -86,6 +88,7 @@ export interface RpdEntityBeforeCreateEventPayload {
86
88
  }
87
89
 
88
90
  export interface RpdEntityCreateEventPayload {
91
+ routerContext?: RouteContext;
89
92
  namespace: string;
90
93
  modelSingularCode: string;
91
94
  baseModelSingularCode?: string;
@@ -93,6 +96,7 @@ export interface RpdEntityCreateEventPayload {
93
96
  }
94
97
 
95
98
  export interface RpdEntityBeforeUpdateEventPayload {
99
+ routerContext?: RouteContext;
96
100
  namespace: string;
97
101
  modelSingularCode: string;
98
102
  baseModelSingularCode?: string;
@@ -101,6 +105,7 @@ export interface RpdEntityBeforeUpdateEventPayload {
101
105
  }
102
106
 
103
107
  export interface RpdEntityUpdateEventPayload {
108
+ routerContext?: RouteContext;
104
109
  namespace: string;
105
110
  modelSingularCode: string;
106
111
  baseModelSingularCode?: string;
@@ -110,6 +115,7 @@ export interface RpdEntityUpdateEventPayload {
110
115
  }
111
116
 
112
117
  export interface RpdEntityBeforeDeleteEventPayload {
118
+ routerContext?: RouteContext;
113
119
  namespace: string;
114
120
  modelSingularCode: string;
115
121
  baseModelSingularCode?: string;
@@ -117,6 +123,7 @@ export interface RpdEntityBeforeDeleteEventPayload {
117
123
  }
118
124
 
119
125
  export interface RpdEntityDeleteEventPayload {
126
+ routerContext?: RouteContext;
120
127
  namespace: string;
121
128
  modelSingularCode: string;
122
129
  baseModelSingularCode?: string;
@@ -124,6 +131,7 @@ export interface RpdEntityDeleteEventPayload {
124
131
  }
125
132
 
126
133
  export interface RpdEntityAddRelationsEventPayload {
134
+ routerContext?: RouteContext;
127
135
  namespace: string;
128
136
  modelSingularCode: string;
129
137
  baseModelSingularCode?: string;
@@ -133,6 +141,7 @@ export interface RpdEntityAddRelationsEventPayload {
133
141
  }
134
142
 
135
143
  export interface RpdEntityRemoveRelationsEventPayload {
144
+ routerContext?: RouteContext;
136
145
  namespace: string;
137
146
  modelSingularCode: string;
138
147
  baseModelSingularCode?: string;
@@ -142,6 +151,7 @@ export interface RpdEntityRemoveRelationsEventPayload {
142
151
  }
143
152
 
144
153
  export interface RpdEntityBeforeResponseEventPayload {
154
+ routerContext?: RouteContext;
145
155
  namespace: string;
146
156
  modelSingularCode: string;
147
157
  baseModelSingularCode?: string;
@@ -389,6 +399,7 @@ export type EntityFilterOptions = FindEntityRelationalFilterOptions | FindEntity
389
399
  export type EntityNonRelationPropertyFilterOptions = FindEntityRelationalFilterOptions | FindEntitySetFilterOptions | FindEntityUnaryFilterOptions;
390
400
 
391
401
  export interface FindEntityOptions {
402
+ routerContext?: RouteContext;
392
403
  filters?: EntityFilterOptions[];
393
404
  orderBy?: FindEntityOrderByOptions[];
394
405
  pagination?: FindEntityPaginationOptions;
@@ -445,15 +456,18 @@ export interface CountEntityResult {
445
456
  }
446
457
 
447
458
  export interface CreateEntityOptions {
459
+ routerContext?: RouteContext;
448
460
  entity: any;
449
461
  }
450
462
 
451
463
  export interface UpdateEntityOptions {
464
+ routerContext?: RouteContext;
452
465
  filters?: EntityFilterOptions[];
453
466
  entity: any;
454
467
  }
455
468
 
456
469
  export interface UpdateEntityByIdOptions {
470
+ routerContext?: RouteContext;
457
471
  id: any;
458
472
  entityToSave: any;
459
473
  operation?: any;
@@ -461,16 +475,19 @@ export interface UpdateEntityByIdOptions {
461
475
  }
462
476
 
463
477
  export interface DeleteEntityOptions {
478
+ routerContext?: RouteContext;
464
479
  filters?: EntityFilterOptions[];
465
480
  }
466
481
 
467
482
  export interface AddEntityRelationsOptions {
483
+ routerContext?: RouteContext;
468
484
  id: number;
469
485
  property: string;
470
486
  relations: { id?: number; [k: string]: any }[];
471
487
  }
472
488
 
473
489
  export interface RemoveEntityRelationsOptions {
490
+ routerContext?: RouteContext;
474
491
  id: number;
475
492
  property: string;
476
493
  relations: { id?: number; [k: string]: any }[];