skyeye-svc-common-utils 2.0.0-dev0.2 → 2.0.0-dev0.21
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/index.d.ts +10 -7
- package/dist/index.js +10 -7
- package/dist/index.js.map +1 -1
- package/dist/lib/azure/azureKeyVault.d.ts +10 -0
- package/dist/lib/azure/azureKeyVault.js +39 -1
- package/dist/lib/azure/azureKeyVault.js.map +1 -1
- package/dist/lib/azure/cognitiveSearch/QueryType.d.ts +8 -0
- package/dist/lib/azure/cognitiveSearch/QueryType.js +13 -0
- package/dist/lib/azure/cognitiveSearch/QueryType.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchClient.d.ts +42 -0
- package/dist/lib/azure/cognitiveSearch/SearchClient.js +161 -0
- package/dist/lib/azure/cognitiveSearch/SearchClient.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchFacets.d.ts +16 -0
- package/dist/lib/azure/cognitiveSearch/SearchFacets.js +3 -0
- package/dist/lib/azure/cognitiveSearch/SearchFacets.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchFilter.d.ts +68 -0
- package/dist/lib/azure/cognitiveSearch/SearchFilter.js +125 -0
- package/dist/lib/azure/cognitiveSearch/SearchFilter.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchMode.d.ts +9 -0
- package/dist/lib/azure/cognitiveSearch/SearchMode.js +14 -0
- package/dist/lib/azure/cognitiveSearch/SearchMode.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchOption.d.ts +88 -0
- package/dist/lib/azure/cognitiveSearch/SearchOption.js +104 -0
- package/dist/lib/azure/cognitiveSearch/SearchOption.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/SearchOrder.d.ts +9 -0
- package/dist/lib/azure/cognitiveSearch/SearchOrder.js +3 -0
- package/dist/lib/azure/cognitiveSearch/SearchOrder.js.map +1 -0
- package/dist/lib/azure/cognitiveSearch/index.d.ts +7 -0
- package/dist/lib/azure/cognitiveSearch/index.js +24 -0
- package/dist/lib/azure/cognitiveSearch/index.js.map +1 -0
- package/dist/lib/azure/index.d.ts +7 -0
- package/dist/lib/azure/index.js +24 -0
- package/dist/lib/azure/index.js.map +1 -0
- package/dist/lib/fetch/index.d.ts +2 -0
- package/dist/lib/fetch/index.js +19 -0
- package/dist/lib/fetch/index.js.map +1 -0
- package/dist/lib/fetch/sendRequest.d.ts +8 -0
- package/dist/lib/fetch/sendRequest.js +25 -0
- package/dist/lib/fetch/sendRequest.js.map +1 -0
- package/dist/lib/fetch/sendRequestWithTimeout.d.ts +9 -0
- package/dist/lib/fetch/sendRequestWithTimeout.js +53 -0
- package/dist/lib/fetch/sendRequestWithTimeout.js.map +1 -0
- package/dist/lib/httpClient.d.ts +1 -0
- package/dist/lib/httpClient.js +18 -14
- package/dist/lib/httpClient.js.map +1 -1
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +20 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/teams/broadcastLogMessage.d.ts +8 -0
- package/dist/lib/teams/broadcastLogMessage.js +81 -0
- package/dist/lib/teams/broadcastLogMessage.js.map +1 -0
- package/dist/lib/teams/index.d.ts +2 -0
- package/dist/lib/teams/index.js +19 -0
- package/dist/lib/teams/index.js.map +1 -0
- package/dist/lib/teams/logMessage.d.ts +51 -0
- package/dist/lib/teams/logMessage.js +33 -0
- package/dist/lib/teams/logMessage.js.map +1 -0
- package/dist/utils/appConst.d.ts +57 -0
- package/dist/utils/appConst.js +56 -1
- package/dist/utils/appConst.js.map +1 -1
- package/dist/utils/baseClass/appDataSource.d.ts +7 -0
- package/dist/utils/baseClass/appDataSource.js +31 -0
- package/dist/utils/baseClass/appDataSource.js.map +1 -0
- package/dist/utils/baseClass/baseController.js +8 -8
- package/dist/utils/baseClass/baseController.js.map +1 -1
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +19 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logger/morganLogger.d.ts +2 -4
- package/dist/utils/logger/morganLogger.js +3 -3
- package/dist/utils/logger/morganLogger.js.map +1 -1
- package/dist/utils/middleware/loadConfig.d.ts +128 -0
- package/dist/utils/middleware/loadConfig.js +143 -0
- package/dist/utils/middleware/loadConfig.js.map +1 -0
- package/package.json +26 -23
- package/src/index.ts +10 -7
- package/src/lib/azure/azureKeyVault.ts +44 -0
- package/src/lib/azure/cognitiveSearch/QueryType.ts +8 -0
- package/src/lib/azure/cognitiveSearch/SearchClient.ts +149 -0
- package/src/lib/azure/cognitiveSearch/SearchFacets.ts +16 -0
- package/src/lib/azure/cognitiveSearch/SearchFilter.ts +140 -0
- package/src/lib/azure/cognitiveSearch/SearchMode.ts +9 -0
- package/src/lib/azure/cognitiveSearch/SearchOption.ts +155 -0
- package/src/lib/azure/cognitiveSearch/SearchOrder.ts +9 -0
- package/src/lib/azure/cognitiveSearch/index.ts +7 -0
- package/src/lib/azure/index.ts +7 -0
- package/src/lib/fetch/index.ts +2 -0
- package/src/lib/fetch/sendRequest.ts +10 -0
- package/src/lib/fetch/sendRequestWithTimeout.ts +38 -0
- package/src/lib/httpClient.ts +27 -26
- package/src/lib/index.ts +3 -0
- package/src/lib/teams/broadcastLogMessage.ts +70 -0
- package/src/lib/teams/index.ts +2 -0
- package/src/lib/teams/logMessage.ts +55 -0
- package/src/utils/appConfig.ts +2 -2
- package/src/utils/appConst.ts +64 -0
- package/src/utils/baseClass/appDataSource.ts +23 -0
- package/src/utils/baseClass/baseController.ts +9 -9
- package/src/utils/index.ts +2 -0
- package/src/utils/logger/morganLogger.ts +2 -2
- package/src/utils/middleware/loadConfig.ts +150 -0
- package/.env +0 -3
- package/dist/utils/baseClass/dataSourceManager.d.ts +0 -11
- package/dist/utils/baseClass/dataSourceManager.js +0 -54
- package/dist/utils/baseClass/dataSourceManager.js.map +0 -1
- package/dist/utils/middleware/startdbConnection.d.ts +0 -1
- package/dist/utils/middleware/startdbConnection.js +0 -30
- package/dist/utils/middleware/startdbConnection.js.map +0 -1
- package/src/utils/baseClass/dataSourceManager.ts +0 -44
- package/src/utils/middleware/startdbConnection.ts +0 -20
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DataSource, DataSourceOptions } from 'typeorm';
|
|
2
|
+
import { logger } from '../logger/logger';
|
|
3
|
+
|
|
4
|
+
export class AppDataSource {
|
|
5
|
+
|
|
6
|
+
private static instance: DataSource;
|
|
7
|
+
|
|
8
|
+
private constructor() {}
|
|
9
|
+
|
|
10
|
+
public static getInstance() {
|
|
11
|
+
return AppDataSource.instance;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public static async startConnection(options: DataSourceOptions) {
|
|
15
|
+
if (!AppDataSource.instance) {
|
|
16
|
+
AppDataSource.instance = new DataSource(options);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!AppDataSource.instance.isInitialized) {
|
|
20
|
+
await AppDataSource.instance.initialize();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -7,7 +7,7 @@ import { QueryRunner } from 'typeorm';
|
|
|
7
7
|
import { commonAppConst } from '../appConst';
|
|
8
8
|
import { commonAppConfig } from '../appConfig';
|
|
9
9
|
import { Mutex } from 'async-mutex';
|
|
10
|
-
import {
|
|
10
|
+
import { AppDataSource } from './appDataSource';
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
export abstract class BaseController {
|
|
@@ -18,8 +18,8 @@ export abstract class BaseController {
|
|
|
18
18
|
let queryRunner: QueryRunner;
|
|
19
19
|
|
|
20
20
|
try {
|
|
21
|
-
|
|
22
|
-
queryRunner =
|
|
21
|
+
// Use queryRunner if transaction is needed
|
|
22
|
+
queryRunner = AppDataSource.getInstance().createQueryRunner();
|
|
23
23
|
await queryRunner.startTransaction();
|
|
24
24
|
|
|
25
25
|
return Promise.resolve(queryRunner);
|
|
@@ -59,12 +59,12 @@ export abstract class BaseController {
|
|
|
59
59
|
} catch (err) {
|
|
60
60
|
logger.error(`BaseController/endTransaction: ${err}`);
|
|
61
61
|
} finally {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} catch (err) {
|
|
66
|
-
logger.error(`BaseController/endTransaction Release Error: ${err}`);
|
|
62
|
+
try {
|
|
63
|
+
if (!baseControllerParameter.queryRunner.isReleased) {
|
|
64
|
+
await baseControllerParameter.queryRunner.release();
|
|
67
65
|
}
|
|
66
|
+
} catch (err) {
|
|
67
|
+
logger.error(`BaseController/endTransaction Release Error: ${err}`);
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
}
|
|
@@ -149,7 +149,7 @@ export abstract class BaseController {
|
|
|
149
149
|
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
150
150
|
baseControllerParameter.serviceResponse.error = true;
|
|
151
151
|
baseControllerParameter.serviceResponse.message = err.message;
|
|
152
|
-
|
|
152
|
+
|
|
153
153
|
throw err;
|
|
154
154
|
});
|
|
155
155
|
}
|
|
@@ -7,7 +7,7 @@ import dateformat from 'dateformat';
|
|
|
7
7
|
// ensure log directory exists
|
|
8
8
|
fs.existsSync(commonAppConfig.LogPath) || fs.mkdirSync(commonAppConfig.LogPath)
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const FileNameGenerator = (time: number | Date, index: number) =>{
|
|
11
11
|
if (!time)
|
|
12
12
|
{
|
|
13
13
|
time = new Date();
|
|
@@ -18,7 +18,7 @@ function FileNameGenerator(time:Date, index:number){
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
// create a rotating write stream
|
|
21
|
-
var accessLogStream = rfs(FileNameGenerator, {
|
|
21
|
+
var accessLogStream = rfs.createStream(FileNameGenerator, {
|
|
22
22
|
interval: '1d', // rotate daily
|
|
23
23
|
path: commonAppConfig.LogPath
|
|
24
24
|
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
import { getKeyVault } from '../../lib';
|
|
3
|
+
import { logger } from '../logger/logger';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { zEnvironment, Environment } from '../appConst';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @description
|
|
9
|
+
* @property {string} appName - service's app name from package.json
|
|
10
|
+
* @property {string} version - service's version from package.json
|
|
11
|
+
* @property {enum} environment - current environment
|
|
12
|
+
* @property {string} keyVaultName - Azure key vault name
|
|
13
|
+
* @property {string} keyVaultSecretName - secret name of Azure key vault
|
|
14
|
+
* @todo migration to single typedef
|
|
15
|
+
*/
|
|
16
|
+
const zEnvFileAppConfig = z.object({
|
|
17
|
+
appName: z.string(),
|
|
18
|
+
version: z.string(),
|
|
19
|
+
environment: zEnvironment,
|
|
20
|
+
keyVaultName: z.string(),
|
|
21
|
+
keyVaultSecretName: z.string()
|
|
22
|
+
}).strict();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @description typedef of environment File App Config
|
|
26
|
+
*/
|
|
27
|
+
type EnvFileAppConfig = z.infer<typeof zEnvFileAppConfig>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @todo migration to single typedef
|
|
31
|
+
*/
|
|
32
|
+
const zElasticKeyVault = z.object({
|
|
33
|
+
secretToken: z.string(),
|
|
34
|
+
serverUrl: z.string()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @todo migration to single typedef
|
|
39
|
+
*/
|
|
40
|
+
const zTeamsChannelWebHook = z.object({
|
|
41
|
+
errorChannel: z.string(),
|
|
42
|
+
warnChannel: z.string()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @todo migration to single typedef
|
|
47
|
+
*/
|
|
48
|
+
const zTelemetry = z.object({
|
|
49
|
+
teamsChannelWebHook: zTeamsChannelWebHook,
|
|
50
|
+
elasticKeyVault: zElasticKeyVault
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @todo migration to single typedef
|
|
55
|
+
*/
|
|
56
|
+
const zSecretFileAppConfig = z.object({
|
|
57
|
+
telemetry: zTelemetry,
|
|
58
|
+
serviceConfig: z.any().nullable()
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @description typedef of secret file
|
|
63
|
+
*/
|
|
64
|
+
type SecretFileAppConfig = z.infer<typeof zSecretFileAppConfig>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @todo migration to single typedef
|
|
68
|
+
*/
|
|
69
|
+
type AppConfig = EnvFileAppConfig & SecretFileAppConfig;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @description configuration carrier (or, instance). We can classified all the configurations into 2 categories:
|
|
73
|
+
* - Environment File App Config: configuration from the environment file (.env) and some other settings
|
|
74
|
+
* - Secret File App Config: loaded from the Azure Key Vault
|
|
75
|
+
*/
|
|
76
|
+
export let appConfig: AppConfig;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @todo move to loadConfig since all the environment fields should be loaded from appConfig
|
|
80
|
+
*/
|
|
81
|
+
export const environment: Environment = (process.env.NODE_ENV as Environment) ?? 'development';
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @description preload for app configuration, once it is been called, the appConfig will be loaded and be freezed
|
|
85
|
+
* @param packagePath - optional, path of `package.json` in order to load app name and version
|
|
86
|
+
*/
|
|
87
|
+
export const loadConfig = async (packagePath?: string) => {
|
|
88
|
+
logger.verbose('start /appConfig/preload()');
|
|
89
|
+
|
|
90
|
+
// load environment file
|
|
91
|
+
dotenv.config({ path: `${environment}.env` });
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* To load the appName and the version, we read the package information in the following order:
|
|
95
|
+
* 1. check if the environment value is defined directly
|
|
96
|
+
* 2. check if environment given from npm is defined
|
|
97
|
+
* 3. check if value in package.json is defined
|
|
98
|
+
*/
|
|
99
|
+
const pkg = require(packagePath ?? `${process.env.PWD}/package.json`);
|
|
100
|
+
|
|
101
|
+
const appName: string = ((): string => {
|
|
102
|
+
if (!!process.env.APP_NAME) { return process.env.APP_NAME; }
|
|
103
|
+
if (!!process.env.npm_package_name) { return process.env.npm_package_name; }
|
|
104
|
+
if (!!pkg.name) { return pkg.name; }
|
|
105
|
+
return 'default-app-name';
|
|
106
|
+
})();
|
|
107
|
+
|
|
108
|
+
const version: string = ((): string => {
|
|
109
|
+
if (!!process.env.VERSION) { return process.env.VERSION; }
|
|
110
|
+
if (!!process.env.npm_package_version) { return process.env.npm_package_version; }
|
|
111
|
+
if (!!pkg.version) { return pkg.version; }
|
|
112
|
+
return '0.0.0';
|
|
113
|
+
})();
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* check the env which suppose to be hide as possible as it can,
|
|
117
|
+
* which is not allowed to define in-memory.
|
|
118
|
+
*/
|
|
119
|
+
if (process.env.AZURE_TENANT_ID === undefined || process.env.AZURE_CLIENT_ID === undefined || process.env.AZURE_CLIENT_SECRET === undefined) {
|
|
120
|
+
throw new Error('/appConfig/preload: missing Azure configuration');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const envFileAppConfig: EnvFileAppConfig = {
|
|
124
|
+
appName,
|
|
125
|
+
version,
|
|
126
|
+
environment,
|
|
127
|
+
keyVaultName: process.env.KEYVAULT_NAME,
|
|
128
|
+
keyVaultSecretName: process.env.LIBRARY_KEY_VAULT_SECRET_NAME
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// check envFileAppConfig
|
|
132
|
+
const envTypeCheck = zEnvFileAppConfig.safeParse(envFileAppConfig);
|
|
133
|
+
if (!envTypeCheck.success) {
|
|
134
|
+
throw new Error('/appConfig/preload: invalid schema of envFileAppConfig');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// check secretFileAppConfig
|
|
138
|
+
const rawSecretResp = await getKeyVault(envFileAppConfig.keyVaultName, envFileAppConfig.keyVaultSecretName);
|
|
139
|
+
if (!rawSecretResp.isSuccess) { throw new Error(rawSecretResp.message); }
|
|
140
|
+
const secretFileAppConfig: SecretFileAppConfig = rawSecretResp.data;
|
|
141
|
+
const secretFileTypeCheck = zSecretFileAppConfig.safeParse(secretFileAppConfig);
|
|
142
|
+
if (!secretFileTypeCheck.success) {
|
|
143
|
+
throw new Error('/appConfig/preload: invalid schema of secretFileAppConfig');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
appConfig = { ...envFileAppConfig, ...secretFileAppConfig };
|
|
147
|
+
Object.freeze(appConfig);
|
|
148
|
+
|
|
149
|
+
logger.verbose('end /appConfig/preload()');
|
|
150
|
+
};
|
package/.env
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { DataSource, DataSourceOptions } from 'typeorm';
|
|
2
|
-
export declare class DataSourceManager {
|
|
3
|
-
private static instance;
|
|
4
|
-
private dataSources;
|
|
5
|
-
private constructor();
|
|
6
|
-
static getInstance(): DataSourceManager;
|
|
7
|
-
createDataSource(name: string, options?: DataSourceOptions): Promise<DataSource>;
|
|
8
|
-
getDataSource(dataSourceName: string): DataSource;
|
|
9
|
-
}
|
|
10
|
-
export declare function getDataSource(dataSourceName?: string): DataSource;
|
|
11
|
-
export declare function createDataSource(options?: DataSourceOptions, name?: string): Promise<DataSource>;
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.createDataSource = exports.getDataSource = exports.DataSourceManager = void 0;
|
|
13
|
-
const typeorm_1 = require("typeorm");
|
|
14
|
-
// to be used instead of getConnectionManager()
|
|
15
|
-
class DataSourceManager {
|
|
16
|
-
constructor() {
|
|
17
|
-
this.dataSources = new Map();
|
|
18
|
-
}
|
|
19
|
-
static getInstance() {
|
|
20
|
-
if (!DataSourceManager.instance) {
|
|
21
|
-
DataSourceManager.instance = new DataSourceManager();
|
|
22
|
-
}
|
|
23
|
-
return DataSourceManager.instance;
|
|
24
|
-
}
|
|
25
|
-
createDataSource(name, options) {
|
|
26
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
-
const _options = options;
|
|
28
|
-
return new Promise((resolve, reject) => {
|
|
29
|
-
new typeorm_1.DataSource(_options).initialize()
|
|
30
|
-
.then((dataSource) => {
|
|
31
|
-
this.dataSources.set(name, dataSource).get(name);
|
|
32
|
-
resolve(dataSource);
|
|
33
|
-
})
|
|
34
|
-
.catch((e) => {
|
|
35
|
-
reject(e);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
getDataSource(dataSourceName) {
|
|
41
|
-
return this.dataSources.get(dataSourceName || 'default');
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.DataSourceManager = DataSourceManager;
|
|
45
|
-
// to be used instead of getConnection()
|
|
46
|
-
function getDataSource(dataSourceName) {
|
|
47
|
-
return DataSourceManager.getInstance().getDataSource(dataSourceName || 'default');
|
|
48
|
-
}
|
|
49
|
-
exports.getDataSource = getDataSource;
|
|
50
|
-
function createDataSource(options, name = 'default') {
|
|
51
|
-
return DataSourceManager.getInstance().createDataSource(name, options);
|
|
52
|
-
}
|
|
53
|
-
exports.createDataSource = createDataSource;
|
|
54
|
-
//# sourceMappingURL=dataSourceManager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dataSourceManager.js","sourceRoot":"","sources":["../../../src/utils/baseClass/dataSourceManager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAAiG;AAEjG,+CAA+C;AAC/C,MAAa,iBAAiB;IAI1B;QAFQ,gBAAW,GAA4B,IAAI,GAAG,EAAE,CAAC;IAElC,CAAC;IAEjB,MAAM,CAAC,WAAW;QACrB,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE,CAAC;YAClC,iBAAiB,CAAC,QAAQ,GAAG,IAAI,iBAAiB,EAAE,CAAC;QACrD,CAAC;QACD,OAAO,iBAAiB,CAAC,QAAQ,CAAC;IACtC,CAAC;IAEY,gBAAgB,CAAC,IAAY,EAAE,OAA2B;;YACnE,MAAM,QAAQ,GAAG,OAAO,CAAC;YACzB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACnC,IAAI,oBAAU,CAAC,QAAQ,CAAC,CAAC,UAAU,EAAE;qBACpC,IAAI,CAAC,CAAC,UAAsB,EAAE,EAAE;oBAC7B,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;oBACjD,OAAO,CAAC,UAAU,CAAC,CAAC;gBACxB,CAAC,CAAC;qBACD,KAAK,CAAC,CAAC,CAAM,EAAE,EAAE;oBACd,MAAM,CAAC,CAAC,CAAC,CAAC;gBACd,CAAC,CAAC,CAAA;YACN,CAAC,CAAC,CAAC;QACP,CAAC;KAAA;IAEM,aAAa,CAAC,cAAsB;QACvC,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,cAAc,IAAI,SAAS,CAAC,CAAC;IAC7D,CAAC;CAEJ;AA/BD,8CA+BC;AAED,wCAAwC;AACxC,SAAgB,aAAa,CAAC,cAAuB;IACjD,OAAO,iBAAiB,CAAC,WAAW,EAAE,CAAC,aAAa,CAAC,cAAc,IAAI,SAAS,CAAC,CAAC;AACtF,CAAC;AAFD,sCAEC;AAED,SAAgB,gBAAgB,CAAC,OAA2B,EAAE,OAAe,SAAS;IAClF,OAAO,iBAAiB,CAAC,WAAW,EAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAC3E,CAAC;AAFD,4CAEC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function startConnection(entities: any[]): Promise<void>;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.startConnection = void 0;
|
|
13
|
-
const typeorm_1 = require("typeorm");
|
|
14
|
-
const dataSourceManager_1 = require("../baseClass/dataSourceManager");
|
|
15
|
-
const appConfig_1 = require("../appConfig");
|
|
16
|
-
const azureKeyVault_1 = require("../../lib/azure/azureKeyVault");
|
|
17
|
-
const logger_1 = require("../logger/logger");
|
|
18
|
-
function startConnection(entities) {
|
|
19
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
const options = Object.assign(Object.assign({}, JSON.parse(yield (0, azureKeyVault_1.getKey)(appConfig_1.commonAppConfig.SqlConnectionConfigKey))), { synchronize: false, entities: entities });
|
|
21
|
-
const myDataSource = new typeorm_1.DataSource(options);
|
|
22
|
-
if (appConfig_1.commonAppConfig.SqlEnable) {
|
|
23
|
-
(0, dataSourceManager_1.createDataSource)(options).then(() => {
|
|
24
|
-
logger_1.logger.info("TypeORM connected");
|
|
25
|
-
}).catch(error => logger_1.logger.info("TypeORM connection error: ", error));
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
exports.startConnection = startConnection;
|
|
30
|
-
//# sourceMappingURL=startdbConnection.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"startdbConnection.js","sourceRoot":"","sources":["../../../src/utils/middleware/startdbConnection.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qCAAwD;AACxD,sEAAkE;AAClE,4CAA+C;AAC/C,iEAAuD;AACvD,6CAA0C;AAE1C,SAAsB,eAAe,CAAC,QAAe;;QACjD,MAAM,OAAO,mCAEN,IAAI,CAAC,KAAK,CAAC,MAAM,IAAA,sBAAM,EAAC,2BAAe,CAAC,sBAAsB,CAAC,CAAC,KACnE,WAAW,EAAE,KAAK,EAClB,QAAQ,EAAE,QAAQ,GACrB,CAAC;QACF,MAAM,YAAY,GAAG,IAAI,oBAAU,CAAC,OAAO,CAAC,CAAC;QAC7C,IAAI,2BAAe,CAAC,SAAS,EAAE,CAAC;YAC5B,IAAA,oCAAgB,EAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;gBAChC,eAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;YACrC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,eAAM,CAAC,IAAI,CAAC,4BAA4B,EAAE,KAAK,CAAC,CAAC,CAAC;QACxE,CAAC;IACL,CAAC;CAAA;AAbD,0CAaC"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { DataSource, DataSourceOptions, EntityTarget, ObjectLiteral, Repository } from 'typeorm';
|
|
2
|
-
|
|
3
|
-
// to be used instead of getConnectionManager()
|
|
4
|
-
export class DataSourceManager {
|
|
5
|
-
private static instance: DataSourceManager;
|
|
6
|
-
private dataSources: Map<string, DataSource> = new Map();
|
|
7
|
-
|
|
8
|
-
private constructor() {}
|
|
9
|
-
|
|
10
|
-
public static getInstance(): DataSourceManager {
|
|
11
|
-
if (!DataSourceManager.instance) {
|
|
12
|
-
DataSourceManager.instance = new DataSourceManager();
|
|
13
|
-
}
|
|
14
|
-
return DataSourceManager.instance;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public async createDataSource(name: string, options?: DataSourceOptions): Promise<DataSource> {
|
|
18
|
-
const _options = options;
|
|
19
|
-
return new Promise((resolve, reject) => {
|
|
20
|
-
new DataSource(_options).initialize()
|
|
21
|
-
.then((dataSource: DataSource) => {
|
|
22
|
-
this.dataSources.set(name, dataSource).get(name);
|
|
23
|
-
resolve(dataSource);
|
|
24
|
-
})
|
|
25
|
-
.catch((e: any) => {
|
|
26
|
-
reject(e);
|
|
27
|
-
})
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public getDataSource(dataSourceName: string): DataSource {
|
|
32
|
-
return this.dataSources.get(dataSourceName || 'default');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// to be used instead of getConnection()
|
|
38
|
-
export function getDataSource(dataSourceName?: string): DataSource {
|
|
39
|
-
return DataSourceManager.getInstance().getDataSource(dataSourceName || 'default');
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function createDataSource(options?: DataSourceOptions, name: string = 'default') {
|
|
43
|
-
return DataSourceManager.getInstance().createDataSource(name, options);
|
|
44
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { DataSourceOptions, DataSource } from 'typeorm';
|
|
2
|
-
import { createDataSource } from '../baseClass/dataSourceManager';
|
|
3
|
-
import { commonAppConfig } from '../appConfig';
|
|
4
|
-
import { getKey } from '../../lib/azure/azureKeyVault';
|
|
5
|
-
import { logger } from '../logger/logger';
|
|
6
|
-
|
|
7
|
-
export async function startConnection(entities: any[]) {
|
|
8
|
-
const options: DataSourceOptions =
|
|
9
|
-
{
|
|
10
|
-
...JSON.parse(await getKey(commonAppConfig.SqlConnectionConfigKey)),
|
|
11
|
-
synchronize: false,
|
|
12
|
-
entities: entities
|
|
13
|
-
};
|
|
14
|
-
const myDataSource = new DataSource(options);
|
|
15
|
-
if (commonAppConfig.SqlEnable) {
|
|
16
|
-
createDataSource(options).then(() => {
|
|
17
|
-
logger.info("TypeORM connected");
|
|
18
|
-
}).catch(error => logger.info("TypeORM connection error: ", error));
|
|
19
|
-
}
|
|
20
|
-
}
|