@ruiapp/rapid-core 0.11.0 → 0.11.2
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/core/actionHandlers/ifActionHandler.d.ts +11 -0
- package/dist/index.js +36 -11
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
- package/src/core/actionHandlers/ifActionHandler.ts +27 -0
- package/src/dataAccess/entityManager.ts +15 -4
- package/src/dataAccess/entityMapper.ts +2 -2
- package/src/server.ts +8 -4
- package/src/types.ts +5 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ActionHandlerContext, IPluginActionHandler } from "../../core/actionHandler";
|
|
2
|
+
import { RpdRouteActionConfig } from "../../types";
|
|
3
|
+
export declare const code = "if";
|
|
4
|
+
export type IfActionHandlerConfig = {
|
|
5
|
+
condition: string;
|
|
6
|
+
then: RpdRouteActionConfig[];
|
|
7
|
+
otherwise: RpdRouteActionConfig[];
|
|
8
|
+
};
|
|
9
|
+
export declare function handler(_plugin: any, ctx: ActionHandlerContext, options: IfActionHandlerConfig): Promise<void>;
|
|
10
|
+
declare const _default: IPluginActionHandler;
|
|
11
|
+
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -2676,7 +2676,7 @@ function mapEntityToDbRow(server, model, entity) {
|
|
|
2676
2676
|
return result;
|
|
2677
2677
|
}
|
|
2678
2678
|
const propertyNames = Object.keys(entity);
|
|
2679
|
-
|
|
2679
|
+
for (const propertyName of propertyNames) {
|
|
2680
2680
|
let columnName = propertyName;
|
|
2681
2681
|
const property = getEntityPropertyByCode(server, model, propertyName);
|
|
2682
2682
|
if (property) {
|
|
@@ -2701,7 +2701,7 @@ function mapEntityToDbRow(server, model, entity) {
|
|
|
2701
2701
|
}
|
|
2702
2702
|
}
|
|
2703
2703
|
}
|
|
2704
|
-
}
|
|
2704
|
+
}
|
|
2705
2705
|
return result;
|
|
2706
2706
|
}
|
|
2707
2707
|
|
|
@@ -4411,13 +4411,13 @@ class EntityManager {
|
|
|
4411
4411
|
async addRelations(options, plugin) {
|
|
4412
4412
|
const server = this.#server;
|
|
4413
4413
|
const model = this.getModel();
|
|
4414
|
-
const { id, property, relations, routeContext } = options;
|
|
4414
|
+
const { id: selfId, property, relations, routeContext } = options;
|
|
4415
4415
|
const entity = await this.findById({
|
|
4416
|
-
id,
|
|
4416
|
+
id: selfId,
|
|
4417
4417
|
routeContext,
|
|
4418
4418
|
});
|
|
4419
4419
|
if (!entity) {
|
|
4420
|
-
throw new Error(`${model.namespace}.${model.singularCode} with id "${
|
|
4420
|
+
throw new Error(`${model.namespace}.${model.singularCode} with id "${selfId}" was not found.`);
|
|
4421
4421
|
}
|
|
4422
4422
|
const relationProperty = getEntityPropertyByCode(server, model, property);
|
|
4423
4423
|
if (!relationProperty) {
|
|
@@ -4438,10 +4438,21 @@ class EntityManager {
|
|
|
4438
4438
|
FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
|
|
4439
4439
|
WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2
|
|
4440
4440
|
)`;
|
|
4441
|
-
const
|
|
4441
|
+
const targetId = relation[relationProperty.targetIdColumnName] || relation.id;
|
|
4442
|
+
const params = [selfId, targetId];
|
|
4442
4443
|
await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
|
|
4443
4444
|
}
|
|
4444
4445
|
}
|
|
4446
|
+
else {
|
|
4447
|
+
const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
|
|
4448
|
+
for (const relation of relations) {
|
|
4449
|
+
relation[relationProperty.selfIdColumnName] = selfId;
|
|
4450
|
+
await targetEntityManager.createEntity({
|
|
4451
|
+
routeContext,
|
|
4452
|
+
entity: relation,
|
|
4453
|
+
});
|
|
4454
|
+
}
|
|
4455
|
+
}
|
|
4445
4456
|
await server.emitEvent({
|
|
4446
4457
|
eventName: "entity.addRelations",
|
|
4447
4458
|
payload: {
|
|
@@ -4504,9 +4515,9 @@ var healthz = {
|
|
|
4504
4515
|
type: "RESTful",
|
|
4505
4516
|
method: "GET",
|
|
4506
4517
|
endpoint: "/healthz",
|
|
4507
|
-
handler: handler$
|
|
4518
|
+
handler: handler$z,
|
|
4508
4519
|
};
|
|
4509
|
-
async function handler$
|
|
4520
|
+
async function handler$z(ctx) {
|
|
4510
4521
|
ctx.output = {};
|
|
4511
4522
|
}
|
|
4512
4523
|
|
|
@@ -4549,6 +4560,19 @@ function interpreteConfigExpressions(config, vars) {
|
|
|
4549
4560
|
}
|
|
4550
4561
|
}
|
|
4551
4562
|
|
|
4563
|
+
const code$y = "if";
|
|
4564
|
+
async function handler$y(_plugin, ctx, options) {
|
|
4565
|
+
const { server, input, routerContext: routeContext } = ctx;
|
|
4566
|
+
const { condition, then, otherwise } = options;
|
|
4567
|
+
const conditionResult = interpreteExpression(condition, ctx.vars);
|
|
4568
|
+
const actions = conditionResult ? then : otherwise;
|
|
4569
|
+
if (!actions || !actions.length) {
|
|
4570
|
+
return;
|
|
4571
|
+
}
|
|
4572
|
+
await server.runActionHandlers(ctx, actions);
|
|
4573
|
+
}
|
|
4574
|
+
var ifActionHandler = { code: code$y, handler: handler$y };
|
|
4575
|
+
|
|
4552
4576
|
class RapidServer {
|
|
4553
4577
|
#logger;
|
|
4554
4578
|
#facilityFactories;
|
|
@@ -4785,6 +4809,8 @@ class RapidServer {
|
|
|
4785
4809
|
}
|
|
4786
4810
|
async start() {
|
|
4787
4811
|
this.#logger.info("Starting rapid server...");
|
|
4812
|
+
// register core action handlers
|
|
4813
|
+
this.registerActionHandler(null, ifActionHandler);
|
|
4788
4814
|
const pluginManager = this.#pluginManager;
|
|
4789
4815
|
await pluginManager.loadPlugins(this.#plugins);
|
|
4790
4816
|
await pluginManager.initPlugins();
|
|
@@ -4916,7 +4942,7 @@ class RapidServer {
|
|
|
4916
4942
|
return response.getResponse();
|
|
4917
4943
|
}
|
|
4918
4944
|
async runActionHandlers(handlerContext, actions) {
|
|
4919
|
-
if (!actions) {
|
|
4945
|
+
if (!actions || !actions.length) {
|
|
4920
4946
|
return;
|
|
4921
4947
|
}
|
|
4922
4948
|
for (const action of actions) {
|
|
@@ -4927,8 +4953,7 @@ class RapidServer {
|
|
|
4927
4953
|
throw new Error("Unknown handler: " + actionCode);
|
|
4928
4954
|
}
|
|
4929
4955
|
await this.beforeRunActionHandler(handlerContext, action);
|
|
4930
|
-
|
|
4931
|
-
handlerContext.results.push(result);
|
|
4956
|
+
await handler(handlerContext, action.config);
|
|
4932
4957
|
}
|
|
4933
4958
|
}
|
|
4934
4959
|
async beforeRunRouteActions(handlerContext) {
|
package/dist/types.d.ts
CHANGED
|
@@ -666,8 +666,8 @@ export interface RemoveEntityRelationsOptions {
|
|
|
666
666
|
[k: string]: any;
|
|
667
667
|
}[];
|
|
668
668
|
}
|
|
669
|
-
export type EntityWatcherType = EntityWatcher<"entity.create"> | EntityWatcher<"entity.update"> | EntityWatcher<"entity.delete"> | EntityWatcher<"entity.addRelations"> | EntityWatcher<"entity.removeRelations"> | EntityWatcher<any>;
|
|
670
|
-
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes
|
|
669
|
+
export type EntityWatcherType = EntityWatcher<"entity.beforeCreate"> | EntityWatcher<"entity.create"> | EntityWatcher<"entity.beforeUpdate"> | EntityWatcher<"entity.update"> | EntityWatcher<"entity.beforeDelete"> | EntityWatcher<"entity.delete"> | EntityWatcher<"entity.addRelations"> | EntityWatcher<"entity.removeRelations"> | EntityWatcher<"entity.beforeResponse"> | EntityWatcher<any>;
|
|
670
|
+
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes> {
|
|
671
671
|
eventName: TEventName;
|
|
672
672
|
modelSingularCode: string;
|
|
673
673
|
handler: EntityWatchHandler<TEventName>;
|
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ActionHandler, ActionHandlerContext, IPluginActionHandler } from "~/core/actionHandler";
|
|
2
|
+
import { generatePasswordHash, validatePassword } from "~/utilities/passwordUtility";
|
|
3
|
+
import { interpreteConfigExpressions, interpreteExpression } from "../ExpressionInterpreter";
|
|
4
|
+
import { RpdRouteActionConfig } from "~/types";
|
|
5
|
+
|
|
6
|
+
export const code = "if";
|
|
7
|
+
|
|
8
|
+
export type IfActionHandlerConfig = {
|
|
9
|
+
condition: string;
|
|
10
|
+
then: RpdRouteActionConfig[];
|
|
11
|
+
otherwise: RpdRouteActionConfig[];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export async function handler(_plugin: any, ctx: ActionHandlerContext, options: IfActionHandlerConfig) {
|
|
15
|
+
const { server, input, routerContext: routeContext } = ctx;
|
|
16
|
+
const { response } = routeContext;
|
|
17
|
+
const { condition, then, otherwise } = options;
|
|
18
|
+
|
|
19
|
+
const conditionResult = interpreteExpression(condition, ctx.vars);
|
|
20
|
+
const actions: RpdRouteActionConfig[] = conditionResult ? then : otherwise;
|
|
21
|
+
if (!actions || !actions.length) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
await server.runActionHandlers(ctx, actions);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default { code, handler } as IPluginActionHandler;
|
|
@@ -1871,13 +1871,13 @@ export default class EntityManager<TEntity = any> {
|
|
|
1871
1871
|
async addRelations(options: AddEntityRelationsOptions, plugin?: RapidPlugin): Promise<void> {
|
|
1872
1872
|
const server = this.#server;
|
|
1873
1873
|
const model = this.getModel();
|
|
1874
|
-
const { id, property, relations, routeContext } = options;
|
|
1874
|
+
const { id: selfId, property, relations, routeContext } = options;
|
|
1875
1875
|
const entity = await this.findById({
|
|
1876
|
-
id,
|
|
1876
|
+
id: selfId,
|
|
1877
1877
|
routeContext,
|
|
1878
1878
|
});
|
|
1879
1879
|
if (!entity) {
|
|
1880
|
-
throw new Error(`${model.namespace}.${model.singularCode} with id "${
|
|
1880
|
+
throw new Error(`${model.namespace}.${model.singularCode} with id "${selfId}" was not found.`);
|
|
1881
1881
|
}
|
|
1882
1882
|
|
|
1883
1883
|
const relationProperty = getEntityPropertyByCode(server, model, property);
|
|
@@ -1901,9 +1901,20 @@ export default class EntityManager<TEntity = any> {
|
|
|
1901
1901
|
FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
|
|
1902
1902
|
WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName!)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName!)}=$2
|
|
1903
1903
|
)`;
|
|
1904
|
-
|
|
1904
|
+
|
|
1905
|
+
const targetId = relation[relationProperty.targetIdColumnName!] || relation.id;
|
|
1906
|
+
const params = [selfId, targetId];
|
|
1905
1907
|
await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
|
|
1906
1908
|
}
|
|
1909
|
+
} else {
|
|
1910
|
+
const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
|
|
1911
|
+
for (const relation of relations) {
|
|
1912
|
+
relation[relationProperty.selfIdColumnName!] = selfId;
|
|
1913
|
+
await targetEntityManager.createEntity({
|
|
1914
|
+
routeContext,
|
|
1915
|
+
entity: relation,
|
|
1916
|
+
});
|
|
1917
|
+
}
|
|
1907
1918
|
}
|
|
1908
1919
|
|
|
1909
1920
|
await server.emitEvent({
|
|
@@ -83,7 +83,7 @@ export function mapEntityToDbRow(server: IRpdServer, model: RpdDataModel, entity
|
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
const propertyNames = Object.keys(entity);
|
|
86
|
-
|
|
86
|
+
for (const propertyName of propertyNames) {
|
|
87
87
|
let columnName = propertyName;
|
|
88
88
|
const property = getEntityPropertyByCode(server, model, propertyName);
|
|
89
89
|
if (property) {
|
|
@@ -105,7 +105,7 @@ export function mapEntityToDbRow(server: IRpdServer, model: RpdDataModel, entity
|
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
-
}
|
|
108
|
+
}
|
|
109
109
|
|
|
110
110
|
return result;
|
|
111
111
|
}
|
package/src/server.ts
CHANGED
|
@@ -38,6 +38,7 @@ import { FacilityFactory } from "./core/facility";
|
|
|
38
38
|
import { CronJobConfiguration } from "./types/cron-job-types";
|
|
39
39
|
import coreRoutes from "./core/routes";
|
|
40
40
|
import { interpreteConfigExpressions } from "./core/ExpressionInterpreter";
|
|
41
|
+
import ifActionHandler from "./core/actionHandlers/ifActionHandler";
|
|
41
42
|
|
|
42
43
|
export interface InitServerOptions {
|
|
43
44
|
logger: Logger;
|
|
@@ -336,6 +337,10 @@ export class RapidServer implements IRpdServer {
|
|
|
336
337
|
|
|
337
338
|
async start() {
|
|
338
339
|
this.#logger.info("Starting rapid server...");
|
|
340
|
+
|
|
341
|
+
// register core action handlers
|
|
342
|
+
this.registerActionHandler(null, ifActionHandler);
|
|
343
|
+
|
|
339
344
|
const pluginManager = this.#pluginManager;
|
|
340
345
|
await pluginManager.loadPlugins(this.#plugins);
|
|
341
346
|
|
|
@@ -477,9 +482,10 @@ export class RapidServer implements IRpdServer {
|
|
|
477
482
|
}
|
|
478
483
|
|
|
479
484
|
async runActionHandlers(handlerContext: ActionHandlerContext, actions: RpdRouteActionConfig[]) {
|
|
480
|
-
if (!actions) {
|
|
485
|
+
if (!actions || !actions.length) {
|
|
481
486
|
return;
|
|
482
487
|
}
|
|
488
|
+
|
|
483
489
|
for (const action of actions) {
|
|
484
490
|
const actionCode = action.code;
|
|
485
491
|
interpreteConfigExpressions(action.config, handlerContext.vars);
|
|
@@ -490,9 +496,7 @@ export class RapidServer implements IRpdServer {
|
|
|
490
496
|
}
|
|
491
497
|
|
|
492
498
|
await this.beforeRunActionHandler(handlerContext, action);
|
|
493
|
-
|
|
494
|
-
const result = await handler(handlerContext, action.config);
|
|
495
|
-
handlerContext.results.push(result);
|
|
499
|
+
await handler(handlerContext, action.config);
|
|
496
500
|
}
|
|
497
501
|
}
|
|
498
502
|
|
package/src/types.ts
CHANGED
|
@@ -844,14 +844,18 @@ export interface RemoveEntityRelationsOptions {
|
|
|
844
844
|
}
|
|
845
845
|
|
|
846
846
|
export type EntityWatcherType =
|
|
847
|
+
| EntityWatcher<"entity.beforeCreate">
|
|
847
848
|
| EntityWatcher<"entity.create">
|
|
849
|
+
| EntityWatcher<"entity.beforeUpdate">
|
|
848
850
|
| EntityWatcher<"entity.update">
|
|
851
|
+
| EntityWatcher<"entity.beforeDelete">
|
|
849
852
|
| EntityWatcher<"entity.delete">
|
|
850
853
|
| EntityWatcher<"entity.addRelations">
|
|
851
854
|
| EntityWatcher<"entity.removeRelations">
|
|
855
|
+
| EntityWatcher<"entity.beforeResponse">
|
|
852
856
|
| EntityWatcher<any>;
|
|
853
857
|
|
|
854
|
-
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes
|
|
858
|
+
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes> {
|
|
855
859
|
eventName: TEventName;
|
|
856
860
|
modelSingularCode: string;
|
|
857
861
|
handler: EntityWatchHandler<TEventName>;
|