@ruiapp/rapid-core 0.1.12 → 0.1.13
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/eventManager.d.ts +1 -2
- package/dist/dataAccess/entityManager.d.ts +8 -6
- package/dist/index.d.ts +4 -0
- package/dist/index.js +402 -233
- package/dist/plugins/entityWatch/EntityWatchPlugin.d.ts +15 -0
- package/dist/plugins/entityWatch/EntityWatchPluginTypes.d.ts +15 -0
- package/dist/plugins/serverOperation/ServerOperationPlugin.d.ts +25 -0
- package/dist/plugins/serverOperation/ServerOperationPluginTypes.d.ts +11 -0
- package/dist/plugins/serverOperation/actionHandlers/index.d.ts +3 -0
- package/dist/plugins/serverOperation/actionHandlers/runServerOperation.d.ts +6 -0
- package/dist/types.d.ts +17 -2
- package/package.json +1 -1
- package/src/core/eventManager.ts +3 -4
- package/src/dataAccess/entityManager.ts +153 -10
- package/src/index.ts +5 -1
- package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +2 -50
- package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +1 -12
- package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +1 -11
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +1 -20
- package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +2 -48
- package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +1 -25
- package/src/plugins/entityWatch/EntityWatchPlugin.ts +96 -0
- package/src/plugins/entityWatch/EntityWatchPluginTypes.ts +19 -0
- package/src/plugins/serverOperation/ServerOperationPlugin.ts +94 -0
- package/src/plugins/serverOperation/ServerOperationPluginTypes.ts +13 -0
- package/src/plugins/serverOperation/actionHandlers/index.ts +6 -0
- package/src/plugins/serverOperation/actionHandlers/runServerOperation.ts +15 -0
- package/src/server.ts +1 -1
- package/src/types.ts +13 -2
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { RpdEntityCreateEventPayload, RpdServerEventTypes } from "~/types";
|
|
2
|
+
|
|
3
|
+
import { IRpdServer, RapidPlugin, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "~/core/server";
|
|
4
|
+
import { EntityWatchHandlerContext, EntityWatchPluginInitOptions, EntityWatcher } from "./EntityWatchPluginTypes";
|
|
5
|
+
import EventManager from "~/core/eventManager";
|
|
6
|
+
|
|
7
|
+
class EntityWatchPlugin implements RapidPlugin {
|
|
8
|
+
#createEventEmitters: EventManager<Record<string, [EntityWatchHandlerContext<any>]>>
|
|
9
|
+
#updateEventEmitters: EventManager<Record<string, [EntityWatchHandlerContext<any>]>>
|
|
10
|
+
#deleteEventEmitters: EventManager<Record<string, [EntityWatchHandlerContext<any>]>>
|
|
11
|
+
#addRelationsEventEmitters: EventManager<Record<string, [EntityWatchHandlerContext<any>]>>
|
|
12
|
+
#removeRelationsEventEmitters: EventManager<Record<string, [EntityWatchHandlerContext<any>]>>
|
|
13
|
+
|
|
14
|
+
constructor(options: EntityWatchPluginInitOptions) {
|
|
15
|
+
const { watchers } = options;
|
|
16
|
+
|
|
17
|
+
this.#createEventEmitters = new EventManager();
|
|
18
|
+
this.#updateEventEmitters = new EventManager();
|
|
19
|
+
this.#deleteEventEmitters = new EventManager();
|
|
20
|
+
this.#addRelationsEventEmitters = new EventManager();
|
|
21
|
+
this.#removeRelationsEventEmitters = new EventManager();
|
|
22
|
+
|
|
23
|
+
for (const watcher of watchers) {
|
|
24
|
+
if (watcher.eventName === "entity.create") {
|
|
25
|
+
this.#createEventEmitters.on(watcher.modelSingularCode, watcher.handler);
|
|
26
|
+
} else if (watcher.eventName === "entity.update") {
|
|
27
|
+
this.#updateEventEmitters.on(watcher.modelSingularCode, watcher.handler);
|
|
28
|
+
} else if (watcher.eventName === "entity.delete") {
|
|
29
|
+
this.#deleteEventEmitters.on(watcher.modelSingularCode, watcher.handler);
|
|
30
|
+
} else if (watcher.eventName === "entity.addRelations") {
|
|
31
|
+
this.#addRelationsEventEmitters.on(watcher.modelSingularCode, watcher.handler);
|
|
32
|
+
} else if (watcher.eventName === "entity.removeRelations") {
|
|
33
|
+
this.#removeRelationsEventEmitters.on(watcher.modelSingularCode, watcher.handler);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get code(): string {
|
|
39
|
+
return "entityWatch";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get description(): string {
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get configurations(): RpdConfigurationItemOptions[] {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async registerEventHandlers(server: IRpdServer): Promise<any> {
|
|
59
|
+
server.registerEventHandler("entity.create", this.handleEntityEvent.bind(this, server, "entity.create"));
|
|
60
|
+
server.registerEventHandler("entity.update", this.handleEntityEvent.bind(this, server, "entity.update"));
|
|
61
|
+
server.registerEventHandler("entity.delete", this.handleEntityEvent.bind(this, server, "entity.delete"));
|
|
62
|
+
server.registerEventHandler("entity.addRelations", this.handleEntityEvent.bind(this, server, "entity.addRelations"));
|
|
63
|
+
server.registerEventHandler("entity.removeRelations", this.handleEntityEvent.bind(this, server, "entity.removeRelations"));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
handleEntityEvent(
|
|
67
|
+
server: IRpdServer,
|
|
68
|
+
eventName: keyof RpdServerEventTypes,
|
|
69
|
+
sender: RapidPlugin,
|
|
70
|
+
payload: RpdEntityCreateEventPayload
|
|
71
|
+
) {
|
|
72
|
+
if (sender === this) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const { modelSingularCode } = payload;
|
|
77
|
+
const entityWatchHandlerContext: EntityWatchHandlerContext<typeof eventName> = {
|
|
78
|
+
server,
|
|
79
|
+
payload,
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
if (eventName === "entity.create") {
|
|
83
|
+
this.#createEventEmitters.emit(modelSingularCode, entityWatchHandlerContext);
|
|
84
|
+
} else if (eventName === "entity.update") {
|
|
85
|
+
this.#updateEventEmitters.emit(modelSingularCode, entityWatchHandlerContext);
|
|
86
|
+
} else if (eventName === "entity.delete") {
|
|
87
|
+
this.#deleteEventEmitters.emit(modelSingularCode, entityWatchHandlerContext);
|
|
88
|
+
} else if (eventName === "entity.addRelations") {
|
|
89
|
+
this.#addRelationsEventEmitters.emit(modelSingularCode, entityWatchHandlerContext);
|
|
90
|
+
} else if (eventName === "entity.removeRelations") {
|
|
91
|
+
this.#removeRelationsEventEmitters.emit(modelSingularCode, entityWatchHandlerContext);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export default EntityWatchPlugin;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IRpdServer } from "~/core/server";
|
|
2
|
+
import { RpdServerEventTypes } from "~/types";
|
|
3
|
+
|
|
4
|
+
export interface EntityWatcher<TEventName extends keyof RpdServerEventTypes> {
|
|
5
|
+
eventName: TEventName;
|
|
6
|
+
modelSingularCode: string;
|
|
7
|
+
handler: EntityWatchHandler<TEventName>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type EntityWatchHandler<TEventName extends keyof RpdServerEventTypes> = (ctx: EntityWatchHandlerContext<TEventName>) => Promise<void>;
|
|
11
|
+
|
|
12
|
+
export type EntityWatchHandlerContext<TEventName extends keyof RpdServerEventTypes> = {
|
|
13
|
+
server: IRpdServer;
|
|
14
|
+
payload: RpdServerEventTypes[TEventName][1];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface EntityWatchPluginInitOptions {
|
|
18
|
+
watchers: EntityWatcher<keyof RpdServerEventTypes>[];
|
|
19
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { RpdApplicationConfig, RpdRoute } from "~/types";
|
|
2
|
+
|
|
3
|
+
import pluginActionHandlers from "./actionHandlers";
|
|
4
|
+
import { ServerOperation, ServerOperationPluginInitOptions } from "./ServerOperationPluginTypes";
|
|
5
|
+
import { IRpdServer, RapidPlugin, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "~/core/server";
|
|
6
|
+
|
|
7
|
+
class ServerOperationPlugin implements RapidPlugin {
|
|
8
|
+
#operations: ServerOperation[];
|
|
9
|
+
|
|
10
|
+
constructor(options: ServerOperationPluginInitOptions) {
|
|
11
|
+
this.#operations = options.operations || [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get code(): string {
|
|
15
|
+
return "serverOperation";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get description(): string {
|
|
19
|
+
return "";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
get configurations(): RpdConfigurationItemOptions[] {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async initPlugin(server: IRpdServer): Promise<any> {
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async registerMiddlewares(server: IRpdServer): Promise<any> {
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async registerActionHandlers(server: IRpdServer): Promise<any> {
|
|
41
|
+
for (const actionHandler of pluginActionHandlers) {
|
|
42
|
+
server.registerActionHandler(this, actionHandler);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async registerEventHandlers(server: IRpdServer): Promise<any> {
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async registerMessageHandlers(server: IRpdServer): Promise<any> {
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async registerTaskProcessors(server: IRpdServer): Promise<any> {
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
65
|
+
const routes: RpdRoute[] = [];
|
|
66
|
+
for (const operation of this.#operations) {
|
|
67
|
+
routes.push({
|
|
68
|
+
namespace: "app",
|
|
69
|
+
name: `app.operations.${operation.code}`,
|
|
70
|
+
code: `app.operations.${operation.code}`,
|
|
71
|
+
type: "RESTful",
|
|
72
|
+
method: operation.method,
|
|
73
|
+
endpoint: `/app/${operation.code}`,
|
|
74
|
+
actions: [
|
|
75
|
+
{
|
|
76
|
+
code: "runServerOperation",
|
|
77
|
+
config: {
|
|
78
|
+
operation: operation.handler,
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
server.appendApplicationConfig({ routes });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export default ServerOperationPlugin;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
2
|
+
import { RpdHttpMethod } from "~/types";
|
|
3
|
+
|
|
4
|
+
export interface ServerOperation {
|
|
5
|
+
code: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
method: RpdHttpMethod;
|
|
8
|
+
handler: (ctx: ActionHandlerContext) => Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ServerOperationPluginInitOptions {
|
|
12
|
+
operations: ServerOperation[];
|
|
13
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
2
|
+
import { RapidPlugin } from "~/core/server";
|
|
3
|
+
|
|
4
|
+
export const code = "runServerOperation";
|
|
5
|
+
|
|
6
|
+
export async function handler(
|
|
7
|
+
plugin: RapidPlugin,
|
|
8
|
+
ctx: ActionHandlerContext,
|
|
9
|
+
options: {
|
|
10
|
+
operation: (ctx: ActionHandlerContext) => Promise<void>;
|
|
11
|
+
},
|
|
12
|
+
) {
|
|
13
|
+
const { operation } = options;
|
|
14
|
+
await operation(ctx);
|
|
15
|
+
}
|
package/src/server.ts
CHANGED
|
@@ -35,7 +35,7 @@ export interface InitServerOptions {
|
|
|
35
35
|
export class RapidServer implements IRpdServer {
|
|
36
36
|
#pluginManager: PluginManager;
|
|
37
37
|
#plugins: RapidPlugin[];
|
|
38
|
-
#eventManager: EventManager
|
|
38
|
+
#eventManager: EventManager<RpdServerEventTypes>;
|
|
39
39
|
#middlewares: any[];
|
|
40
40
|
#bootstrapApplicationConfig: RpdApplicationConfig;
|
|
41
41
|
#applicationConfig: RpdApplicationConfig;
|
package/src/types.ts
CHANGED
|
@@ -431,10 +431,21 @@ export interface UpdateEntityOptions {
|
|
|
431
431
|
|
|
432
432
|
export interface UpdateEntityByIdOptions {
|
|
433
433
|
id: any;
|
|
434
|
-
|
|
435
|
-
changes: any;
|
|
434
|
+
entityToSave: any;
|
|
436
435
|
}
|
|
437
436
|
|
|
438
437
|
export interface DeleteEntityOptions {
|
|
439
438
|
filters?: EntityFilterOptions[];
|
|
440
439
|
}
|
|
440
|
+
|
|
441
|
+
export interface AddEntityRelationsOptions {
|
|
442
|
+
id: number;
|
|
443
|
+
property: string;
|
|
444
|
+
relations: {id?: number, [k: string]: any}[];
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export interface RemoveEntityRelationsOptions {
|
|
448
|
+
id: number;
|
|
449
|
+
property: string;
|
|
450
|
+
relations: {id?: number, [k: string]: any}[];
|
|
451
|
+
}
|