@ruiapp/rapid-core 0.3.1 → 0.3.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.
Files changed (101) hide show
  1. package/CHANGELOG.md +7 -7
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.js +356 -134
  4. package/dist/plugins/license/LicensePlugin.d.ts +23 -0
  5. package/dist/plugins/license/LicensePluginTypes.d.ts +78 -0
  6. package/dist/plugins/license/LicenseService.d.ts +22 -0
  7. package/dist/plugins/license/actionHandlers/getLicense.d.ts +6 -0
  8. package/dist/plugins/license/actionHandlers/index.d.ts +3 -0
  9. package/dist/plugins/license/helpers/certHelper.d.ts +2 -0
  10. package/dist/plugins/license/helpers/cryptoHelper.d.ts +8 -0
  11. package/dist/plugins/license/models/index.d.ts +2 -0
  12. package/dist/plugins/license/routes/getLicense.d.ts +12 -0
  13. package/dist/plugins/license/routes/index.d.ts +12 -0
  14. package/dist/utilities/typeUtility.d.ts +1 -0
  15. package/package.json +1 -1
  16. package/src/bootstrapApplicationConfig.ts +631 -631
  17. package/src/core/response.ts +76 -76
  18. package/src/core/routeContext.ts +47 -47
  19. package/src/core/server.ts +142 -142
  20. package/src/dataAccess/columnTypeMapper.ts +22 -22
  21. package/src/dataAccess/dataAccessTypes.ts +163 -163
  22. package/src/dataAccess/dataAccessor.ts +135 -135
  23. package/src/dataAccess/entityManager.ts +1718 -1718
  24. package/src/dataAccess/entityMapper.ts +100 -100
  25. package/src/dataAccess/propertyMapper.ts +28 -28
  26. package/src/deno-std/http/cookie.ts +372 -372
  27. package/src/helpers/entityHelpers.ts +76 -76
  28. package/src/helpers/filterHelper.ts +148 -148
  29. package/src/helpers/metaHelper.ts +89 -89
  30. package/src/helpers/runCollectionEntityActionHandler.ts +27 -27
  31. package/src/index.ts +57 -54
  32. package/src/plugins/auth/AuthPlugin.ts +85 -85
  33. package/src/plugins/auth/actionHandlers/changePassword.ts +54 -54
  34. package/src/plugins/auth/actionHandlers/createSession.ts +75 -63
  35. package/src/plugins/auth/actionHandlers/resetPassword.ts +38 -38
  36. package/src/plugins/cronJob/CronJobPlugin.ts +112 -112
  37. package/src/plugins/dataManage/DataManagePlugin.ts +163 -163
  38. package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +20 -20
  39. package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +16 -16
  40. package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +42 -42
  41. package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +24 -24
  42. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +38 -38
  43. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +22 -22
  44. package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +26 -26
  45. package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +21 -21
  46. package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +20 -20
  47. package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +41 -41
  48. package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
  49. package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
  50. package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
  51. package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
  52. package/src/plugins/license/LicensePlugin.ts +79 -0
  53. package/src/plugins/license/LicensePluginTypes.ts +95 -0
  54. package/src/plugins/license/LicenseService.ts +112 -0
  55. package/src/plugins/license/actionHandlers/getLicense.ts +18 -0
  56. package/src/plugins/license/actionHandlers/index.ts +4 -0
  57. package/src/plugins/license/helpers/certHelper.ts +21 -0
  58. package/src/plugins/license/helpers/cryptoHelper.ts +47 -0
  59. package/src/plugins/license/models/index.ts +1 -0
  60. package/src/plugins/license/routes/getLicense.ts +15 -0
  61. package/src/plugins/license/routes/index.ts +3 -0
  62. package/src/plugins/mail/MailPlugin.ts +74 -74
  63. package/src/plugins/mail/MailPluginTypes.ts +27 -27
  64. package/src/plugins/mail/MailService.ts +38 -38
  65. package/src/plugins/mail/actionHandlers/index.ts +3 -3
  66. package/src/plugins/mail/models/index.ts +1 -1
  67. package/src/plugins/mail/routes/index.ts +1 -1
  68. package/src/plugins/metaManage/MetaManagePlugin.ts +504 -504
  69. package/src/plugins/notification/NotificationPlugin.ts +68 -68
  70. package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
  71. package/src/plugins/notification/NotificationService.ts +25 -25
  72. package/src/plugins/notification/actionHandlers/index.ts +3 -3
  73. package/src/plugins/notification/models/Notification.ts +60 -60
  74. package/src/plugins/notification/models/index.ts +3 -3
  75. package/src/plugins/notification/routes/index.ts +1 -1
  76. package/src/plugins/routeManage/RouteManagePlugin.ts +62 -62
  77. package/src/plugins/sequence/SequencePlugin.ts +136 -136
  78. package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
  79. package/src/plugins/sequence/SequenceService.ts +81 -81
  80. package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
  81. package/src/plugins/sequence/segments/autoIncrement.ts +78 -78
  82. package/src/plugins/sequence/segments/dayOfMonth.ts +17 -17
  83. package/src/plugins/sequence/segments/literal.ts +14 -14
  84. package/src/plugins/sequence/segments/month.ts +17 -17
  85. package/src/plugins/sequence/segments/parameter.ts +18 -18
  86. package/src/plugins/sequence/segments/year.ts +17 -17
  87. package/src/plugins/setting/SettingPlugin.ts +68 -68
  88. package/src/plugins/setting/SettingPluginTypes.ts +37 -37
  89. package/src/plugins/setting/models/SystemSettingItem.ts +48 -42
  90. package/src/plugins/setting/models/UserSettingItem.ts +55 -49
  91. package/src/plugins/stateMachine/StateMachinePlugin.ts +186 -186
  92. package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
  93. package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +51 -51
  94. package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
  95. package/src/plugins/webhooks/pluginConfig.ts +75 -75
  96. package/src/queryBuilder/queryBuilder.ts +665 -665
  97. package/src/server.ts +463 -463
  98. package/src/types.ts +701 -701
  99. package/src/utilities/errorUtility.ts +15 -15
  100. package/src/utilities/pathUtility.ts +14 -14
  101. package/src/utilities/typeUtility.ts +15 -11
@@ -1,49 +1,55 @@
1
- import { RpdDataModel } from "~/types";
2
-
3
- export default {
4
- maintainedBy: "settingPlugin",
5
- namespace: "svc",
6
- name: "user_setting_item",
7
- singularCode: "user_setting_item",
8
- pluralCode: "user_setting_items",
9
- schema: "public",
10
- tableName: "user_setting_items",
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: "ownerId",
22
- code: "ownerId",
23
- columnName: "owner_id",
24
- type: "integer",
25
- required: true,
26
- },
27
- {
28
- name: "groupCode",
29
- code: "groupCode",
30
- columnName: "group_code",
31
- type: "text",
32
- required: true,
33
- },
34
- {
35
- name: "itemCode",
36
- code: "itemCode",
37
- columnName: "item_code",
38
- type: "text",
39
- required: true,
40
- },
41
- {
42
- name: "value",
43
- code: "value",
44
- columnName: "value",
45
- type: "json",
46
- required: false,
47
- },
48
- ],
49
- } as RpdDataModel;
1
+ import { RpdDataModel } from "~/types";
2
+
3
+ export default {
4
+ maintainedBy: "settingPlugin",
5
+ namespace: "svc",
6
+ name: "user_setting_item",
7
+ singularCode: "user_setting_item",
8
+ pluralCode: "user_setting_items",
9
+ schema: "public",
10
+ tableName: "user_setting_items",
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: "ownerId",
22
+ code: "ownerId",
23
+ columnName: "owner_id",
24
+ type: "integer",
25
+ required: true,
26
+ },
27
+ {
28
+ name: "groupCode",
29
+ code: "groupCode",
30
+ columnName: "group_code",
31
+ type: "text",
32
+ required: true,
33
+ },
34
+ {
35
+ name: "itemCode",
36
+ code: "itemCode",
37
+ columnName: "item_code",
38
+ type: "text",
39
+ required: true,
40
+ },
41
+ {
42
+ name: "value",
43
+ code: "value",
44
+ columnName: "value",
45
+ type: "json",
46
+ required: false,
47
+ },
48
+ ],
49
+ indexes: [
50
+ {
51
+ unique: true,
52
+ properties: ["ownerId", "groupCode", "itemCode"],
53
+ },
54
+ ],
55
+ } as RpdDataModel;
@@ -1,186 +1,186 @@
1
- /**
2
- * State machine plugin
3
- */
4
-
5
- import { CreateEntityOptions, RpdApplicationConfig, RpdDataModel, RpdDataModelProperty, UpdateEntityByIdOptions } from "~/types";
6
- import {
7
- IRpdServer,
8
- RapidPlugin,
9
- RpdConfigurationItemOptions,
10
- RpdServerPluginConfigurableTargetOptions,
11
- RpdServerPluginExtendingAbilities,
12
- } from "~/core/server";
13
-
14
- import pluginActionHandlers from "./actionHandlers";
15
- import pluginModels from "./models";
16
- import pluginRoutes from "./routes";
17
- import { filter, find, first, get, isEqual } from "lodash";
18
- import { PropertyStateMachineConfig } from "./StateMachinePluginTypes";
19
- import { isNullOrUndefined } from "~/utilities/typeUtility";
20
- import { getStateMachineNextSnapshot } from "./stateMachineHelper";
21
- import { getEntityPropertiesIncludingBase } from "~/helpers/metaHelper";
22
-
23
- class StateMachinePlugin implements RapidPlugin {
24
- get code(): string {
25
- return "stateMachinePlugin";
26
- }
27
-
28
- get description(): string {
29
- return null;
30
- }
31
-
32
- get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
33
- return [];
34
- }
35
-
36
- get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
37
- return [];
38
- }
39
-
40
- get configurations(): RpdConfigurationItemOptions[] {
41
- return [];
42
- }
43
-
44
- async registerActionHandlers(server: IRpdServer): Promise<any> {
45
- for (const actionHandler of pluginActionHandlers) {
46
- server.registerActionHandler(this, actionHandler);
47
- }
48
- }
49
-
50
- async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
51
- server.appendApplicationConfig({ models: pluginModels });
52
- }
53
-
54
- async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
55
- server.appendApplicationConfig({ routes: pluginRoutes });
56
- }
57
-
58
- async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig) {
59
- const models = server.getApplicationConfig().models;
60
- for (const model of models) {
61
- for (const property of getEntityPropertiesIncludingBase(server, model)) {
62
- const propertyStateMachineConfig: PropertyStateMachineConfig = property.config?.stateMachine;
63
- if (propertyStateMachineConfig) {
64
- const stateMachineCode = getStateMachineCode(model, property);
65
- const stateMachineConfig = propertyStateMachineConfig.config;
66
-
67
- const stateMachineDataAccessor = server.getDataAccessor({
68
- singularCode: "state_machine",
69
- });
70
- const stateMachine = await stateMachineDataAccessor.findOne({
71
- filters: [
72
- {
73
- operator: "eq",
74
- field: "code",
75
- value: stateMachineCode,
76
- },
77
- ],
78
- });
79
-
80
- if (stateMachine) {
81
- if (!isEqual(stateMachine.config, stateMachineConfig)) {
82
- await stateMachineDataAccessor.updateById(stateMachine.id, {
83
- config: stateMachineConfig,
84
- });
85
- }
86
- } else {
87
- await stateMachineDataAccessor.create({
88
- code: stateMachineCode,
89
- config: stateMachineConfig,
90
- });
91
- }
92
- }
93
- }
94
- }
95
- }
96
-
97
- /**
98
- * 创建实体前的处理。
99
- * 当属性启用了状态机管理,如创建实体时没有指定该属性的状态值,则应将该属性设置为 stateMachine.config.initial 。
100
- * @param server
101
- * @param model
102
- * @param options
103
- */
104
- async beforeCreateEntity(server: IRpdServer, model: RpdDataModel, options: CreateEntityOptions) {
105
- for (const property of getEntityPropertiesIncludingBase(server, model)) {
106
- const isStateMachineEnabled = get(property.config, "stateMachine.enabled", false);
107
- if (isStateMachineEnabled && isNullOrUndefined(options.entity[property.code])) {
108
- const initialState = get(property.config, "stateMachine.config.initial", null);
109
- if (initialState) {
110
- options.entity[property.code] = initialState;
111
- }
112
- }
113
- }
114
- }
115
-
116
- /**
117
- * 更新实体前的处理。
118
- * 1. 对所有启用了状态机管理,且 transferControl 为 true 的属性,应禁止直接更新这些属性
119
- * 2. 当更新实体时指定了operation,则查找启用了状态机管理的属性。
120
- * 如果一个模型中存在多个属性启用了状态机管理,则以 options.stateProperty 中指定的为准。
121
- * 对于该状态属性应用 options.operation,使其转换到下一状态。
122
- * @param server
123
- * @param model
124
- * @param options
125
- */
126
- async beforeUpdateEntity(server: IRpdServer, model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any) {
127
- const entity = options.entityToSave;
128
-
129
- const stateMachineEnabledProperties: RpdDataModelProperty[] = [];
130
- for (const property of getEntityPropertiesIncludingBase(server, model)) {
131
- const isStateMachineEnabled = get(property.config, "stateMachine.enabled", false);
132
-
133
- if (isStateMachineEnabled) {
134
- stateMachineEnabledProperties.push(property);
135
-
136
- const isTransferControlEnabled = get(property.config, "stateMachine.transferControl", false);
137
- if (isTransferControlEnabled && !isNullOrUndefined(entity[property.code])) {
138
- throw new Error(`You're not allowed to change '${property.code}' property directly when transfer control is enabled, do an operation instead.`);
139
- }
140
- }
141
- }
142
-
143
- if (!options.operation) {
144
- return;
145
- }
146
-
147
- let statePropertiesToUpdate: RpdDataModelProperty[];
148
- const statePropertyCodes = options.stateProperties;
149
- if (statePropertyCodes && statePropertyCodes.length) {
150
- statePropertiesToUpdate = filter(stateMachineEnabledProperties, (property) => statePropertyCodes.includes(property.code));
151
- } else {
152
- statePropertiesToUpdate = stateMachineEnabledProperties;
153
- }
154
-
155
- if (!statePropertiesToUpdate.length) {
156
- throw new Error(`State machine property not found.`);
157
- }
158
-
159
- for (const statePropertyToUpdate of statePropertiesToUpdate) {
160
- const machineConfig = get(statePropertyToUpdate.config, "stateMachine.config", null);
161
- if (!machineConfig) {
162
- throw new Error(`State machine of property '${statePropertyToUpdate.code}' not configured.`);
163
- }
164
- machineConfig.id = getStateMachineCode(model, statePropertyToUpdate);
165
-
166
- const nextSnapshot = await getStateMachineNextSnapshot({
167
- machineConfig,
168
- context: {},
169
- currentState: currentEntity[statePropertyToUpdate.code],
170
- event: options.operation,
171
- });
172
-
173
- entity[statePropertyToUpdate.code] = nextSnapshot.value;
174
- }
175
- }
176
- }
177
-
178
- function getStateMachineCode(model: RpdDataModel, property: RpdDataModelProperty) {
179
- if (property.isBaseProperty) {
180
- return `propertyStateMachine.${model.namespace}.${model.base}.${property.code}`;
181
- } else {
182
- return `propertyStateMachine.${model.namespace}.${model.singularCode}.${property.code}`;
183
- }
184
- }
185
-
186
- export default StateMachinePlugin;
1
+ /**
2
+ * State machine plugin
3
+ */
4
+
5
+ import { CreateEntityOptions, RpdApplicationConfig, RpdDataModel, RpdDataModelProperty, UpdateEntityByIdOptions } from "~/types";
6
+ import {
7
+ IRpdServer,
8
+ RapidPlugin,
9
+ RpdConfigurationItemOptions,
10
+ RpdServerPluginConfigurableTargetOptions,
11
+ RpdServerPluginExtendingAbilities,
12
+ } from "~/core/server";
13
+
14
+ import pluginActionHandlers from "./actionHandlers";
15
+ import pluginModels from "./models";
16
+ import pluginRoutes from "./routes";
17
+ import { filter, find, first, get, isEqual } from "lodash";
18
+ import { PropertyStateMachineConfig } from "./StateMachinePluginTypes";
19
+ import { isNullOrUndefined } from "~/utilities/typeUtility";
20
+ import { getStateMachineNextSnapshot } from "./stateMachineHelper";
21
+ import { getEntityPropertiesIncludingBase } from "~/helpers/metaHelper";
22
+
23
+ class StateMachinePlugin implements RapidPlugin {
24
+ get code(): string {
25
+ return "stateMachinePlugin";
26
+ }
27
+
28
+ get description(): string {
29
+ return null;
30
+ }
31
+
32
+ get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
33
+ return [];
34
+ }
35
+
36
+ get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
37
+ return [];
38
+ }
39
+
40
+ get configurations(): RpdConfigurationItemOptions[] {
41
+ return [];
42
+ }
43
+
44
+ async registerActionHandlers(server: IRpdServer): Promise<any> {
45
+ for (const actionHandler of pluginActionHandlers) {
46
+ server.registerActionHandler(this, actionHandler);
47
+ }
48
+ }
49
+
50
+ async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
51
+ server.appendApplicationConfig({ models: pluginModels });
52
+ }
53
+
54
+ async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
55
+ server.appendApplicationConfig({ routes: pluginRoutes });
56
+ }
57
+
58
+ async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig) {
59
+ const models = server.getApplicationConfig().models;
60
+ for (const model of models) {
61
+ for (const property of getEntityPropertiesIncludingBase(server, model)) {
62
+ const propertyStateMachineConfig: PropertyStateMachineConfig = property.config?.stateMachine;
63
+ if (propertyStateMachineConfig) {
64
+ const stateMachineCode = getStateMachineCode(model, property);
65
+ const stateMachineConfig = propertyStateMachineConfig.config;
66
+
67
+ const stateMachineDataAccessor = server.getDataAccessor({
68
+ singularCode: "state_machine",
69
+ });
70
+ const stateMachine = await stateMachineDataAccessor.findOne({
71
+ filters: [
72
+ {
73
+ operator: "eq",
74
+ field: "code",
75
+ value: stateMachineCode,
76
+ },
77
+ ],
78
+ });
79
+
80
+ if (stateMachine) {
81
+ if (!isEqual(stateMachine.config, stateMachineConfig)) {
82
+ await stateMachineDataAccessor.updateById(stateMachine.id, {
83
+ config: stateMachineConfig,
84
+ });
85
+ }
86
+ } else {
87
+ await stateMachineDataAccessor.create({
88
+ code: stateMachineCode,
89
+ config: stateMachineConfig,
90
+ });
91
+ }
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ /**
98
+ * 创建实体前的处理。
99
+ * 当属性启用了状态机管理,如创建实体时没有指定该属性的状态值,则应将该属性设置为 stateMachine.config.initial 。
100
+ * @param server
101
+ * @param model
102
+ * @param options
103
+ */
104
+ async beforeCreateEntity(server: IRpdServer, model: RpdDataModel, options: CreateEntityOptions) {
105
+ for (const property of getEntityPropertiesIncludingBase(server, model)) {
106
+ const isStateMachineEnabled = get(property.config, "stateMachine.enabled", false);
107
+ if (isStateMachineEnabled && isNullOrUndefined(options.entity[property.code])) {
108
+ const initialState = get(property.config, "stateMachine.config.initial", null);
109
+ if (initialState) {
110
+ options.entity[property.code] = initialState;
111
+ }
112
+ }
113
+ }
114
+ }
115
+
116
+ /**
117
+ * 更新实体前的处理。
118
+ * 1. 对所有启用了状态机管理,且 transferControl 为 true 的属性,应禁止直接更新这些属性
119
+ * 2. 当更新实体时指定了operation,则查找启用了状态机管理的属性。
120
+ * 如果一个模型中存在多个属性启用了状态机管理,则以 options.stateProperty 中指定的为准。
121
+ * 对于该状态属性应用 options.operation,使其转换到下一状态。
122
+ * @param server
123
+ * @param model
124
+ * @param options
125
+ */
126
+ async beforeUpdateEntity(server: IRpdServer, model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any) {
127
+ const entity = options.entityToSave;
128
+
129
+ const stateMachineEnabledProperties: RpdDataModelProperty[] = [];
130
+ for (const property of getEntityPropertiesIncludingBase(server, model)) {
131
+ const isStateMachineEnabled = get(property.config, "stateMachine.enabled", false);
132
+
133
+ if (isStateMachineEnabled) {
134
+ stateMachineEnabledProperties.push(property);
135
+
136
+ const isTransferControlEnabled = get(property.config, "stateMachine.transferControl", false);
137
+ if (isTransferControlEnabled && !isNullOrUndefined(entity[property.code])) {
138
+ throw new Error(`You're not allowed to change '${property.code}' property directly when transfer control is enabled, do an operation instead.`);
139
+ }
140
+ }
141
+ }
142
+
143
+ if (!options.operation) {
144
+ return;
145
+ }
146
+
147
+ let statePropertiesToUpdate: RpdDataModelProperty[];
148
+ const statePropertyCodes = options.stateProperties;
149
+ if (statePropertyCodes && statePropertyCodes.length) {
150
+ statePropertiesToUpdate = filter(stateMachineEnabledProperties, (property) => statePropertyCodes.includes(property.code));
151
+ } else {
152
+ statePropertiesToUpdate = stateMachineEnabledProperties;
153
+ }
154
+
155
+ if (!statePropertiesToUpdate.length) {
156
+ throw new Error(`State machine property not found.`);
157
+ }
158
+
159
+ for (const statePropertyToUpdate of statePropertiesToUpdate) {
160
+ const machineConfig = get(statePropertyToUpdate.config, "stateMachine.config", null);
161
+ if (!machineConfig) {
162
+ throw new Error(`State machine of property '${statePropertyToUpdate.code}' not configured.`);
163
+ }
164
+ machineConfig.id = getStateMachineCode(model, statePropertyToUpdate);
165
+
166
+ const nextSnapshot = await getStateMachineNextSnapshot({
167
+ machineConfig,
168
+ context: {},
169
+ currentState: currentEntity[statePropertyToUpdate.code],
170
+ event: options.operation,
171
+ });
172
+
173
+ entity[statePropertyToUpdate.code] = nextSnapshot.value;
174
+ }
175
+ }
176
+ }
177
+
178
+ function getStateMachineCode(model: RpdDataModel, property: RpdDataModelProperty) {
179
+ if (property.isBaseProperty) {
180
+ return `propertyStateMachine.${model.namespace}.${model.base}.${property.code}`;
181
+ } else {
182
+ return `propertyStateMachine.${model.namespace}.${model.singularCode}.${property.code}`;
183
+ }
184
+ }
185
+
186
+ export default StateMachinePlugin;
@@ -1,48 +1,48 @@
1
- import { AnyActorRef, AnyEventObject, EventObject, MachineConfig, MachineContext, MachineSnapshot, MetaObject, StateValue } from "xstate";
2
-
3
- export type PropertyStateMachineConfig = {
4
- enabled: boolean;
5
- config: MachineConfig<any, any>;
6
- transferControl?: boolean;
7
- };
8
-
9
- export type SendStateMachineEventOptions = {
10
- code: string;
11
- };
12
-
13
- export type StateMachineEvent = AnyEventObject;
14
-
15
- export type SendStateMachineEventInput = {
16
- code?: string;
17
- context: any;
18
- currentState: string;
19
- event: StateMachineEvent;
20
- };
21
-
22
- export type GetStateMachineNextSnapshotOptions = {
23
- machineConfig: MachineConfig<any, any>;
24
- context: any;
25
- currentState: string;
26
- event: StateMachineEvent;
27
- };
28
-
29
- export type DefaultStateMachineSnapshot = MachineSnapshot<
30
- MachineContext,
31
- EventObject,
32
- Record<string, AnyActorRef | undefined>,
33
- StateValue,
34
- string,
35
- unknown,
36
- MetaObject
37
- >;
38
-
39
- export type TryGetStateMachineNextSnapshotResult = TryGetStateMachineNextSnapshotPositiveResult | TryGetStateMachineNextSnapshotNegativeResult;
40
-
41
- export type TryGetStateMachineNextSnapshotPositiveResult = {
42
- canTransfer: true;
43
- nextSnapshot: DefaultStateMachineSnapshot;
44
- };
45
-
46
- export type TryGetStateMachineNextSnapshotNegativeResult = {
47
- canTransfer: false;
48
- };
1
+ import { AnyActorRef, AnyEventObject, EventObject, MachineConfig, MachineContext, MachineSnapshot, MetaObject, StateValue } from "xstate";
2
+
3
+ export type PropertyStateMachineConfig = {
4
+ enabled: boolean;
5
+ config: MachineConfig<any, any>;
6
+ transferControl?: boolean;
7
+ };
8
+
9
+ export type SendStateMachineEventOptions = {
10
+ code: string;
11
+ };
12
+
13
+ export type StateMachineEvent = AnyEventObject;
14
+
15
+ export type SendStateMachineEventInput = {
16
+ code?: string;
17
+ context: any;
18
+ currentState: string;
19
+ event: StateMachineEvent;
20
+ };
21
+
22
+ export type GetStateMachineNextSnapshotOptions = {
23
+ machineConfig: MachineConfig<any, any>;
24
+ context: any;
25
+ currentState: string;
26
+ event: StateMachineEvent;
27
+ };
28
+
29
+ export type DefaultStateMachineSnapshot = MachineSnapshot<
30
+ MachineContext,
31
+ EventObject,
32
+ Record<string, AnyActorRef | undefined>,
33
+ StateValue,
34
+ string,
35
+ unknown,
36
+ MetaObject
37
+ >;
38
+
39
+ export type TryGetStateMachineNextSnapshotResult = TryGetStateMachineNextSnapshotPositiveResult | TryGetStateMachineNextSnapshotNegativeResult;
40
+
41
+ export type TryGetStateMachineNextSnapshotPositiveResult = {
42
+ canTransfer: true;
43
+ nextSnapshot: DefaultStateMachineSnapshot;
44
+ };
45
+
46
+ export type TryGetStateMachineNextSnapshotNegativeResult = {
47
+ canTransfer: false;
48
+ };