@ruiapp/rapid-core 0.10.2 → 0.10.3
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/helpers/dbTransactionHelper.d.ts +9 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +52 -8
- package/dist/plugins/serverOperation/ServerOperationPluginTypes.d.ts +4 -0
- package/dist/plugins/serverOperation/actionHandlers/runServerOperation.d.ts +1 -0
- package/dist/types/cron-job-types.d.ts +4 -0
- package/package.json +1 -1
- package/src/helpers/dbTransactionHelper.ts +42 -0
- package/src/index.ts +1 -0
- package/src/plugins/cronJob/services/CronJobService.ts +10 -6
- package/src/plugins/serverOperation/ServerOperationPlugin.ts +1 -0
- package/src/plugins/serverOperation/ServerOperationPluginTypes.ts +5 -0
- package/src/plugins/serverOperation/actionHandlers/runServerOperation.ts +6 -1
- package/src/types/cron-job-types.ts +5 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RouteContext } from "../core/routeContext";
|
|
2
|
+
/**
|
|
3
|
+
* 在事务中执行`func`
|
|
4
|
+
* @param routeContext
|
|
5
|
+
* @param func
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare function executeInDbTransaction<TResult>(routeContext: RouteContext, func: () => Promise<TResult>): Promise<TResult>;
|
|
9
|
+
export declare function executeInRouteContext<TResult>(routeContext: RouteContext, inDbTransaction: boolean | undefined | null, func: () => Promise<TResult>): Promise<TResult>;
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export * from "./helpers/licenseHelper";
|
|
|
23
23
|
export * as entityHelper from "./helpers/entityHelper";
|
|
24
24
|
export * as licenseHelper from "./helpers/licenseHelper";
|
|
25
25
|
export * as metaHelper from "./helpers/metaHelper";
|
|
26
|
+
export * from "./helpers/dbTransactionHelper";
|
|
26
27
|
export * from "./deno-std/http/cookie";
|
|
27
28
|
export { mapDbRowToEntity } from "./dataAccess/entityMapper";
|
|
28
29
|
export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
|
package/dist/index.js
CHANGED
|
@@ -5380,6 +5380,43 @@ var licenseHelper = /*#__PURE__*/Object.freeze({
|
|
|
5380
5380
|
tryValidateLicense: tryValidateLicense
|
|
5381
5381
|
});
|
|
5382
5382
|
|
|
5383
|
+
/**
|
|
5384
|
+
* 在事务中执行`func`
|
|
5385
|
+
* @param routeContext
|
|
5386
|
+
* @param func
|
|
5387
|
+
* @returns
|
|
5388
|
+
*/
|
|
5389
|
+
async function executeInDbTransaction(routeContext, func) {
|
|
5390
|
+
if (!routeContext) {
|
|
5391
|
+
throw new Error(`Patameter "routeContext" is required if you want run in transaction.`);
|
|
5392
|
+
}
|
|
5393
|
+
let transactionDbClient;
|
|
5394
|
+
try {
|
|
5395
|
+
transactionDbClient = await routeContext.initDbTransactionClient();
|
|
5396
|
+
await routeContext.beginDbTransaction();
|
|
5397
|
+
const result = await func();
|
|
5398
|
+
await routeContext.commitDbTransaction();
|
|
5399
|
+
return result;
|
|
5400
|
+
}
|
|
5401
|
+
catch (ex) {
|
|
5402
|
+
await routeContext.rollbackDbTransaction();
|
|
5403
|
+
throw ex;
|
|
5404
|
+
}
|
|
5405
|
+
finally {
|
|
5406
|
+
if (transactionDbClient) {
|
|
5407
|
+
transactionDbClient.release();
|
|
5408
|
+
}
|
|
5409
|
+
}
|
|
5410
|
+
}
|
|
5411
|
+
async function executeInRouteContext(routeContext, inDbTransaction, func) {
|
|
5412
|
+
if (inDbTransaction) {
|
|
5413
|
+
return await executeInDbTransaction(routeContext, func);
|
|
5414
|
+
}
|
|
5415
|
+
else {
|
|
5416
|
+
return await func();
|
|
5417
|
+
}
|
|
5418
|
+
}
|
|
5419
|
+
|
|
5383
5420
|
const values = new Map();
|
|
5384
5421
|
async function set(key, value, options) {
|
|
5385
5422
|
let expireAt = -1;
|
|
@@ -8279,7 +8316,9 @@ class SequencePlugin {
|
|
|
8279
8316
|
const code$5 = "runServerOperation";
|
|
8280
8317
|
async function handler$5(plugin, ctx, options) {
|
|
8281
8318
|
const { operation } = options;
|
|
8282
|
-
await
|
|
8319
|
+
await executeInRouteContext(ctx.routerContext, options.executeInDbTransaction, async () => {
|
|
8320
|
+
await operation(ctx);
|
|
8321
|
+
});
|
|
8283
8322
|
}
|
|
8284
8323
|
|
|
8285
8324
|
var runServerOperation = /*#__PURE__*/Object.freeze({
|
|
@@ -8339,6 +8378,7 @@ class ServerOperationPlugin {
|
|
|
8339
8378
|
config: {
|
|
8340
8379
|
operation: operation.handler,
|
|
8341
8380
|
permissionCheck: operation.permissionCheck,
|
|
8381
|
+
executeInDbTransaction: operation.executeInDbTransaction,
|
|
8342
8382
|
},
|
|
8343
8383
|
},
|
|
8344
8384
|
],
|
|
@@ -9360,13 +9400,15 @@ class CronJobService {
|
|
|
9360
9400
|
let lastErrorStack;
|
|
9361
9401
|
try {
|
|
9362
9402
|
validateLicense(server);
|
|
9363
|
-
|
|
9364
|
-
|
|
9365
|
-
|
|
9366
|
-
|
|
9367
|
-
|
|
9368
|
-
|
|
9369
|
-
|
|
9403
|
+
await executeInRouteContext(handlerContext.routerContext, job.executeInDbTransaction, async () => {
|
|
9404
|
+
if (job.actionHandlerCode) {
|
|
9405
|
+
const actionHandler = server.getActionHandlerByCode(job.code);
|
|
9406
|
+
await actionHandler(handlerContext, job.handleOptions);
|
|
9407
|
+
}
|
|
9408
|
+
else {
|
|
9409
|
+
await job.handler(handlerContext, job.handleOptions);
|
|
9410
|
+
}
|
|
9411
|
+
});
|
|
9370
9412
|
result = "success";
|
|
9371
9413
|
logger.info(`Completed cron job '${jobCode}'...`);
|
|
9372
9414
|
}
|
|
@@ -9947,6 +9989,8 @@ exports.detectChangedFieldsOfEntity = detectChangedFieldsOfEntity;
|
|
|
9947
9989
|
exports.ensureDirectoryExists = ensureDirectoryExists;
|
|
9948
9990
|
exports.entityHelper = entityHelper;
|
|
9949
9991
|
exports.enumFileBaseNamesInDirectory = enumFileBaseNamesInDirectory;
|
|
9992
|
+
exports.executeInDbTransaction = executeInDbTransaction;
|
|
9993
|
+
exports.executeInRouteContext = executeInRouteContext;
|
|
9950
9994
|
exports.formatDateTimeWithTimezone = formatDateTimeWithTimezone;
|
|
9951
9995
|
exports.generateJwtSecretKey = generateJwtSecretKey;
|
|
9952
9996
|
exports.generatePasswordHash = generatePasswordHash;
|
|
@@ -6,6 +6,10 @@ export interface ServerOperation {
|
|
|
6
6
|
description?: string;
|
|
7
7
|
method: RpdHttpMethod;
|
|
8
8
|
permissionCheck?: PermissionCheckPolicy;
|
|
9
|
+
/**
|
|
10
|
+
* 是否在事务中执行
|
|
11
|
+
*/
|
|
12
|
+
executeInDbTransaction?: boolean;
|
|
9
13
|
handler: (ctx: ActionHandlerContext) => Promise<void>;
|
|
10
14
|
}
|
|
11
15
|
export interface ServerOperationPluginInitOptions {
|
|
@@ -3,4 +3,5 @@ import { RapidPlugin } from "../../../core/server";
|
|
|
3
3
|
export declare const code = "runServerOperation";
|
|
4
4
|
export declare function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: {
|
|
5
5
|
operation: (ctx: ActionHandlerContext) => Promise<void>;
|
|
6
|
+
executeInDbTransaction?: boolean;
|
|
6
7
|
}): Promise<void>;
|
package/package.json
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { RouteContext } from "~/core/routeContext";
|
|
2
|
+
import { IDatabaseClient } from "~/types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 在事务中执行`func`
|
|
6
|
+
* @param routeContext
|
|
7
|
+
* @param func
|
|
8
|
+
* @returns
|
|
9
|
+
*/
|
|
10
|
+
export async function executeInDbTransaction<TResult>(routeContext: RouteContext, func: () => Promise<TResult>): Promise<TResult> {
|
|
11
|
+
if (!routeContext) {
|
|
12
|
+
throw new Error(`Patameter "routeContext" is required if you want run in transaction.`);
|
|
13
|
+
}
|
|
14
|
+
let transactionDbClient: IDatabaseClient | undefined;
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
transactionDbClient = await routeContext.initDbTransactionClient();
|
|
18
|
+
await routeContext.beginDbTransaction();
|
|
19
|
+
const result = await func();
|
|
20
|
+
await routeContext.commitDbTransaction();
|
|
21
|
+
return result;
|
|
22
|
+
} catch (ex) {
|
|
23
|
+
await routeContext.rollbackDbTransaction();
|
|
24
|
+
throw ex;
|
|
25
|
+
} finally {
|
|
26
|
+
if (transactionDbClient) {
|
|
27
|
+
transactionDbClient.release();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function executeInRouteContext<TResult>(
|
|
33
|
+
routeContext: RouteContext,
|
|
34
|
+
inDbTransaction: boolean | undefined | null,
|
|
35
|
+
func: () => Promise<TResult>,
|
|
36
|
+
): Promise<TResult> {
|
|
37
|
+
if (inDbTransaction) {
|
|
38
|
+
return await executeInDbTransaction(routeContext, func);
|
|
39
|
+
} else {
|
|
40
|
+
return await func();
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -31,6 +31,7 @@ export * from "./helpers/licenseHelper";
|
|
|
31
31
|
export * as entityHelper from "./helpers/entityHelper";
|
|
32
32
|
export * as licenseHelper from "./helpers/licenseHelper";
|
|
33
33
|
export * as metaHelper from "./helpers/metaHelper";
|
|
34
|
+
export * from "./helpers/dbTransactionHelper";
|
|
34
35
|
|
|
35
36
|
export * from "./deno-std/http/cookie";
|
|
36
37
|
|
|
@@ -7,6 +7,7 @@ import { JobRunningResult, NamedCronJobInstance, SysCronJob, UpdateJobConfigOpti
|
|
|
7
7
|
import { CronJobConfiguration } from "~/types/cron-job-types";
|
|
8
8
|
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
9
9
|
import { formatDateTimeWithTimezone, getNowStringWithTimezone } from "~/utilities/timeUtility";
|
|
10
|
+
import { executeInRouteContext } from "~/helpers/dbTransactionHelper";
|
|
10
11
|
|
|
11
12
|
export default class CronJobService {
|
|
12
13
|
#server: IRpdServer;
|
|
@@ -144,12 +145,15 @@ export default class CronJobService {
|
|
|
144
145
|
try {
|
|
145
146
|
validateLicense(server);
|
|
146
147
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
148
|
+
await executeInRouteContext(handlerContext.routerContext, job.executeInDbTransaction, async () => {
|
|
149
|
+
if (job.actionHandlerCode) {
|
|
150
|
+
const actionHandler = server.getActionHandlerByCode(job.code);
|
|
151
|
+
await actionHandler(handlerContext, job.handleOptions);
|
|
152
|
+
} else {
|
|
153
|
+
await job.handler(handlerContext, job.handleOptions);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
153
157
|
result = "success";
|
|
154
158
|
logger.info(`Completed cron job '${jobCode}'...`);
|
|
155
159
|
} catch (ex: any) {
|
|
@@ -7,6 +7,11 @@ export interface ServerOperation {
|
|
|
7
7
|
description?: string;
|
|
8
8
|
method: RpdHttpMethod;
|
|
9
9
|
permissionCheck?: PermissionCheckPolicy;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 是否在事务中执行
|
|
13
|
+
*/
|
|
14
|
+
executeInDbTransaction?: boolean;
|
|
10
15
|
handler: (ctx: ActionHandlerContext) => Promise<void>;
|
|
11
16
|
}
|
|
12
17
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
2
2
|
import { RapidPlugin } from "~/core/server";
|
|
3
|
+
import { executeInDbTransaction, executeInRouteContext } from "~/helpers/dbTransactionHelper";
|
|
3
4
|
|
|
4
5
|
export const code = "runServerOperation";
|
|
5
6
|
|
|
@@ -8,8 +9,12 @@ export async function handler(
|
|
|
8
9
|
ctx: ActionHandlerContext,
|
|
9
10
|
options: {
|
|
10
11
|
operation: (ctx: ActionHandlerContext) => Promise<void>;
|
|
12
|
+
executeInDbTransaction?: boolean;
|
|
11
13
|
},
|
|
12
14
|
) {
|
|
13
15
|
const { operation } = options;
|
|
14
|
-
|
|
16
|
+
|
|
17
|
+
await executeInRouteContext(ctx.routerContext, options.executeInDbTransaction, async () => {
|
|
18
|
+
await operation(ctx);
|
|
19
|
+
});
|
|
15
20
|
}
|