@ruiapp/rapid-core 0.1.22 → 0.1.24

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.
@@ -23,7 +23,7 @@ export interface IRpdServer {
23
23
  appendModelProperties(modelSingularCode: string, properties: RpdDataModelProperty[]): any;
24
24
  getModel(options: GetModelOptions): RpdDataModel | undefined;
25
25
  registerEventHandler<K extends keyof RpdServerEventTypes>(eventName: K, listener: (...args: RpdServerEventTypes[K]) => void): this;
26
- emitEvent<K extends keyof RpdServerEventTypes>(eventName: K, sender: RapidPlugin, payload: RpdServerEventTypes[K][1]): void;
26
+ emitEvent<K extends keyof RpdServerEventTypes>(eventName: K, payload: RpdServerEventTypes[K][1], sender?: RapidPlugin): void;
27
27
  handleRequest(request: Request, next: Next): Promise<Response>;
28
28
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
29
29
  }
@@ -7,10 +7,10 @@ export default class EntityManager<TEntity = any> {
7
7
  findEntities(options: FindEntityOptions): Promise<TEntity[]>;
8
8
  findEntity(options: FindEntityOptions): Promise<TEntity | null>;
9
9
  findById(id: any, keepNonPropertyFields?: boolean): Promise<TEntity | null>;
10
- createEntity(options: CreateEntityOptions, plugin: RapidPlugin): Promise<TEntity>;
11
- updateEntityById(options: UpdateEntityByIdOptions, plugin: RapidPlugin): Promise<TEntity>;
10
+ createEntity(options: CreateEntityOptions, plugin?: RapidPlugin): Promise<TEntity>;
11
+ updateEntityById(options: UpdateEntityByIdOptions, plugin?: RapidPlugin): Promise<TEntity>;
12
12
  count(options: CountEntityOptions): Promise<CountEntityResult>;
13
- deleteById(id: any, plugin: RapidPlugin): Promise<void>;
14
- addRelations(options: AddEntityRelationsOptions, plugin: RapidPlugin): Promise<void>;
15
- removeRelations(options: RemoveEntityRelationsOptions, plugin: RapidPlugin): Promise<void>;
13
+ deleteById(id: any, plugin?: RapidPlugin): Promise<void>;
14
+ addRelations(options: AddEntityRelationsOptions, plugin?: RapidPlugin): Promise<void>;
15
+ removeRelations(options: RemoveEntityRelationsOptions, plugin?: RapidPlugin): Promise<void>;
16
16
  }
package/dist/index.js CHANGED
@@ -1041,7 +1041,7 @@ class RapidRequest {
1041
1041
  const requestMethod = this.method;
1042
1042
  if (requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH") {
1043
1043
  const req = this.#raw;
1044
- const contentType = this.#headers.get("Content-Type");
1044
+ const contentType = this.#headers.get("Content-Type") || "application/json";
1045
1045
  if (contentType.includes("json")) {
1046
1046
  this.#body = {
1047
1047
  type: "json",
@@ -2171,7 +2171,8 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
2171
2171
  if (Object.keys(row).length) {
2172
2172
  updatedRow = await dataAccessor.updateById(id, row);
2173
2173
  }
2174
- const updatedEntity = Object.assign({}, entity, updatedRow);
2174
+ let updatedEntity = mapDbRowToEntity(model, updatedRow, false);
2175
+ updatedEntity = Object.assign({}, entity, updatedEntity);
2175
2176
  // save many-relation properties
2176
2177
  for (const property of manyRelationPropertiesToUpdate) {
2177
2178
  const relatedEntities = [];
@@ -2245,13 +2246,13 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
2245
2246
  }
2246
2247
  updatedEntity[property.code] = relatedEntities;
2247
2248
  }
2248
- server.emitEvent("entity.update", plugin, {
2249
+ server.emitEvent("entity.update", {
2249
2250
  namespace: model.namespace,
2250
2251
  modelSingularCode: model.singularCode,
2251
2252
  before: entity,
2252
2253
  after: updatedEntity,
2253
2254
  changes: changes,
2254
- });
2255
+ }, plugin);
2255
2256
  return updatedEntity;
2256
2257
  }
2257
2258
  class EntityManager {
@@ -2276,11 +2277,11 @@ class EntityManager {
2276
2277
  async createEntity(options, plugin) {
2277
2278
  const model = this.getModel();
2278
2279
  const newEntity = await createEntity(this.#server, this.#dataAccessor, options);
2279
- this.#server.emitEvent("entity.create", plugin, {
2280
+ this.#server.emitEvent("entity.create", {
2280
2281
  namespace: model.namespace,
2281
2282
  modelSingularCode: model.singularCode,
2282
2283
  after: newEntity,
2283
- });
2284
+ }, plugin);
2284
2285
  return newEntity;
2285
2286
  }
2286
2287
  async updateEntityById(options, plugin) {
@@ -2296,11 +2297,11 @@ class EntityManager {
2296
2297
  return;
2297
2298
  }
2298
2299
  await this.#dataAccessor.deleteById(id);
2299
- this.#server.emitEvent("entity.delete", plugin, {
2300
+ this.#server.emitEvent("entity.delete", {
2300
2301
  namespace: model.namespace,
2301
2302
  modelSingularCode: model.singularCode,
2302
2303
  before: entity,
2303
- });
2304
+ }, plugin);
2304
2305
  }
2305
2306
  async addRelations(options, plugin) {
2306
2307
  const model = this.getModel();
@@ -2330,13 +2331,13 @@ class EntityManager {
2330
2331
  await server.queryDatabaseObject(command, params);
2331
2332
  }
2332
2333
  }
2333
- server.emitEvent("entity.addRelations", plugin, {
2334
+ server.emitEvent("entity.addRelations", {
2334
2335
  namespace: model.namespace,
2335
2336
  modelSingularCode: model.singularCode,
2336
2337
  entity,
2337
2338
  property,
2338
2339
  relations,
2339
- });
2340
+ }, plugin);
2340
2341
  }
2341
2342
  async removeRelations(options, plugin) {
2342
2343
  const model = this.getModel();
@@ -2362,13 +2363,13 @@ class EntityManager {
2362
2363
  await server.queryDatabaseObject(command, params);
2363
2364
  }
2364
2365
  }
2365
- server.emitEvent("entity.removeRelations", plugin, {
2366
+ server.emitEvent("entity.removeRelations", {
2366
2367
  namespace: model.namespace,
2367
2368
  modelSingularCode: model.singularCode,
2368
2369
  entity,
2369
2370
  property,
2370
2371
  relations,
2371
- });
2372
+ }, plugin);
2372
2373
  }
2373
2374
  }
2374
2375
 
@@ -2519,7 +2520,7 @@ class RapidServer {
2519
2520
  this.#eventManager.on(eventName, listener);
2520
2521
  return this;
2521
2522
  }
2522
- async emitEvent(eventName, sender, payload) {
2523
+ async emitEvent(eventName, payload, sender) {
2523
2524
  this.#logger.debug(`Emitting '${eventName}' event.`, { eventName, payload });
2524
2525
  await this.#eventManager.emit(eventName, sender, payload);
2525
2526
  // TODO: should move this logic into metaManager
@@ -2559,10 +2560,15 @@ class RapidServer {
2559
2560
  registerFacilityFactory(factory) {
2560
2561
  this.#facilityFactories.set(factory.name, factory);
2561
2562
  }
2562
- async getFacility(name, options) {
2563
+ async getFacility(name, options, nullIfUnknownFacility) {
2563
2564
  const factory = this.#facilityFactories.get(name);
2564
2565
  if (!factory) {
2565
- throw new Error(`Failed to get facility. Unknown facility name: ${name}`);
2566
+ if (nullIfUnknownFacility) {
2567
+ return null;
2568
+ }
2569
+ else {
2570
+ throw new Error(`Failed to get facility. Unknown facility name: ${name}`);
2571
+ }
2566
2572
  }
2567
2573
  return await factory.createFacility(this, options);
2568
2574
  }
package/dist/server.d.ts CHANGED
@@ -31,11 +31,11 @@ export declare class RapidServer implements IRpdServer {
31
31
  getModel(options: GetModelOptions): RpdDataModel | undefined;
32
32
  getEntityManager<TEntity = any>(singularCode: string): EntityManager<TEntity>;
33
33
  registerEventHandler<K extends keyof RpdServerEventTypes>(eventName: K, listener: (...args: RpdServerEventTypes[K]) => void): this;
34
- emitEvent<K extends keyof RpdServerEventTypes>(eventName: K, sender: RapidPlugin, payload: RpdServerEventTypes[K][1]): Promise<void>;
34
+ emitEvent<K extends keyof RpdServerEventTypes>(eventName: K, payload: RpdServerEventTypes[K][1], sender?: RapidPlugin): Promise<void>;
35
35
  start(): Promise<void>;
36
36
  configureApplication(): Promise<void>;
37
37
  registerFacilityFactory(factory: FacilityFactory): void;
38
- getFacility<TFacility = any>(name: string, options?: any): Promise<TFacility>;
38
+ getFacility<TFacility = any>(name: string, options?: any, nullIfUnknownFacility?: boolean): Promise<TFacility>;
39
39
  queryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>): Promise<any[]>;
40
40
  tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>): Promise<any[]>;
41
41
  get middlewares(): any[];
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
6
7
  "keywords": [],
7
8
  "author": "Fossil",
8
9
  "license": "MIT",
@@ -38,7 +38,7 @@ export class RapidRequest {
38
38
  const requestMethod = this.method;
39
39
  if (requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH") {
40
40
  const req = this.#raw;
41
- const contentType = this.#headers.get("Content-Type");
41
+ const contentType = this.#headers.get("Content-Type") || "application/json";
42
42
  if (contentType.includes("json")) {
43
43
  this.#body = {
44
44
  type: "json",
@@ -43,8 +43,8 @@ export interface IRpdServer {
43
43
  ): this;
44
44
  emitEvent<K extends keyof RpdServerEventTypes>(
45
45
  eventName: K,
46
- sender: RapidPlugin,
47
46
  payload: RpdServerEventTypes[K][1],
47
+ sender?: RapidPlugin,
48
48
  ): void;
49
49
  handleRequest(request: Request, next: Next): Promise<Response>;
50
50
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
@@ -553,7 +553,7 @@ async function updateEntityById(
553
553
  server: IRpdServer,
554
554
  dataAccessor: IRpdDataAccessor,
555
555
  options: UpdateEntityByIdOptions,
556
- plugin: RapidPlugin
556
+ plugin?: RapidPlugin
557
557
  ) {
558
558
  const model = dataAccessor.getModel();
559
559
  const { id, entityToSave } = options;
@@ -602,7 +602,8 @@ async function updateEntityById(
602
602
  if (Object.keys(row).length) {
603
603
  updatedRow = await dataAccessor.updateById(id, row);
604
604
  }
605
- const updatedEntity = Object.assign({}, entity, updatedRow);
605
+ let updatedEntity = mapDbRowToEntity(model, updatedRow, false);
606
+ updatedEntity = Object.assign({}, entity, updatedEntity);
606
607
 
607
608
  // save many-relation properties
608
609
  for (const property of manyRelationPropertiesToUpdate) {
@@ -685,7 +686,6 @@ async function updateEntityById(
685
686
 
686
687
  server.emitEvent(
687
688
  "entity.update",
688
- plugin,
689
689
  {
690
690
  namespace: model.namespace,
691
691
  modelSingularCode: model.singularCode,
@@ -693,6 +693,7 @@ async function updateEntityById(
693
693
  after: updatedEntity,
694
694
  changes: changes,
695
695
  },
696
+ plugin,
696
697
  );
697
698
  return updatedEntity;
698
699
  }
@@ -722,24 +723,24 @@ export default class EntityManager<TEntity=any> {
722
723
  return await findById(this.#server, this.#dataAccessor, id, keepNonPropertyFields);
723
724
  }
724
725
 
725
- async createEntity(options: CreateEntityOptions, plugin: RapidPlugin): Promise<TEntity> {
726
+ async createEntity(options: CreateEntityOptions, plugin?: RapidPlugin): Promise<TEntity> {
726
727
  const model = this.getModel();
727
728
  const newEntity = await createEntity(this.#server, this.#dataAccessor, options);
728
729
 
729
730
  this.#server.emitEvent(
730
731
  "entity.create",
731
- plugin,
732
732
  {
733
733
  namespace: model.namespace,
734
734
  modelSingularCode: model.singularCode,
735
735
  after: newEntity,
736
736
  },
737
+ plugin,
737
738
  );
738
739
 
739
740
  return newEntity;
740
741
  }
741
742
 
742
- async updateEntityById(options: UpdateEntityByIdOptions, plugin: RapidPlugin): Promise<TEntity> {
743
+ async updateEntityById(options: UpdateEntityByIdOptions, plugin?: RapidPlugin): Promise<TEntity> {
743
744
  return await updateEntityById(this.#server, this.#dataAccessor, options, plugin);
744
745
  }
745
746
 
@@ -747,7 +748,7 @@ export default class EntityManager<TEntity=any> {
747
748
  return await this.#dataAccessor.count(options);
748
749
  }
749
750
 
750
- async deleteById(id: any, plugin: RapidPlugin): Promise<void> {
751
+ async deleteById(id: any, plugin?: RapidPlugin): Promise<void> {
751
752
  const model = this.getModel();
752
753
  const entity = await this.findById(id, true);
753
754
  if (!entity) {
@@ -757,16 +758,16 @@ export default class EntityManager<TEntity=any> {
757
758
  await this.#dataAccessor.deleteById(id);
758
759
  this.#server.emitEvent(
759
760
  "entity.delete",
760
- plugin,
761
761
  {
762
762
  namespace: model.namespace,
763
763
  modelSingularCode: model.singularCode,
764
764
  before: entity,
765
765
  },
766
+ plugin,
766
767
  );
767
768
  }
768
769
 
769
- async addRelations(options: AddEntityRelationsOptions, plugin: RapidPlugin): Promise<void> {
770
+ async addRelations(options: AddEntityRelationsOptions, plugin?: RapidPlugin): Promise<void> {
770
771
  const model = this.getModel();
771
772
  const {id, property, relations} = options;
772
773
  const entity = await this.findById(id);
@@ -800,7 +801,6 @@ export default class EntityManager<TEntity=any> {
800
801
 
801
802
  server.emitEvent(
802
803
  "entity.addRelations",
803
- plugin,
804
804
  {
805
805
  namespace: model.namespace,
806
806
  modelSingularCode: model.singularCode,
@@ -808,10 +808,11 @@ export default class EntityManager<TEntity=any> {
808
808
  property,
809
809
  relations,
810
810
  },
811
+ plugin,
811
812
  );
812
813
  }
813
814
 
814
- async removeRelations(options: RemoveEntityRelationsOptions, plugin: RapidPlugin): Promise<void> {
815
+ async removeRelations(options: RemoveEntityRelationsOptions, plugin?: RapidPlugin): Promise<void> {
815
816
  const model = this.getModel();
816
817
  const {id, property, relations} = options;
817
818
  const entity = await this.findById(id);
@@ -841,7 +842,6 @@ export default class EntityManager<TEntity=any> {
841
842
 
842
843
  server.emitEvent(
843
844
  "entity.removeRelations",
844
- plugin,
845
845
  {
846
846
  namespace: model.namespace,
847
847
  modelSingularCode: model.singularCode,
@@ -849,6 +849,7 @@ export default class EntityManager<TEntity=any> {
849
849
  property,
850
850
  relations,
851
851
  },
852
+ plugin,
852
853
  );
853
854
  }
854
855
  }
package/src/server.ts CHANGED
@@ -215,8 +215,8 @@ export class RapidServer implements IRpdServer {
215
215
 
216
216
  async emitEvent<K extends keyof RpdServerEventTypes>(
217
217
  eventName: K,
218
- sender: RapidPlugin,
219
218
  payload: RpdServerEventTypes[K][1],
219
+ sender?: RapidPlugin,
220
220
  ) {
221
221
  this.#logger.debug(`Emitting '${eventName}' event.`, { eventName, payload });
222
222
  await this.#eventManager.emit<K>(eventName, sender, payload as any);
@@ -272,10 +272,14 @@ export class RapidServer implements IRpdServer {
272
272
  this.#facilityFactories.set(factory.name, factory);
273
273
  }
274
274
 
275
- async getFacility<TFacility=any>(name: string, options?: any): Promise<TFacility> {
275
+ async getFacility<TFacility=any>(name: string, options?: any, nullIfUnknownFacility?: boolean): Promise<TFacility> {
276
276
  const factory = this.#facilityFactories.get(name);
277
277
  if (!factory) {
278
- throw new Error(`Failed to get facility. Unknown facility name: ${name}`);
278
+ if (nullIfUnknownFacility) {
279
+ return null;
280
+ } else {
281
+ throw new Error(`Failed to get facility. Unknown facility name: ${name}`);
282
+ }
279
283
  }
280
284
 
281
285
  return await factory.createFacility(this, options);