@ruiapp/rapid-core 0.0.1

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.
Files changed (134) hide show
  1. package/dist/bootstrapApplicationConfig.d.ts +3 -0
  2. package/dist/core/eventManager.d.ts +7 -0
  3. package/dist/core/http-types.d.ts +3 -0
  4. package/dist/core/httpHandler.d.ts +18 -0
  5. package/dist/core/plugin.d.ts +6 -0
  6. package/dist/core/pluginManager.d.ts +27 -0
  7. package/dist/core/request.d.ts +15 -0
  8. package/dist/core/response.d.ts +17 -0
  9. package/dist/core/routeContext.d.ts +17 -0
  10. package/dist/core/routesBuilder.d.ts +4 -0
  11. package/dist/core/server.d.ts +83 -0
  12. package/dist/dataAccess/dataAccessor.d.ts +20 -0
  13. package/dist/dataAccess/entityManager.d.ts +6 -0
  14. package/dist/dataAccess/entityMapper.d.ts +3 -0
  15. package/dist/dataAccess/filterHelper.d.ts +2 -0
  16. package/dist/dataAccess/propertyMapper.d.ts +3 -0
  17. package/dist/deno-std/assert/assert.d.ts +2 -0
  18. package/dist/deno-std/assert/assertion_error.d.ts +4 -0
  19. package/dist/deno-std/datetime/to_imf.d.ts +17 -0
  20. package/dist/deno-std/http/cookie.d.ts +134 -0
  21. package/dist/helpers/entityHelpers.d.ts +1 -0
  22. package/dist/helpers/inputHelper.d.ts +1 -0
  23. package/dist/helpers/runCollectionEntityHttpHandler.d.ts +5 -0
  24. package/dist/index.d.ts +7 -0
  25. package/dist/index.js +3590 -0
  26. package/dist/plugins/authManager/httpHandlers/createSession.d.ts +8 -0
  27. package/dist/plugins/authManager/httpHandlers/deleteSession.d.ts +4 -0
  28. package/dist/plugins/authManager/httpHandlers/getMyProfile.d.ts +4 -0
  29. package/dist/plugins/authManager/httpHandlers/index.d.ts +5 -0
  30. package/dist/plugins/authManager/mod.d.ts +16 -0
  31. package/dist/plugins/authManager/models/AccessToken.d.ts +3 -0
  32. package/dist/plugins/authManager/models/index.d.ts +2 -0
  33. package/dist/plugins/authManager/routes/getMyProfile.d.ts +3 -0
  34. package/dist/plugins/authManager/routes/index.d.ts +2 -0
  35. package/dist/plugins/authManager/routes/signin.d.ts +3 -0
  36. package/dist/plugins/authManager/routes/signout.d.ts +3 -0
  37. package/dist/plugins/dataManager/httpHandlers/addEntityRelations.d.ts +4 -0
  38. package/dist/plugins/dataManager/httpHandlers/countCollectionEntities.d.ts +4 -0
  39. package/dist/plugins/dataManager/httpHandlers/createCollectionEntitiesBatch.d.ts +4 -0
  40. package/dist/plugins/dataManager/httpHandlers/createCollectionEntity.d.ts +4 -0
  41. package/dist/plugins/dataManager/httpHandlers/deleteCollectionEntityById.d.ts +4 -0
  42. package/dist/plugins/dataManager/httpHandlers/findCollectionEntities.d.ts +4 -0
  43. package/dist/plugins/dataManager/httpHandlers/findCollectionEntityById.d.ts +4 -0
  44. package/dist/plugins/dataManager/httpHandlers/queryDatabase.d.ts +4 -0
  45. package/dist/plugins/dataManager/httpHandlers/removeEntityRelations.d.ts +4 -0
  46. package/dist/plugins/dataManager/httpHandlers/updateCollectionEntityById.d.ts +4 -0
  47. package/dist/plugins/dataManager/mod.d.ts +16 -0
  48. package/dist/plugins/metaManager/httpHandlers/getMetaModelDetail.d.ts +4 -0
  49. package/dist/plugins/metaManager/httpHandlers/listMetaModels.d.ts +4 -0
  50. package/dist/plugins/metaManager/mod.d.ts +15 -0
  51. package/dist/plugins/routeManager/httpHandlers/httpProxy.d.ts +4 -0
  52. package/dist/plugins/routeManager/httpHandlers/listMetaRoutes.d.ts +4 -0
  53. package/dist/plugins/routeManager/mod.d.ts +15 -0
  54. package/dist/plugins/webhooks/mod.d.ts +24 -0
  55. package/dist/plugins/webhooks/pluginConfig.d.ts +48 -0
  56. package/dist/polyfill.d.ts +1 -0
  57. package/dist/proxy/mod.d.ts +13 -0
  58. package/dist/proxy/types.d.ts +17 -0
  59. package/dist/queryBuilder/index.d.ts +1 -0
  60. package/dist/queryBuilder/queryBuilder.d.ts +34 -0
  61. package/dist/server.d.ts +31 -0
  62. package/dist/types.d.ts +327 -0
  63. package/dist/utilities/httpUtility.d.ts +1 -0
  64. package/dist/utilities/jwtUtility.d.ts +8 -0
  65. package/dist/utilities/rapidUtility.d.ts +2 -0
  66. package/dist/utilities/typeUtility.d.ts +3 -0
  67. package/package.json +29 -0
  68. package/rollup.config.js +20 -0
  69. package/src/bootstrapApplicationConfig.ts +524 -0
  70. package/src/core/eventManager.ts +21 -0
  71. package/src/core/http-types.ts +4 -0
  72. package/src/core/httpHandler.ts +29 -0
  73. package/src/core/plugin.ts +13 -0
  74. package/src/core/pluginManager.ts +143 -0
  75. package/src/core/request.ts +23 -0
  76. package/src/core/response.ts +77 -0
  77. package/src/core/routeContext.ts +38 -0
  78. package/src/core/routesBuilder.ts +86 -0
  79. package/src/core/server.ts +144 -0
  80. package/src/dataAccess/dataAccessor.ts +110 -0
  81. package/src/dataAccess/entityManager.ts +651 -0
  82. package/src/dataAccess/entityMapper.ts +74 -0
  83. package/src/dataAccess/filterHelper.ts +47 -0
  84. package/src/dataAccess/propertyMapper.ts +27 -0
  85. package/src/deno-std/assert/assert.ts +9 -0
  86. package/src/deno-std/assert/assertion_error.ts +7 -0
  87. package/src/deno-std/datetime/to_imf.ts +47 -0
  88. package/src/deno-std/http/cookie.ts +398 -0
  89. package/src/helpers/entityHelpers.ts +24 -0
  90. package/src/helpers/inputHelper.ts +11 -0
  91. package/src/helpers/runCollectionEntityHttpHandler.ts +34 -0
  92. package/src/index.ts +12 -0
  93. package/src/plugins/authManager/httpHandlers/createSession.ts +57 -0
  94. package/src/plugins/authManager/httpHandlers/deleteSession.ts +22 -0
  95. package/src/plugins/authManager/httpHandlers/getMyProfile.ts +43 -0
  96. package/src/plugins/authManager/httpHandlers/index.ts +10 -0
  97. package/src/plugins/authManager/mod.ts +56 -0
  98. package/src/plugins/authManager/models/AccessToken.ts +56 -0
  99. package/src/plugins/authManager/models/index.ts +5 -0
  100. package/src/plugins/authManager/routes/getMyProfile.ts +15 -0
  101. package/src/plugins/authManager/routes/index.ts +9 -0
  102. package/src/plugins/authManager/routes/signin.ts +15 -0
  103. package/src/plugins/authManager/routes/signout.ts +15 -0
  104. package/src/plugins/dataManager/httpHandlers/addEntityRelations.ts +76 -0
  105. package/src/plugins/dataManager/httpHandlers/countCollectionEntities.ts +22 -0
  106. package/src/plugins/dataManager/httpHandlers/createCollectionEntitiesBatch.ts +57 -0
  107. package/src/plugins/dataManager/httpHandlers/createCollectionEntity.ts +43 -0
  108. package/src/plugins/dataManager/httpHandlers/deleteCollectionEntityById.ts +38 -0
  109. package/src/plugins/dataManager/httpHandlers/findCollectionEntities.ts +35 -0
  110. package/src/plugins/dataManager/httpHandlers/findCollectionEntityById.ts +30 -0
  111. package/src/plugins/dataManager/httpHandlers/queryDatabase.ts +29 -0
  112. package/src/plugins/dataManager/httpHandlers/removeEntityRelations.ts +72 -0
  113. package/src/plugins/dataManager/httpHandlers/updateCollectionEntityById.ts +53 -0
  114. package/src/plugins/dataManager/mod.ts +150 -0
  115. package/src/plugins/metaManager/httpHandlers/getMetaModelDetail.ts +14 -0
  116. package/src/plugins/metaManager/httpHandlers/listMetaModels.ts +13 -0
  117. package/src/plugins/metaManager/mod.ts +419 -0
  118. package/src/plugins/routeManager/httpHandlers/httpProxy.ts +15 -0
  119. package/src/plugins/routeManager/httpHandlers/listMetaRoutes.ts +13 -0
  120. package/src/plugins/routeManager/mod.ts +97 -0
  121. package/src/plugins/webhooks/mod.ts +144 -0
  122. package/src/plugins/webhooks/pluginConfig.ts +74 -0
  123. package/src/polyfill.ts +5 -0
  124. package/src/proxy/mod.ts +47 -0
  125. package/src/proxy/types.ts +21 -0
  126. package/src/queryBuilder/index.ts +1 -0
  127. package/src/queryBuilder/queryBuilder.ts +424 -0
  128. package/src/server.ts +192 -0
  129. package/src/types.ts +438 -0
  130. package/src/utilities/httpUtility.ts +23 -0
  131. package/src/utilities/jwtUtility.ts +16 -0
  132. package/src/utilities/rapidUtility.ts +5 -0
  133. package/src/utilities/typeUtility.ts +11 -0
  134. package/tsconfig.json +19 -0
@@ -0,0 +1,57 @@
1
+ import { IPluginInstance } from "~/types";
2
+ import { setCookie } from "~/deno-std/http/cookie";
3
+ import { createJWT } from "~/utilities/jwtUtility";
4
+ import { HttpHandlerContext } from "~/core/httpHandler";
5
+
6
+ export interface UserAccessToken {
7
+ sub: "userAccessToken",
8
+ aud: string;
9
+ }
10
+
11
+ export const code = "createSession";
12
+
13
+ export async function handler(
14
+ plugin: IPluginInstance,
15
+ ctx: HttpHandlerContext,
16
+ options: any,
17
+ ) {
18
+ const { server, input, routerContext } = ctx;
19
+ const { response } = routerContext;
20
+ const { account, password } = input;
21
+
22
+ const userDataAccessor = server.getDataAccessor({
23
+ singularCode: "oc_user",
24
+ });
25
+
26
+ const user = await userDataAccessor.findOne({
27
+ filters: [
28
+ {
29
+ operator: "eq",
30
+ field: "login",
31
+ value: account,
32
+ }
33
+ ]
34
+ });
35
+
36
+ if (!user) {
37
+ throw new Error("Wrong account or password.");
38
+ }
39
+
40
+ const token = await createJWT({
41
+ iss: "authManager",
42
+ sub: "userAccessToken",
43
+ aud: "" + user.id,
44
+ iat: new Date,
45
+ act: user.login,
46
+ } as UserAccessToken);
47
+
48
+ setCookie(response.headers, {
49
+ name: ctx.server.config.sessionCookieName,
50
+ value: token,
51
+ path: "/",
52
+ });
53
+
54
+ ctx.output = {
55
+ token,
56
+ };
57
+ }
@@ -0,0 +1,22 @@
1
+ import { HttpHandlerContext } from "~/core/httpHandler";
2
+ import { setCookie } from "~/deno-std/http/cookie";
3
+ import { IPluginInstance } from "~/types";
4
+
5
+ export const code = "deleteSession";
6
+
7
+ export async function handler(
8
+ plugin: IPluginInstance,
9
+ ctx: HttpHandlerContext,
10
+ options: any,
11
+ ) {
12
+ const { server, input, routerContext } = ctx;
13
+ const { response } = routerContext;
14
+
15
+ setCookie(response.headers, {
16
+ name: ctx.server.config.sessionCookieName,
17
+ value: "",
18
+ path: "/",
19
+ });
20
+
21
+ routerContext.redirect("/signin");
22
+ }
@@ -0,0 +1,43 @@
1
+ import { IPluginInstance } from "~/types";
2
+ import { findEntity } from "~/dataAccess/entityManager";
3
+ import { HttpHandlerContext } from "~/core/httpHandler";
4
+
5
+ export const code = "getMyProfile";
6
+
7
+ export async function handler(
8
+ plugin: IPluginInstance,
9
+ ctx: HttpHandlerContext,
10
+ options: any,
11
+ ) {
12
+ const { server, input, routerContext } = ctx;
13
+
14
+ const userId = routerContext.state.userId;
15
+ if (!userId) {
16
+ ctx.status = 401;
17
+ ctx.output = {
18
+ error: {
19
+ message: "You are not signed in."
20
+ }
21
+ }
22
+ return;
23
+ }
24
+
25
+ const userDataAccessor = server.getDataAccessor({
26
+ singularCode: "oc_user",
27
+ });
28
+
29
+ const user = await findEntity(server, userDataAccessor, {
30
+ filters: [
31
+ {
32
+ operator: "eq",
33
+ field: "id",
34
+ value: userId,
35
+ }
36
+ ],
37
+ properties: ["id", "name", "login", "email", "department", "roles", "state", "createdAt"],
38
+ });
39
+
40
+ ctx.output = {
41
+ user,
42
+ };
43
+ }
@@ -0,0 +1,10 @@
1
+ import { IPluginHttpHandler } from "~/core/httpHandler";
2
+ import * as createSession from "./createSession";
3
+ import * as deleteSession from "./deleteSession";
4
+ import * as getMyProfile from "./getMyProfile";
5
+
6
+ export default [
7
+ createSession,
8
+ deleteSession,
9
+ getMyProfile,
10
+ ] satisfies IPluginHttpHandler[];
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Meta manager plugin
3
+ */
4
+
5
+ import * as _ from "lodash";
6
+ import {
7
+ IPluginInstance,
8
+ RpdApplicationConfig,
9
+ } from "~/types";
10
+ import { IRpdServer, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "~/core/server";
11
+
12
+ import pluginHttpHandlers from "./httpHandlers";
13
+ import pluginModels from "./models";
14
+ import pluginRoutes from "./routes";
15
+
16
+ export const code = "authManager";
17
+ export const description = "authManager";
18
+ export const extendingAbilities: RpdServerPluginExtendingAbilities[] = [];
19
+ export const configurableTargets: RpdServerPluginConfigurableTargetOptions[] = [];
20
+ export const configurations: RpdConfigurationItemOptions[] = [];
21
+
22
+ let _plugin: IPluginInstance;
23
+
24
+ export async function initPlugin(plugin: IPluginInstance, server: IRpdServer) {
25
+ _plugin = plugin;
26
+ }
27
+
28
+ export async function registerHttpHandlers(server: IRpdServer) {
29
+ for (const httpHandler of pluginHttpHandlers) {
30
+ server.registerHttpHandler(_plugin, httpHandler);
31
+ }
32
+ }
33
+
34
+ export async function registerEventHandlers(server: IRpdServer) {
35
+ }
36
+
37
+ export async function configureModels(
38
+ server: IRpdServer,
39
+ applicationConfig: RpdApplicationConfig,
40
+ ) {
41
+ applicationConfig.models.push(...pluginModels);
42
+ }
43
+
44
+ export async function configureRoutes(
45
+ server: IRpdServer,
46
+ applicationConfig: RpdApplicationConfig,
47
+ ) {
48
+ applicationConfig.routes.push(...pluginRoutes);
49
+ }
50
+
51
+ export async function onApplicationLoaded(
52
+ server: IRpdServer,
53
+ applicationConfig: RpdApplicationConfig,
54
+ ) {
55
+ console.log("authManager.onApplicationLoaded");
56
+ }
@@ -0,0 +1,56 @@
1
+ import { RpdDataModel } from "~/types";
2
+
3
+ export default {
4
+ maintainedBy: "authManager",
5
+ namespace: "auth",
6
+ name: "access_token",
7
+ singularCode: "access_token",
8
+ pluralCode: "access_tokens",
9
+ schema: "public",
10
+ tableName: "access_tokens",
11
+ properties: [
12
+ {
13
+ name: "id",
14
+ code: "id",
15
+ columnName: "id",
16
+ type: "integer",
17
+ required: true,
18
+ autoIncrement: true,
19
+ },
20
+ {
21
+ name: "token",
22
+ code: "token",
23
+ columnName: "token",
24
+ type: "text",
25
+ required: true,
26
+ },
27
+ {
28
+ name: "description",
29
+ code: "description",
30
+ columnName: "description",
31
+ type: "text",
32
+ required: false,
33
+ },
34
+ {
35
+ name: "ownerType",
36
+ code: "ownerType",
37
+ columnName: "owner_type",
38
+ type: "text",
39
+ required: true,
40
+ },
41
+ {
42
+ name: "ownerId",
43
+ code: "ownerId",
44
+ columnName: "owner_id",
45
+ type: "integer",
46
+ required: true,
47
+ },
48
+ {
49
+ name: "expirationTime",
50
+ code: "expirationTime",
51
+ columnName: "expiration_time",
52
+ type: "datetime",
53
+ required: true,
54
+ },
55
+ ],
56
+ } as RpdDataModel;
@@ -0,0 +1,5 @@
1
+ import AccessToken from "./AccessToken";
2
+
3
+ export default [
4
+ AccessToken,
5
+ ]
@@ -0,0 +1,15 @@
1
+ import { RpdRoute } from "~/types";
2
+
3
+ export default {
4
+ namespace: "auth",
5
+ name: "auth.getMyProfile",
6
+ code: "",
7
+ type: "RESTful",
8
+ method: "get",
9
+ endpoint: "/api/me",
10
+ handlers: [
11
+ {
12
+ code: "getMyProfile",
13
+ },
14
+ ],
15
+ } as RpdRoute;
@@ -0,0 +1,9 @@
1
+ import getMyProfile from "./getMyProfile";
2
+ import signin from "./signin";
3
+ import signout from "./signout";
4
+
5
+ export default [
6
+ getMyProfile,
7
+ signin,
8
+ signout,
9
+ ]
@@ -0,0 +1,15 @@
1
+ import { RpdRoute } from "~/types";
2
+
3
+ export default {
4
+ namespace: "auth",
5
+ name: "auth.signin",
6
+ code: "",
7
+ type: "RESTful",
8
+ method: "post",
9
+ endpoint: "/api/signin",
10
+ handlers: [
11
+ {
12
+ code: "createSession",
13
+ },
14
+ ],
15
+ } as RpdRoute;
@@ -0,0 +1,15 @@
1
+ import { RpdRoute } from "~/types";
2
+
3
+ export default {
4
+ namespace: "auth",
5
+ name: "auth.signout",
6
+ code: "",
7
+ type: "RESTful",
8
+ method: "get",
9
+ endpoint: "/api/signout",
10
+ handlers: [
11
+ {
12
+ code: "deleteSession",
13
+ },
14
+ ],
15
+ } as RpdRoute;
@@ -0,0 +1,76 @@
1
+ import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
+ import { mergeInput } from "~/helpers/inputHelper";
3
+ import { isRelationProperty } from "~/utilities/rapidUtility";
4
+ import { HttpHandlerContext } from "~/core/httpHandler";
5
+
6
+ export const code = "addEntityRelations";
7
+
8
+ interface AddEntityRelationsInput {
9
+ id: number;
10
+ property: string;
11
+ relations: {id?: number, [k: string]: any}[];
12
+ }
13
+
14
+ export async function handler(
15
+ plugin: IPluginInstance,
16
+ ctx: HttpHandlerContext,
17
+ options: RunEntityHttpHandlerOptions,
18
+ ) {
19
+ const { server, input } = ctx;
20
+ const { queryBuilder } = server;
21
+ const { defaultInput, fixedInput } = options;
22
+
23
+ console.debug(`Running ${code} handler...`);
24
+
25
+ console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
26
+ const mergedInput = mergeInput(defaultInput, input, fixedInput);
27
+ console.debug(`fixedInput: ${JSON.stringify(fixedInput)}`);
28
+ console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
29
+
30
+ const dataAccessor = server.getDataAccessor(options);
31
+ const model = dataAccessor.getModel();
32
+
33
+ const {id, property, relations} = mergedInput as AddEntityRelationsInput;
34
+ const row = await dataAccessor.findById(id);
35
+ if (!row) {
36
+ throw new Error(`${options.namespace}.${options.singularCode} with id "${id}" was not found.`);
37
+ }
38
+
39
+ console.log(mergedInput);
40
+
41
+ const relationProperty = model.properties.find(e => e.code === property);
42
+ if (!relationProperty) {
43
+ throw new Error(`Property '${property}' was not found in ${options.namespace}.${options.singularCode}`);
44
+ }
45
+
46
+ if (!(isRelationProperty(relationProperty) && relationProperty.relation === "many")) {
47
+ throw new Error(`Operation 'createEntityRelations' is only supported on property of 'many' relation`);
48
+ }
49
+
50
+ if (relationProperty.linkTableName) {
51
+ for (const relation of relations) {
52
+ const command = `INSERT INTO ${queryBuilder.quoteTable({schema:relationProperty.linkSchema, tableName: relationProperty.linkTableName})} (${queryBuilder.quoteObject(relationProperty.selfIdColumnName!)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName!)})
53
+ SELECT $1, $2 WHERE NOT EXISTS (
54
+ SELECT ${queryBuilder.quoteObject(relationProperty.selfIdColumnName!)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName!)}
55
+ FROM ${queryBuilder.quoteTable({schema:relationProperty.linkSchema, tableName: relationProperty.linkTableName})}
56
+ WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName!)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName!)}=$2
57
+ )`;
58
+ const params = [id, relation.id];
59
+ await server.queryDatabaseObject(command, params);
60
+ }
61
+ }
62
+
63
+ ctx.output = {};
64
+
65
+ server.emitEvent(
66
+ "entity.addRelations",
67
+ plugin,
68
+ {
69
+ namespace: options.namespace,
70
+ modelSingularCode: options.singularCode,
71
+ entity: row,
72
+ property,
73
+ relations: relations,
74
+ },
75
+ );
76
+ }
@@ -0,0 +1,22 @@
1
+ import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
+ import runCollectionEntityHttpHandler from "~/helpers/runCollectionEntityHttpHandler";
3
+ import { removeFiltersWithNullValue } from "~/dataAccess/filterHelper";
4
+ import { HttpHandlerContext } from "~/core/httpHandler";
5
+
6
+ export const code = "countCollectionEntities";
7
+
8
+ export async function handler(
9
+ plugin: IPluginInstance,
10
+ ctx: HttpHandlerContext,
11
+ options: RunEntityHttpHandlerOptions,
12
+ ) {
13
+ await runCollectionEntityHttpHandler(
14
+ ctx,
15
+ options,
16
+ code,
17
+ (dataAccessor, input) => {
18
+ input.filters = removeFiltersWithNullValue(input.filters);
19
+ return dataAccessor.count(input);
20
+ },
21
+ );
22
+ }
@@ -0,0 +1,57 @@
1
+ import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
+ import { mergeInput } from "~/helpers/inputHelper";
3
+ import { createEntity } from "~/dataAccess/entityManager";
4
+ import { isArray } from "lodash";
5
+ import { HttpHandlerContext } from "~/core/httpHandler";
6
+
7
+ export const code = "createCollectionEntitiesBatch";
8
+
9
+ export async function handler(
10
+ plugin: IPluginInstance,
11
+ ctx: HttpHandlerContext,
12
+ options: RunEntityHttpHandlerOptions,
13
+ ) {
14
+ const { server, input } = ctx;
15
+ const { defaultInput, fixedInput } = options;
16
+
17
+ console.debug(`Running ${code} handler...`);
18
+
19
+ const { entities } = input;
20
+ if (!isArray(entities)) {
21
+ throw new Error("input.entities should be an array.");
22
+ }
23
+
24
+ console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
25
+ console.debug(`fixedInput: ${JSON.stringify(fixedInput)}`);
26
+
27
+ const output: any[] = [];
28
+ for(const entity of entities) {
29
+ const mergedEntity = mergeInput(defaultInput?.entity || {}, entity, fixedInput?.entity);
30
+ console.debug(`mergedEntity: ${JSON.stringify(mergedEntity)}`);
31
+
32
+ const userId = ctx.routerContext.state?.userId;
33
+ if (userId) {
34
+ mergedEntity.createdBy = userId;
35
+ }
36
+
37
+ const dataAccessor = server.getDataAccessor(options);
38
+ const newEntity = await createEntity(server, dataAccessor, {
39
+ entity: mergedEntity,
40
+ });
41
+
42
+ server.emitEvent(
43
+ "entity.create",
44
+ plugin,
45
+ {
46
+ namespace: options.namespace,
47
+ modelSingularCode: options.singularCode,
48
+ after: newEntity,
49
+ },
50
+ );
51
+
52
+ output.push(newEntity);
53
+ }
54
+
55
+ ctx.output = output;
56
+
57
+ }
@@ -0,0 +1,43 @@
1
+ import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
+ import { mergeInput } from "~/helpers/inputHelper";
3
+ import { createEntity } from "~/dataAccess/entityManager";
4
+ import { HttpHandlerContext } from "~/core/httpHandler";
5
+
6
+ export const code = "createCollectionEntity";
7
+
8
+ export async function handler(
9
+ plugin: IPluginInstance,
10
+ ctx: HttpHandlerContext,
11
+ options: RunEntityHttpHandlerOptions,
12
+ ) {
13
+ const { server, input } = ctx;
14
+ const { defaultInput, fixedInput } = options;
15
+
16
+ console.debug(`Running ${code} handler...`);
17
+
18
+ console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
19
+ const mergedInput = mergeInput(defaultInput, input, fixedInput);
20
+ console.debug(`fixedInput: ${JSON.stringify(fixedInput)}`);
21
+ console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
22
+
23
+ const userId = ctx.routerContext.state?.userId;
24
+ if (userId) {
25
+ input.createdBy = userId;
26
+ }
27
+
28
+ const dataAccessor = server.getDataAccessor(options);
29
+ const output = await createEntity(server, dataAccessor, {
30
+ entity: input,
31
+ });
32
+ ctx.output = output;
33
+
34
+ server.emitEvent(
35
+ "entity.create",
36
+ plugin,
37
+ {
38
+ namespace: options.namespace,
39
+ modelSingularCode: options.singularCode,
40
+ after: output,
41
+ },
42
+ );
43
+ }
@@ -0,0 +1,38 @@
1
+ import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
+ import { mapDbRowToEntity } from "~/dataAccess/entityMapper";
3
+ import { HttpHandlerContext } from "~/core/httpHandler";
4
+
5
+ export const code = "deleteCollectionEntityById";
6
+
7
+ export async function handler(
8
+ plugin: IPluginInstance,
9
+ ctx: HttpHandlerContext,
10
+ options: RunEntityHttpHandlerOptions,
11
+ ) {
12
+ console.debug(`Running ${code} handler...`);
13
+ const { server, input } = ctx;
14
+
15
+ const dataAccessor = server.getDataAccessor(options);
16
+ const id = input.id;
17
+ const row = await dataAccessor.findById(id);
18
+ if (!row) {
19
+ ctx.routerContext.status = 200;
20
+ return;
21
+ }
22
+
23
+ await dataAccessor.deleteById(id);
24
+
25
+ const entity = mapDbRowToEntity(dataAccessor.getModel(), row);
26
+
27
+ server.emitEvent(
28
+ "entity.delete",
29
+ plugin,
30
+ {
31
+ namespace: options.namespace,
32
+ modelSingularCode: options.singularCode,
33
+ before: entity,
34
+ },
35
+ );
36
+
37
+ ctx.routerContext.status = 200;
38
+ }
@@ -0,0 +1,35 @@
1
+ import * as _ from "lodash";
2
+ import { FindEntityOptions, IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
3
+ import runCollectionEntityHttpHandler from "~/helpers/runCollectionEntityHttpHandler";
4
+ import { findEntities } from "~/dataAccess/entityManager";
5
+ import { removeFiltersWithNullValue } from "~/dataAccess/filterHelper";
6
+ import { HttpHandlerContext } from "~/core/httpHandler";
7
+
8
+ export const code = "findCollectionEntities";
9
+
10
+ export async function handler(
11
+ plugin: IPluginInstance,
12
+ ctx: HttpHandlerContext,
13
+ options: RunEntityHttpHandlerOptions,
14
+ ) {
15
+ await runCollectionEntityHttpHandler(
16
+ ctx,
17
+ options,
18
+ code,
19
+ async (dataAccessor, input: FindEntityOptions) => {
20
+ input.filters = removeFiltersWithNullValue(input.filters);
21
+ const entities = await findEntities(ctx.server, dataAccessor, input);
22
+ const result: {
23
+ list: any;
24
+ total?: any;
25
+ } = { list: entities };
26
+
27
+ if (input.pagination && !input.pagination.withoutTotal) {
28
+ // TOOD: impl count entities by using entity manager, because DataAccessor does not support 'exists' and 'notExists' filter.
29
+ const countResult = await dataAccessor.count(input);
30
+ result.total = countResult.count;
31
+ }
32
+ return result;
33
+ },
34
+ );
35
+ }
@@ -0,0 +1,30 @@
1
+ import { IPluginInstance, RunEntityHttpHandlerOptions } from "~/types";
2
+ import { findEntity } from "~/dataAccess/entityManager";
3
+ import { HttpHandlerContext } from "~/core/httpHandler";
4
+
5
+ export const code = "findCollectionEntityById";
6
+
7
+ export async function handler(
8
+ plugin: IPluginInstance,
9
+ ctx: HttpHandlerContext,
10
+ options: RunEntityHttpHandlerOptions,
11
+ ) {
12
+ console.debug(`Running ${code} handler...`);
13
+ const { server, input } = ctx;
14
+ const { id } = input;
15
+
16
+ const dataAccessor = server.getDataAccessor(options);
17
+ const user = await findEntity(ctx.server, dataAccessor, {
18
+ filters: [
19
+ {
20
+ operator: "eq",
21
+ field: "id",
22
+ value: id,
23
+ }
24
+ ],
25
+ });
26
+ if (!user) {
27
+ throw new Error(`${options.namespace}.${options.singularCode} with id "${id}" was not found.`);
28
+ }
29
+ ctx.output = user;
30
+ }
@@ -0,0 +1,29 @@
1
+ import * as _ from "lodash";
2
+ import { IPluginInstance, RunQueryDatabaseHandlerOptions } from "~/types";
3
+ import { mergeInput } from "~/helpers/inputHelper";
4
+ import { HttpHandlerContext } from "~/core/httpHandler";
5
+
6
+ export const code = "queryDatabase";
7
+
8
+ export async function handler(
9
+ plugin: IPluginInstance,
10
+ ctx: HttpHandlerContext,
11
+ options: RunQueryDatabaseHandlerOptions,
12
+ ) {
13
+ const { server, input } = ctx;
14
+ const { sql, querySingle, defaultInput, fixedInput } = options;
15
+
16
+ console.debug(`Running ${code} handler...`);
17
+
18
+ console.debug(`defaultInput: ${JSON.stringify(defaultInput)}`);
19
+ const mergedInput = mergeInput(defaultInput, input, fixedInput);
20
+ console.debug(`fixedInput: ${JSON.stringify(fixedInput)}`);
21
+ console.debug(`mergedInput: ${JSON.stringify(mergedInput)}`);
22
+
23
+ const result = await server.queryDatabaseObject(sql, mergedInput);
24
+ if (querySingle) {
25
+ ctx.output = _.first(result);
26
+ } else {
27
+ ctx.output = result;
28
+ }
29
+ }