midway-fatcms 0.0.1-beta.14 → 0.0.1-beta.16

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 (85) hide show
  1. package/README.md +7 -0
  2. package/dist/configuration.d.ts +1 -0
  3. package/dist/configuration.js +21 -8
  4. package/dist/controller/base/BaseApiController.d.ts +1 -1
  5. package/dist/controller/gateway/AsyncTaskController.js +15 -4
  6. package/dist/controller/gateway/CrudMtdGatewayController.d.ts +3 -3
  7. package/dist/controller/gateway/CrudMtdGatewayController.js +5 -5
  8. package/dist/index.d.ts +3 -0
  9. package/dist/index.js +3 -0
  10. package/dist/libs/crud-pro/CrudPro.d.ts +1 -0
  11. package/dist/libs/crud-pro/CrudPro.js +5 -0
  12. package/dist/libs/crud-pro/defaultConfigs.js +2 -0
  13. package/dist/libs/crud-pro/interfaces.d.ts +6 -0
  14. package/dist/libs/crud-pro/models/ExecuteContext.d.ts +2 -1
  15. package/dist/libs/crud-pro/models/ExecuteContextFunc.d.ts +8 -1
  16. package/dist/libs/crud-pro/models/ExecuteContextFunc.js +8 -0
  17. package/dist/libs/crud-pro/models/ResModel.d.ts +16 -0
  18. package/dist/libs/crud-pro/models/ResModel.js +2 -0
  19. package/dist/libs/crud-pro/services/CrudProCachedCfgService.d.ts +1 -0
  20. package/dist/libs/crud-pro/services/CrudProCachedCfgService.js +22 -11
  21. package/dist/libs/crud-pro/services/CrudProServiceBase.d.ts +2 -6
  22. package/dist/libs/crud-pro/services/CrudProServiceBase.js +3 -2
  23. package/dist/libs/crud-pro/services/CrudProTableMetaService.d.ts +1 -0
  24. package/dist/libs/crud-pro/services/CrudProTableMetaService.js +31 -7
  25. package/dist/libs/crud-pro/utils/DatabaseName.js +24 -3
  26. package/dist/libs/global-config/global-config.d.ts +6 -0
  27. package/dist/libs/global-config/global-config.js +1 -0
  28. package/dist/models/AsyncTaskModel.d.ts +4 -2
  29. package/dist/schedule/anonymousContext.d.ts +13 -0
  30. package/dist/schedule/anonymousContext.js +59 -0
  31. package/dist/schedule/index.d.ts +4 -4
  32. package/dist/schedule/index.js +9 -68
  33. package/dist/schedule/runSchedule.d.ts +6 -0
  34. package/dist/schedule/runSchedule.js +34 -0
  35. package/dist/schedule/scheduleNames.d.ts +8 -0
  36. package/dist/schedule/scheduleNames.js +17 -0
  37. package/dist/service/asyncTask/AsyncTaskRunnerService.d.ts +14 -0
  38. package/dist/service/asyncTask/AsyncTaskRunnerService.js +63 -13
  39. package/dist/service/asyncTask/AsyncTaskService.js +2 -1
  40. package/dist/service/crudstd/CrudStdService.d.ts +1 -0
  41. package/dist/service/crudstd/CrudStdService.js +27 -0
  42. package/dist/service/curd/CrudProQuick.d.ts +24 -0
  43. package/dist/service/curd/CrudProQuick.js +105 -0
  44. package/dist/service/curd/CurdMixByLinkToCustomService.d.ts +10 -1
  45. package/dist/service/curd/CurdMixByLinkToCustomService.js +71 -23
  46. package/dist/service/curd/CurdMixService.d.ts +1 -1
  47. package/dist/service/curd/CurdMixService.js +2 -2
  48. package/dist/service/curd/CurdMixUtils.js +35 -28
  49. package/dist/service/curd/CurdProService.d.ts +2 -10
  50. package/dist/service/curd/CurdProService.js +28 -143
  51. package/dist/service/curd/fixCfgModel.d.ts +3 -0
  52. package/dist/service/curd/fixCfgModel.js +107 -0
  53. package/package.json +1 -1
  54. package/src/configuration.ts +30 -10
  55. package/src/controller/gateway/AnyApiGatewayController.ts +1 -1
  56. package/src/controller/gateway/AsyncTaskController.ts +18 -5
  57. package/src/controller/gateway/CrudMtdGatewayController.ts +5 -5
  58. package/src/controller/manage/DocManageApi.ts +5 -5
  59. package/src/index.ts +3 -0
  60. package/src/libs/crud-pro/CrudPro.ts +8 -0
  61. package/src/libs/crud-pro/defaultConfigs.ts +2 -0
  62. package/src/libs/crud-pro/interfaces.ts +10 -1
  63. package/src/libs/crud-pro/models/ExecuteContext.ts +5 -2
  64. package/src/libs/crud-pro/models/ExecuteContextFunc.ts +12 -1
  65. package/src/libs/crud-pro/models/ResModel.ts +24 -0
  66. package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +26 -17
  67. package/src/libs/crud-pro/services/CrudProServiceBase.ts +5 -8
  68. package/src/libs/crud-pro/services/CrudProTableMetaService.ts +43 -9
  69. package/src/libs/crud-pro/utils/DatabaseName.ts +35 -15
  70. package/src/libs/global-config/global-config.ts +12 -1
  71. package/src/models/AsyncTaskModel.ts +5 -2
  72. package/src/schedule/anonymousContext.ts +79 -0
  73. package/src/schedule/index.ts +13 -72
  74. package/src/schedule/runSchedule.ts +35 -0
  75. package/src/schedule/scheduleNames.ts +20 -0
  76. package/src/service/asyncTask/AsyncTaskRunnerService.ts +86 -19
  77. package/src/service/asyncTask/AsyncTaskService.ts +3 -2
  78. package/src/service/base/BaseService.ts +2 -2
  79. package/src/service/crudstd/CrudStdService.ts +34 -6
  80. package/src/service/curd/CrudProQuick.ts +137 -0
  81. package/src/service/curd/CurdMixByLinkToCustomService.ts +100 -49
  82. package/src/service/curd/CurdMixService.ts +3 -3
  83. package/src/service/curd/CurdMixUtils.ts +51 -38
  84. package/src/service/curd/CurdProService.ts +42 -186
  85. package/src/service/curd/fixCfgModel.ts +139 -0
@@ -26,12 +26,14 @@ export interface SysAsyncTaskEntity {
26
26
  priority: number;
27
27
  retry_count: number;
28
28
  max_retries: number;
29
+
29
30
  parent_task_id: number | null;
30
31
  created_at: Date;
31
32
  started_at: Date | null;
32
- completed_at: Date | null;
33
33
  updated_at: Date;
34
34
  expired_at: Date | null;
35
+ completed_at: Date | null;
36
+ created_user_session: string;
35
37
  }
36
38
 
37
39
  export enum SysAsyncTaskStatus {
@@ -58,11 +60,12 @@ export enum SysAsyncFileFormat {
58
60
 
59
61
 
60
62
  export interface ISysAsyncTaskHandler {
61
- execute(ctx: SysAsyncTaskContext): Promise<void>;
63
+ execute(asyncTaskContext: SysAsyncTaskContext): Promise<void>;
62
64
  }
63
65
 
64
66
  export interface SysAsyncTaskContext {
65
67
  task: SysAsyncTaskEntity;
68
+ updateTaskStatus: ()=>any;
66
69
  }
67
70
 
68
71
  export interface SysAsyncTaskHandlerConfig {
@@ -0,0 +1,79 @@
1
+ import * as koa from '@midwayjs/koa';
2
+ import {ContextLogger} from "@/models/contextLogger";
3
+ import {Transaction} from "@/libs/crud-pro/models/Transaction";
4
+ import {ISessionInfo, SYS_ACCOUNT_TYPE, UserSessionInfo} from "@/models/userSession";
5
+ import {IWorkbenchEntity} from "@/models/SystemEntities";
6
+
7
+
8
+ const anonymousUserInfo:ISessionInfo = {
9
+ nickName: 'anonymous_user',
10
+ avatar: '',
11
+ roleCodes: [],
12
+ functionCodes: [],
13
+ loginName: 'anonymous_user',
14
+ sessionId: '',
15
+ accountId: '',
16
+ workbenchCode: '',
17
+ accountType: SYS_ACCOUNT_TYPE,
18
+ };
19
+
20
+ const anonymousWorkbenchInfo: IWorkbenchEntity = {
21
+ id: 0,
22
+ workbench_code: 'anonymous_workbench',
23
+ workbench_name: 'anonymous_workbench',
24
+ workbench_domain: 'anonymous_workbench',
25
+ workbench_desc: 'anonymous_workbench',
26
+ html_content: 'anonymous_workbench',
27
+ status: 1,
28
+ workbench_type: 0,
29
+ };
30
+
31
+
32
+ export type RunServiceAtAnonymousContextRunner = (ctx: koa.IMidwayKoaContext)=>any;
33
+
34
+
35
+ class AnonymousContext {
36
+ private app: koa.Application;
37
+
38
+ setApp(app: koa.Application){
39
+ this.app = app;
40
+ }
41
+
42
+ getApp(){
43
+ return this.app;
44
+ }
45
+
46
+
47
+ async runServiceAtAnonymousContext( runner: RunServiceAtAnonymousContextRunner) {
48
+ const app = this.app;
49
+ const ctx: koa.IMidwayKoaContext = app.createAnonymousContext();
50
+
51
+ ctx.app = app;
52
+ ctx.contextLogger = new ContextLogger(ctx as any);
53
+ ctx.transaction = new Transaction();
54
+ ctx.userSession = new UserSessionInfo(anonymousUserInfo, true);
55
+ ctx.workbenchInfo = anonymousWorkbenchInfo;
56
+
57
+ let result: any = null;
58
+ let error: any = null;
59
+ try {
60
+ result = await runner(ctx);
61
+ } catch (e) {
62
+ error = e;
63
+ ctx.logger.error(`runServiceAtAnonymousContext error `, e);
64
+ } finally {
65
+ await ctx.transaction.releaseTx();
66
+ }
67
+ return {result: result, error: error};
68
+ }
69
+
70
+ }
71
+
72
+
73
+ const ANONYMOUS_CONTEXT = new AnonymousContext()
74
+
75
+
76
+
77
+ export {
78
+ ANONYMOUS_CONTEXT
79
+ }
@@ -1,73 +1,14 @@
1
- import * as koa from '@midwayjs/koa';
2
- import { IScheduleService } from '../interface';
3
- import { ContextLogger } from '../models/contextLogger';
4
- import { Transaction } from '../libs/crud-pro/models/Transaction';
5
- import {ISessionInfo, SYS_ACCOUNT_TYPE, UserSessionInfo} from '../models/userSession';
6
- import { IWorkbenchEntity } from '../models/SystemEntities';
7
-
8
- const scheduleUserInfo:ISessionInfo = {
9
- nickName: 'schedule_user',
10
- avatar: '',
11
- roleCodes: [],
12
- functionCodes: [],
13
- loginName: 'schedule_user',
14
- sessionId: '',
15
- accountId: '',
16
- workbenchCode: '',
17
- accountType: SYS_ACCOUNT_TYPE,
18
- };
19
-
20
- const scheduleWorkbenchInfo: IWorkbenchEntity = {
21
- id: 0,
22
- workbench_code: 'schedule_workbench',
23
- workbench_name: 'schedule_workbench',
24
- workbench_domain: 'schedule_workbench',
25
- workbench_desc: 'schedule_workbench',
26
- html_content: 'schedule_workbench',
27
- status: 1,
28
- workbench_type: 0,
29
- };
30
-
31
- async function runScheduleService(app: koa.Application, serviceName: string) {
32
- const ctx = app.createAnonymousContext();
33
-
34
- ctx.app = app;
35
- ctx.contextLogger = new ContextLogger(ctx as any);
36
- ctx.transaction = new Transaction();
37
- ctx.userSession = new UserSessionInfo(scheduleUserInfo, true);
38
- ctx.workbenchInfo = scheduleWorkbenchInfo;
39
-
40
- try {
41
- const serviceObject: IScheduleService = await ctx.requestContext.getAsync(serviceName);
42
- if (serviceObject && serviceObject.runBySchedule) {
43
- await serviceObject.runBySchedule();
44
- } else {
45
- ctx.logger.error(`runSchedule error , serviceName = ${serviceName}, 没有找到服务`);
46
- }
47
- } catch (e) {
48
- ctx.logger.error(`runSchedule error , serviceName = ${serviceName}`, e);
49
- } finally {
50
- await ctx.transaction.releaseTx();
51
- }
1
+ import {startScheduleLoop, runScheduleTaskOnce} from './runSchedule'
2
+ import {ANONYMOUS_CONTEXT} from './anonymousContext'
3
+ import {
4
+ ALL_SCHEDULE_NAMES,
5
+ SCHEDULE_NAMES,
6
+ } from './scheduleNames'
7
+
8
+ export {
9
+ ALL_SCHEDULE_NAMES,
10
+ SCHEDULE_NAMES,
11
+ ANONYMOUS_CONTEXT,
12
+ startScheduleLoop,
13
+ runScheduleTaskOnce
52
14
  }
53
-
54
- async function runSchedule(app: koa.Application, serviceList: string[]) {
55
- for (let i = 0; i < serviceList.length; i++) {
56
- const serviceName = serviceList[i];
57
- try {
58
- await runScheduleService(app, serviceName);
59
- } catch (e) {
60
- const ctx = app.createAnonymousContext();
61
- ctx.logger.error(`runSchedule error , serviceName = ${serviceName}`, e);
62
- }
63
- }
64
- }
65
-
66
- async function startSchedule(app: koa.Application, serviceList: string[]) {
67
- await runSchedule(app, serviceList);
68
- setInterval(() => {
69
- runSchedule(app, serviceList);
70
- }, 2 * 60 * 1000);
71
- }
72
-
73
- export { startSchedule, runSchedule };
@@ -0,0 +1,35 @@
1
+ import * as koa from '@midwayjs/koa';
2
+ import { IScheduleService } from '@/interface';
3
+ import { ANONYMOUS_CONTEXT } from "./anonymousContext";
4
+
5
+
6
+ async function runScheduleTaskOnce(serviceName: string) {
7
+ return await ANONYMOUS_CONTEXT.runServiceAtAnonymousContext(async (ctx: koa.IMidwayKoaContext)=>{
8
+ const serviceObject: IScheduleService = await ctx.requestContext.getAsync(serviceName);
9
+ if (serviceObject && serviceObject.runBySchedule) {
10
+ await serviceObject.runBySchedule();
11
+ } else {
12
+ ctx.logger.error(`runSchedule error , serviceName = ${serviceName}, 没有找到服务`);
13
+ }
14
+ });
15
+ }
16
+
17
+ async function runScheduleServiceList(serviceList: string[]) {
18
+ for (let i = 0; i < serviceList.length; i++) {
19
+ const serviceName = serviceList[i];
20
+ try {
21
+ await runScheduleTaskOnce(serviceName);
22
+ } catch (e) {
23
+ ANONYMOUS_CONTEXT.getApp().getCoreLogger().error(`runSchedule error , serviceName = ${serviceName}`, e);
24
+ }
25
+ }
26
+ }
27
+
28
+ async function startScheduleLoop(serviceList: string[]) {
29
+ await runScheduleServiceList(serviceList);
30
+ setInterval(() => {
31
+ runScheduleServiceList(serviceList);
32
+ }, 2 * 60 * 1000);
33
+ }
34
+
35
+ export { startScheduleLoop, runScheduleTaskOnce };
@@ -0,0 +1,20 @@
1
+
2
+
3
+ const SCHEDULE_NAMES = {
4
+ proxyApiLoadService: 'proxyApiLoadService',
5
+ workbenchService: 'workbenchService',
6
+ visitStatService: 'visitStatService',
7
+ asyncTaskRunnerService: 'asyncTaskRunnerService',
8
+ }
9
+
10
+ const ALL_SCHEDULE_NAMES = [
11
+ SCHEDULE_NAMES.proxyApiLoadService,
12
+ SCHEDULE_NAMES.workbenchService,
13
+ SCHEDULE_NAMES.visitStatService,
14
+ SCHEDULE_NAMES.asyncTaskRunnerService
15
+ ];
16
+
17
+ export {
18
+ ALL_SCHEDULE_NAMES,
19
+ SCHEDULE_NAMES,
20
+ }
@@ -1,5 +1,5 @@
1
1
  import {Inject, Provide} from '@midwayjs/core';
2
- import {Context} from '@midwayjs/koa';
2
+ import {Context, IMidwayKoaContext} from '@midwayjs/koa';
3
3
  import {BaseService} from "@/service/base/BaseService";
4
4
  import {IScheduleService} from "@/interface";
5
5
  import {CurdProService} from "@/service/curd/CurdProService";
@@ -8,9 +8,10 @@ import {GLOBAL_STATIC_CONFIG} from "@/libs/global-config/global-config";
8
8
  import {SystemTables} from "@/models/SystemTables";
9
9
  import {KeysOfSimpleSQL} from "@/libs/crud-pro/models/keys";
10
10
  import {errorToString} from "@/libs/utils/errorToString";
11
+ import {ANONYMOUS_CONTEXT} from "@/schedule";
11
12
 
12
13
 
13
- class AsyncTaskRunner {
14
+ class AsyncTaskRunner {
14
15
  isBusy: boolean = false;
15
16
  taskHandlerMap: Map<string, ISysAsyncTaskHandler> = new Map();
16
17
 
@@ -18,19 +19,29 @@ class AsyncTaskRunner {
18
19
  if (!taskList || taskList.length === 0) {
19
20
  return;
20
21
  }
22
+
21
23
  this.isBusy = true;
22
24
 
23
25
  for (let i = 0; i < taskList.length; i++) {
24
26
  const taskElement = taskList[i];
27
+
25
28
  try {
29
+ // 任务状态的设置在自定义的执行器里面。
26
30
  await this.executeTask(taskElement);
27
- taskElement.task_status = SysAsyncTaskStatus.SUCCEEDED;
28
31
  } catch (error) {
29
32
  taskElement.task_status = SysAsyncTaskStatus.FAILED;
30
33
  taskElement.error_message = errorToString(error)
31
34
  }
35
+
36
+ try {
37
+ await this.updateTaskStatus(taskElement);
38
+ } catch (error) {
39
+ ANONYMOUS_CONTEXT.getApp().getCoreLogger().error("executeTaskList error", error);
40
+ }
32
41
  }
33
42
 
43
+ this.isBusy = false;
44
+
34
45
  }
35
46
 
36
47
  private async executeTask(taskElement: SysAsyncTaskEntity) {
@@ -39,18 +50,52 @@ class AsyncTaskRunner {
39
50
  if (!taskHandler) {
40
51
  throw new Error('TaskHandler not found , taskType = ' + taskType);
41
52
  }
42
- await taskHandler.execute({task: taskElement});
53
+ const updateTaskStatus = () => {
54
+ return this.updateTaskStatus(taskElement);
55
+ }
56
+ await taskHandler.execute({task: taskElement, updateTaskStatus});
57
+ }
58
+
59
+
60
+ private async updateTaskStatus(taskElement: SysAsyncTaskEntity) {
61
+ return await ANONYMOUS_CONTEXT.runServiceAtAnonymousContext(async (ctx: IMidwayKoaContext) => {
62
+ const curdProService: CurdProService = await ctx.requestContext.getAsync("curdProService");
63
+ const {SystemDbName, SystemDbType} = GLOBAL_STATIC_CONFIG.getConfig();
64
+
65
+ const {id, ...otherTaskProps} = taskElement;
66
+
67
+ const res = await curdProService.executeCrudByCfg({
68
+ condition: {
69
+ id: id
70
+ },
71
+ data: otherTaskProps
72
+ }, {
73
+ sqlTable: SystemTables.sys_async_tasks,
74
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_UPDATE,
75
+ sqlDatabase: SystemDbName,
76
+ sqlDbType: SystemDbType,
77
+ });
78
+ return res.getResModel().affected;
79
+ });
80
+
81
+
43
82
  }
44
83
  }
45
84
 
46
85
 
47
- const ASYNC_TASK_RUNNER = new AsyncTaskRunner();
86
+ /**
87
+ * 业务可以扩展
88
+ */
89
+ export const ASYNC_TASK_RUNNER = new AsyncTaskRunner();
48
90
 
49
91
 
92
+ /**
93
+ * Redis锁
94
+ */
50
95
  const LOCK_KEY_ASYNC_TASK_RUNNER = "LOCK_KEY_ASYNC_TASK_RUNNER"
51
96
 
52
97
  @Provide()
53
- export class AsyncTaskRunnerService extends BaseService implements IScheduleService{
98
+ export class AsyncTaskRunnerService extends BaseService implements IScheduleService {
54
99
 
55
100
  @Inject()
56
101
  protected ctx: Context;
@@ -58,31 +103,25 @@ export class AsyncTaskRunnerService extends BaseService implements IScheduleServ
58
103
  @Inject()
59
104
  private curdProService: CurdProService;
60
105
 
61
- async runBySchedule () {
62
- if (ASYNC_TASK_RUNNER.isBusy) {
63
- return Promise.resolve();
64
- }
65
106
 
66
- const lock = await this.redisService.set(LOCK_KEY_ASYNC_TASK_RUNNER, 1,"EX", 3600, "NX");
67
- if (lock !== 'OK') {
68
- return Promise.resolve();
69
- }
107
+ async fetchPendingTasks(): Promise<void> {
70
108
 
71
-
72
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
109
+ const {SystemDbName, SystemDbType} = GLOBAL_STATIC_CONFIG.getConfig();
73
110
 
74
111
  const queryRes = await this.curdProService.executeCrudByCfg({
75
112
  condition: {
76
113
  task_status: SysAsyncTaskStatus.PENDING
77
114
  },
115
+ orderBy: 'id+',
78
116
  limit: 10
79
- },{
117
+ }, {
80
118
  sqlTable: SystemTables.sys_async_tasks,
81
119
  sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY_PAGE,
82
120
  sqlDatabase: SystemDbName,
83
121
  sqlDbType: SystemDbType,
84
122
  });
85
123
 
124
+
86
125
  const taskList = queryRes.getResRows();
87
126
 
88
127
  if (taskList.length === 0) {
@@ -100,7 +139,7 @@ export class AsyncTaskRunnerService extends BaseService implements IScheduleServ
100
139
  data: {
101
140
  task_status: SysAsyncTaskStatus.RUNNING
102
141
  }
103
- },{
142
+ }, {
104
143
  sqlTable: SystemTables.sys_async_tasks,
105
144
  sqlSimpleName: KeysOfSimpleSQL.SIMPLE_UPDATE,
106
145
  sqlDatabase: SystemDbName,
@@ -108,7 +147,35 @@ export class AsyncTaskRunnerService extends BaseService implements IScheduleServ
108
147
  });
109
148
 
110
149
 
111
- ASYNC_TASK_RUNNER.executeTaskList(taskList);
150
+ ASYNC_TASK_RUNNER.executeTaskList(taskList).then(() => {
151
+ console.log("ASYNC_TASK_RUNNER finished taskIds ==> " + JSON.stringify(taskIds))
152
+ })
153
+
154
+ }
155
+
156
+
157
+ async runBySchedule() {
158
+ if (ASYNC_TASK_RUNNER.isBusy) {
159
+ return Promise.resolve();
160
+ }
161
+
162
+ // 这里的过期时间1分钟即可。fetchPendingTasks函数不可能超过一分钟。
163
+ // 因为这里只是从数据库中获取一批任务,放到自己的任务队列里。还没触发执行。
164
+ const lock = await this.redisService.set(LOCK_KEY_ASYNC_TASK_RUNNER, 1, "EX", 60, "NX");
165
+ if (lock !== 'OK') {
166
+ return Promise.resolve();
167
+ }
168
+
169
+ try {
170
+ await this.fetchPendingTasks();
171
+ } catch (e) {
172
+ console.error("fetchPendingTasks error", errorToString(e));
173
+ }
174
+
175
+ await this.redisService.del(LOCK_KEY_ASYNC_TASK_RUNNER);
176
+
112
177
  return Promise.resolve();
113
178
  }
179
+
180
+
114
181
  }
@@ -1,7 +1,8 @@
1
1
  import {Inject, Provide} from '@midwayjs/core';
2
2
  import {Context} from '@midwayjs/koa';
3
3
  import {BaseService} from "@/service/base/BaseService";
4
- import {runSchedule} from "@/schedule";
4
+ import {runScheduleTaskOnce} from "@/schedule";
5
+ import {SCHEDULE_NAMES} from "@/schedule/scheduleNames";
5
6
 
6
7
  @Provide()
7
8
  export class AsyncTaskService extends BaseService {
@@ -9,7 +10,7 @@ export class AsyncTaskService extends BaseService {
9
10
  protected ctx: Context;
10
11
 
11
12
  async startTask() {
12
- runSchedule(this.app, ['asyncTaskRunnerService']).then(schedule => {
13
+ runScheduleTaskOnce(SCHEDULE_NAMES.asyncTaskRunnerService).then(schedule => {
13
14
  console.log(schedule);
14
15
  })
15
16
  }
@@ -3,8 +3,8 @@ import { Context } from '@midwayjs/koa';
3
3
  import { RedisService } from '@midwayjs/redis';
4
4
  import { OSSService, OSSServiceFactory } from '@midwayjs/oss';
5
5
  import * as koa from '@midwayjs/koa';
6
- import { consoleLogger, ContextLogger, ILoggerContext } from '../../models/contextLogger';
7
- import { isEnableDebug, isLocalEnv } from '../../libs/utils/fatcms-request';
6
+ import { consoleLogger, ContextLogger, ILoggerContext } from '@/models/contextLogger';
7
+ import { isEnableDebug, isLocalEnv } from '@/libs/utils/fatcms-request';
8
8
 
9
9
 
10
10
  @Provide()
@@ -2,18 +2,19 @@ import { Inject, Provide } from '@midwayjs/core';
2
2
  import { Context } from '@midwayjs/koa';
3
3
  import { CurdMixService } from '../curd/CurdMixService';
4
4
  import { IRequestCfgModel, IRequestModel } from '../../libs/crud-pro/interfaces';
5
- import { KeysOfAuthType, KeysOfSimpleSQL } from '../../libs/crud-pro/models/keys';
5
+ import { KeysOfAuthType, KeysOfSimpleSQL, SqlDbType } from '../../libs/crud-pro/models/keys';
6
6
  import { parseJsonObject } from '../../libs/utils/functions';
7
7
  import { ICrudStdAppInfo, ICrudStdAppInfoForSettingKey } from '../../models/bizmodels';
8
8
  import { BizException } from '../../models/devops';
9
9
  import { ExecuteContext } from '../../libs/crud-pro/models/ExecuteContext';
10
- import { SystemTables} from '../../models/SystemTables';
10
+ import { SystemTables } from '../../models/SystemTables';
11
11
  import { CrudStdActionService } from './CrudStdActionService';
12
12
  import { CrudStdRelationService } from './CrudStdRelationService';
13
13
  import * as _ from 'lodash';
14
14
  import { ApiBaseService } from '../base/ApiBaseService';
15
- import {parseDatabaseName} from "../../libs/crud-pro/utils/DatabaseName";
15
+ import { parseDatabaseName } from '../../libs/crud-pro/utils/DatabaseName';
16
16
  import { GLOBAL_STATIC_CONFIG } from '../../libs/global-config/global-config';
17
+ import { MixinUtils } from '@/libs/crud-pro/utils/MixinUtils';
17
18
 
18
19
  export const SPECIAL_SETTING_KEY = {
19
20
  QUERY_LIST: 'QUERY_LIST',
@@ -69,6 +70,9 @@ export class CrudStdService extends ApiBaseService {
69
70
  // 根据用户配置,设置关联查询的数据信息。
70
71
  await this.crudStdRelationService.addCfgModelColumnsRelation(cfgModel, appInfo);
71
72
 
73
+ // 根据表结构的数据类型,修正数据类型
74
+ await this.fixDataFieldTypeBySqlTableField(params, cfgModel, appInfo, { dbType, dbName });
75
+
72
76
  // 业务系统自定义的修改cfgModel
73
77
  await GLOBAL_STATIC_CONFIG.getConfig().bizUpdateCfgModelForCrudStd(params, cfgModel, appInfo, this.ctx);
74
78
 
@@ -138,11 +142,8 @@ export class CrudStdService extends ApiBaseService {
138
142
  * @private
139
143
  */
140
144
  private async getCrudStdAppInfo(appCode?: string) {
141
-
142
145
  const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
143
146
 
144
-
145
-
146
147
  this.logInfo('getCrudStdAppInfo', appCode);
147
148
 
148
149
  const ss = {
@@ -195,4 +196,31 @@ export class CrudStdService extends ApiBaseService {
195
196
  }
196
197
  return true; // 无需鉴权
197
198
  }
199
+
200
+ private async fixDataFieldTypeBySqlTableField(params: IRequestModel, cfgModel: IRequestCfgModel, appInfo: ICrudStdAppInfoForSettingKey, arg3: { dbType: SqlDbType; dbName: string }) {
201
+ const tableFields = _.get(appInfo, 'stdCrudCfgObj.tableFields');
202
+
203
+ const tableFieldMap = MixinUtils.toMap(tableFields, (e: any) => {
204
+ return e.fieldIndex;
205
+ });
206
+
207
+ if (arg3.dbType === SqlDbType.postgres) {
208
+ if (cfgModel.sqlSimpleName === KeysOfSimpleSQL.SIMPLE_INSERT || cfgModel.sqlSimpleName === KeysOfSimpleSQL.SIMPLE_UPDATE) {
209
+ const data = params.data || {};
210
+ const dataKeys = Object.keys(data);
211
+ for (let index = 0; index < dataKeys.length; index++) {
212
+ const dataKey = dataKeys[index];
213
+ const dataValue = data[dataKey];
214
+ const fieldInfo = tableFieldMap[dataKey];
215
+ const fieldType = ('' + _.get(fieldInfo, 'type')).toUpperCase();
216
+ if (fieldType === 'ARRAY') {
217
+ const dataValueArray = parseJsonObject(dataValue);
218
+ if (Array.isArray(dataValueArray)) {
219
+ data[dataKey] = `{${dataValueArray.map(v => `"${v}"`).join(',')}}`;
220
+ }
221
+ }
222
+ }
223
+ }
224
+ }
225
+ }
198
226
  }
@@ -0,0 +1,137 @@
1
+ import { KeysOfSimpleSQL, SqlDbType } from "@/libs/crud-pro/models/keys";
2
+ import { IRequestCfgModel, IRequestModel, ISqlCfgModel } from "@/libs/crud-pro/interfaces";
3
+ import { IRequestCfgModel2 } from "@/models/bizmodels";
4
+ import { CrudPro } from "@/libs/crud-pro/CrudPro";
5
+ import { ResModelAffected, ResModelStandard } from "@/libs/crud-pro/models/ResModel";
6
+
7
+
8
+
9
+ export class CrudProQuick {
10
+ private readonly curdPro: CrudPro;
11
+ private readonly sqlDbType: SqlDbType;
12
+ private readonly sqlDatabase: string;
13
+ private readonly sqlTable?: string;
14
+ private baseCfgModel: IRequestCfgModel2 = {};
15
+
16
+ constructor(curdPro: CrudPro, sqlDatabase: string, sqlDbType: SqlDbType, sqlTable?: string) {
17
+ this.sqlDatabase = sqlDatabase;
18
+ this.sqlDbType = sqlDbType;
19
+ this.curdPro = curdPro;
20
+ this.sqlTable = sqlTable; // 全局sqlTable为空时,单独调用时就必须传sqlTable参数。
21
+ }
22
+
23
+ public setBaseCfgModel(baseCfgModel: IRequestCfgModel2) {
24
+ Object.assign(this.baseCfgModel, baseCfgModel);
25
+ }
26
+
27
+ private async executeCrudByCfg(reqJson: IRequestModel, cfgModel2: IRequestCfgModel2) {
28
+
29
+ const cfgModel = { ...this.baseCfgModel };
30
+ Object.assign(cfgModel, cfgModel2);
31
+
32
+ cfgModel.method = 'CrudProQuickAnonymous';
33
+ cfgModel.updateCfg = {};
34
+ cfgModel.sqlDatabase = this.sqlDatabase;
35
+ cfgModel.sqlDbType = this.sqlDbType;
36
+
37
+ if (!cfgModel.sqlTable) {
38
+ cfgModel.sqlTable = this.sqlTable;
39
+ }
40
+
41
+ if (!cfgModel.sqlTable) {
42
+ throw new Error('[CrudProQuick] sqlTable not found');
43
+ }
44
+
45
+ return await this.curdPro.executeCrudByCfg(reqJson, cfgModel);
46
+ }
47
+
48
+
49
+ public async getOne(reqJson: IRequestModel, sqlTable?: string): Promise<any> {
50
+ const cfgModel: IRequestCfgModel = {
51
+ sqlTable,
52
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY_ONE,
53
+ };
54
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
55
+ return res.getOneObj();
56
+ }
57
+
58
+ public async getList(reqJson: IRequestModel, sqlTable?: string): Promise<any[]> {
59
+ const cfgModel: IRequestCfgModel = {
60
+ sqlTable,
61
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
62
+ };
63
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
64
+ return res.getResRows();
65
+ }
66
+
67
+ public async getListPage(reqJson: IRequestModel, sqlTable?: string): Promise<ResModelStandard> {
68
+ const cfgModel: IRequestCfgModel = {
69
+ sqlTable,
70
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY_PAGE,
71
+ };
72
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
73
+ return res.getResModel(); // 因为它有2个返回值
74
+ }
75
+
76
+ public async getTotalCount(reqJson: IRequestModel, sqlTable?: string): Promise<number> {
77
+ const cfgModel: IRequestCfgModel = {
78
+ sqlTable,
79
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY_COUNT,
80
+ };
81
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
82
+ return res.getResModel().total_count;
83
+ }
84
+
85
+ public async insertObject(reqJson: IRequestModel, sqlTable?: string): Promise<ResModelAffected> {
86
+ const cfgModel: IRequestCfgModel = {
87
+ sqlTable,
88
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_INSERT,
89
+ };
90
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
91
+ return res.getResModel().affected;
92
+ }
93
+
94
+ public async updateObject(reqJson: IRequestModel, sqlTable?: string): Promise<ResModelAffected> {
95
+ const cfgModel: IRequestCfgModel = {
96
+ sqlTable,
97
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_UPDATE,
98
+ };
99
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
100
+ return res.getResModel().affected;
101
+ }
102
+
103
+ public async insertOrUpdate(reqJson: IRequestModel, sqlTable?: string): Promise<ResModelStandard> {
104
+ const cfgModel: IRequestCfgModel = {
105
+ sqlTable,
106
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_INSERT_OR_UPDATE,
107
+ };
108
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
109
+ return res.getResModel(); // 因为它有三个返回值
110
+ }
111
+
112
+ public async deleteObject(reqJson: IRequestModel, sqlTable?: string): Promise<ResModelAffected> {
113
+ const cfgModel: IRequestCfgModel = {
114
+ sqlTable,
115
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_DELETE,
116
+ };
117
+ const res = await this.executeCrudByCfg(reqJson, cfgModel);
118
+ return res.getResModel().affected;
119
+ }
120
+
121
+
122
+ public async executeSQL(executeSql: string, args?: any[]): Promise<any> {
123
+ const sqlCfgModel: ISqlCfgModel = {
124
+ executeSql: executeSql,
125
+ executeSqlArgs: args,
126
+ sqlDatabase: this.sqlDatabase,
127
+ sqlDbType: this.sqlDbType,
128
+ };
129
+ return await this.curdPro.executeSQL(sqlCfgModel);
130
+ }
131
+
132
+
133
+
134
+
135
+ }
136
+
137
+