@midwayjs/core 3.4.13 → 3.5.0

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.
@@ -19,7 +19,7 @@ export declare abstract class DataSourceManager<T> {
19
19
  * @param dataSourceName
20
20
  */
21
21
  isConnected(dataSourceName: string): Promise<boolean>;
22
- createInstance(config: any, clientName: any, options?: CreateInstanceOptions): Promise<T | void>;
22
+ createInstance(config: any, clientName: any, options?: CreateDataSourceInstanceOptions): Promise<T | void>;
23
23
  /**
24
24
  * get data source name by model or repository
25
25
  * @param modelOrRepository
@@ -32,7 +32,14 @@ export declare abstract class DataSourceManager<T> {
32
32
  stop(): Promise<void>;
33
33
  }
34
34
  export declare function globModels(globString: string, appDir: string): any[];
35
- export interface CreateInstanceOptions {
35
+ export interface CreateDataSourceInstanceOptions {
36
+ /**
37
+ * @default false
38
+ */
39
+ validateConnection?: boolean;
40
+ /**
41
+ * @default true
42
+ */
36
43
  cacheInstance?: boolean | undefined;
37
44
  }
38
45
  //# sourceMappingURL=dataSourceManager.d.ts.map
@@ -19,7 +19,7 @@ class DataSourceManager {
19
19
  async initDataSource(options, appDir) {
20
20
  this.options = options;
21
21
  if (!options.dataSource) {
22
- throw new error_1.MidwayParameterError('DataSourceManager must set options.dataSource.');
22
+ throw new error_1.MidwayParameterError('[DataSourceManager] must set options.dataSource.');
23
23
  }
24
24
  for (const dataSourceName in options.dataSource) {
25
25
  const dataSourceOptions = options.dataSource[dataSourceName];
@@ -45,7 +45,8 @@ class DataSourceManager {
45
45
  }
46
46
  // create data source
47
47
  const opts = {
48
- cacheInstance: options.cacheInstance, // will default true
48
+ cacheInstance: options.cacheInstance,
49
+ validateConnection: options.validateConnection,
49
50
  };
50
51
  await this.createInstance(dataSourceOptions, dataSourceName, opts);
51
52
  }
@@ -79,12 +80,22 @@ class DataSourceManager {
79
80
  const cache = options && typeof options.cacheInstance === 'boolean'
80
81
  ? options.cacheInstance
81
82
  : true;
82
- // options.default will be merge in to options.clients[id]
83
+ const validateConnection = (options && options.validateConnection) || false;
84
+ // options.clients[id] will be merged with options.default
83
85
  const configNow = (0, extend_1.extend)(true, {}, this.options['default'], config);
84
86
  const client = await this.createDataSource(configNow, clientName);
85
87
  if (cache && clientName && client) {
86
88
  this.dataSource.set(clientName, client);
87
89
  }
90
+ if (validateConnection) {
91
+ if (!client) {
92
+ throw new error_1.MidwayCommonError(`[DataSourceManager] ${clientName} initialization failed.`);
93
+ }
94
+ const connected = await this.checkConnected(client);
95
+ if (!connected) {
96
+ throw new error_1.MidwayCommonError(`[DataSourceManager] ${clientName} is not connected.`);
97
+ }
98
+ }
88
99
  return client;
89
100
  }
90
101
  /**
@@ -18,6 +18,7 @@ export declare const FrameworkErrorEnum: {
18
18
  readonly INVALID_CONFIG: "MIDWAY_10014";
19
19
  readonly DUPLICATE_CLASS_NAME: "MIDWAY_10015";
20
20
  readonly DUPLICATE_CONTROLLER_PREFIX_OPTIONS: "MIDWAY_10016";
21
+ readonly RETRY_OVER_MAX_TIME: "MIDWAY_10017";
21
22
  };
22
23
  export declare class MidwayCommonError extends MidwayError {
23
24
  constructor(message: string);
@@ -70,4 +71,7 @@ export declare class MidwayDuplicateClassNameError extends MidwayError {
70
71
  export declare class MidwayDuplicateControllerOptionsError extends MidwayError {
71
72
  constructor(prefix: string, existController: string, existControllerOther: string);
72
73
  }
74
+ export declare class MidwayRetryExceededMaxTimesError extends MidwayError {
75
+ constructor(methodName: any, times: number, err: Error);
76
+ }
73
77
  //# sourceMappingURL=framework.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MidwayDuplicateControllerOptionsError = exports.MidwayDuplicateClassNameError = exports.MidwayInconsistentVersionError = exports.MidwayUtilHttpClientTimeoutError = exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayInvalidConfigError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
3
+ exports.MidwayRetryExceededMaxTimesError = exports.MidwayDuplicateControllerOptionsError = exports.MidwayDuplicateClassNameError = exports.MidwayInconsistentVersionError = exports.MidwayUtilHttpClientTimeoutError = exports.MidwayMissingImportComponentError = exports.MidwaySingletonInjectRequestError = exports.MidwayUseWrongMethodError = exports.MidwayDuplicateRouteError = exports.MidwayResolverMissingError = exports.MidwayInvalidConfigError = exports.MidwayConfigMissingError = exports.MidwayFeatureNotImplementedError = exports.MidwayFeatureNoLongerSupportedError = exports.MidwayDefinitionNotFoundError = exports.MidwayParameterError = exports.MidwayCommonError = exports.FrameworkErrorEnum = void 0;
4
4
  const base_1 = require("./base");
5
5
  exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
6
6
  UNKNOWN: 10000,
@@ -20,6 +20,7 @@ exports.FrameworkErrorEnum = (0, base_1.registerErrorCode)('midway', {
20
20
  INVALID_CONFIG: 10014,
21
21
  DUPLICATE_CLASS_NAME: 10015,
22
22
  DUPLICATE_CONTROLLER_PREFIX_OPTIONS: 10016,
23
+ RETRY_OVER_MAX_TIME: 10017,
23
24
  });
24
25
  class MidwayCommonError extends base_1.MidwayError {
25
26
  constructor(message) {
@@ -136,4 +137,12 @@ class MidwayDuplicateControllerOptionsError extends base_1.MidwayError {
136
137
  }
137
138
  }
138
139
  exports.MidwayDuplicateControllerOptionsError = MidwayDuplicateControllerOptionsError;
140
+ class MidwayRetryExceededMaxTimesError extends base_1.MidwayError {
141
+ constructor(methodName, times, err) {
142
+ super(`Invoke "${methodName}" retries exceeded the maximum number of times(${times}), error: ${err.message}`, exports.FrameworkErrorEnum.RETRY_OVER_MAX_TIME, {
143
+ cause: err,
144
+ });
145
+ }
146
+ }
147
+ exports.MidwayRetryExceededMaxTimesError = MidwayRetryExceededMaxTimesError;
139
148
  //# sourceMappingURL=framework.js.map
package/dist/index.d.ts CHANGED
@@ -21,7 +21,7 @@ export { MidwayDecoratorService } from './service/decoratorService';
21
21
  export { MidwayMockService } from './service/mockService';
22
22
  export { RouterInfo, DynamicRouterInfo, RouterPriority, RouterCollectorOptions, MidwayWebRouterService, } from './service/webRouterService';
23
23
  export { MidwayServerlessFunctionService, WebRouterCollector, } from './service/slsFunctionService';
24
- export { DataSourceManager } from './common/dataSourceManager';
24
+ export { CreateDataSourceInstanceOptions, DataSourceManager, } from './common/dataSourceManager';
25
25
  export * from './service/pipelineService';
26
26
  export * from './util/contextUtil';
27
27
  export * from './common/serviceFactory';
@@ -31,6 +31,7 @@ export * from './common/webGenerator';
31
31
  export * from './common/middlewareManager';
32
32
  export * from './util/pathToRegexp';
33
33
  export * from './util/httpclient';
34
+ export * from './util/retry';
34
35
  export * from './common/filterManager';
35
36
  export * from './common/applicationManager';
36
37
  export * from './setup';
package/dist/index.js CHANGED
@@ -77,6 +77,7 @@ __exportStar(require("./common/webGenerator"), exports);
77
77
  __exportStar(require("./common/middlewareManager"), exports);
78
78
  __exportStar(require("./util/pathToRegexp"), exports);
79
79
  __exportStar(require("./util/httpclient"), exports);
80
+ __exportStar(require("./util/retry"), exports);
80
81
  __exportStar(require("./common/filterManager"), exports);
81
82
  __exportStar(require("./common/applicationManager"), exports);
82
83
  __exportStar(require("./setup"), exports);
@@ -0,0 +1,20 @@
1
+ /**
2
+ * wrap async function with retry
3
+ * @param retryFn
4
+ * @param retryTimes
5
+ * @param options
6
+ */
7
+ export declare function retryWithAsync<T extends (...args: any[]) => Promise<unknown>>(retryFn: T, retryTimes?: number, options?: {
8
+ throwOriginError?: boolean;
9
+ retryInterval?: number;
10
+ }): (...args: Parameters<T>) => ReturnType<T>;
11
+ /**
12
+ * wrap sync function with retry
13
+ * @param retryFn
14
+ * @param retryTimes
15
+ * @param options
16
+ */
17
+ export declare function retryWith<T extends (...args: any[]) => unknown>(retryFn: T, retryTimes?: number, options?: {
18
+ throwOriginError?: boolean;
19
+ }): (...args: Parameters<T>) => ReturnType<T>;
20
+ //# sourceMappingURL=retry.d.ts.map
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.retryWith = exports.retryWithAsync = void 0;
4
+ const error_1 = require("../error");
5
+ const decorator_1 = require("@midwayjs/decorator");
6
+ /**
7
+ * wrap async function with retry
8
+ * @param retryFn
9
+ * @param retryTimes
10
+ * @param options
11
+ */
12
+ function retryWithAsync(retryFn, retryTimes = 1, options = {}) {
13
+ let defaultRetry = retryTimes;
14
+ let error;
15
+ return (async (...args) => {
16
+ do {
17
+ try {
18
+ return await retryFn(...args);
19
+ }
20
+ catch (err) {
21
+ error = err;
22
+ }
23
+ if (options.retryInterval >= 0) {
24
+ await (0, decorator_1.sleep)(options.retryInterval);
25
+ }
26
+ } while (defaultRetry-- > 0);
27
+ if (options.throwOriginError) {
28
+ throw error;
29
+ }
30
+ else {
31
+ throw new error_1.MidwayRetryExceededMaxTimesError(retryFn.name || 'anonymous', retryTimes, error);
32
+ }
33
+ });
34
+ }
35
+ exports.retryWithAsync = retryWithAsync;
36
+ /**
37
+ * wrap sync function with retry
38
+ * @param retryFn
39
+ * @param retryTimes
40
+ * @param options
41
+ */
42
+ function retryWith(retryFn, retryTimes = 1, options = {}) {
43
+ let defaultRetry = retryTimes;
44
+ let error;
45
+ return ((...args) => {
46
+ do {
47
+ try {
48
+ return retryFn(...args);
49
+ }
50
+ catch (err) {
51
+ error = err;
52
+ }
53
+ } while (defaultRetry-- > 0);
54
+ if (options.throwOriginError) {
55
+ throw error;
56
+ }
57
+ else {
58
+ throw new error_1.MidwayRetryExceededMaxTimesError(retryFn.name || 'anonymous', retryTimes, error);
59
+ }
60
+ });
61
+ }
62
+ exports.retryWith = retryWith;
63
+ //# sourceMappingURL=retry.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "3.4.13",
3
+ "version": "3.5.0",
4
4
  "description": "midway core",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -25,6 +25,7 @@
25
25
  "koa": "2.13.4",
26
26
  "midway-test-component": "*",
27
27
  "mm": "3.2.0",
28
+ "pg": "^8.8.0",
28
29
  "raw-body": "2.5.1",
29
30
  "sinon": "14.0.0"
30
31
  },
@@ -45,5 +46,5 @@
45
46
  "engines": {
46
47
  "node": ">=12"
47
48
  },
48
- "gitHead": "bde878f01df0a4b27919eea367d100b5ee5edb04"
49
+ "gitHead": "4200e30d1af97a7817c36899c4ffc41a7d97c06d"
49
50
  }