@alanszp/serverless 6.0.1 → 6.0.4
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.
|
@@ -11,11 +11,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.initializeDBMiddleware = void 0;
|
|
13
13
|
const typeorm_1 = require("typeorm");
|
|
14
|
+
function hasConnection(name) {
|
|
15
|
+
try {
|
|
16
|
+
return Boolean((0, typeorm_1.getConnection)(name));
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
14
22
|
function initializeDBMiddleware(connectionOptions) {
|
|
15
23
|
return {
|
|
16
|
-
before: (
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
before: () => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
if (!hasConnection(connectionOptions.name)) {
|
|
26
|
+
yield (0, typeorm_1.createConnection)(connectionOptions);
|
|
27
|
+
}
|
|
19
28
|
}),
|
|
20
29
|
};
|
|
21
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initializeDBMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/initializeDBMiddleware.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,
|
|
1
|
+
{"version":3,"file":"initializeDBMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/initializeDBMiddleware.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,qCAA6E;AAE7E,SAAS,aAAa,CAAC,IAAwB;IAC7C,IAAI;QACF,OAAO,OAAO,CAAC,IAAA,uBAAa,EAAC,IAAI,CAAC,CAAC,CAAC;KACrC;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAED,SAAgB,sBAAsB,CACpC,iBAAoC;IAEpC,OAAO;QACL,MAAM,EAAE,GAAwB,EAAE;YAChC,IAAI,CAAC,aAAa,CAAC,iBAAiB,CAAC,IAAI,CAAC,EAAE;gBAC1C,MAAM,IAAA,0BAAgB,EAAC,iBAAiB,CAAC,CAAC;aAC3C;QACH,CAAC,CAAA;KACF,CAAC;AACJ,CAAC;AAVD,wDAUC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alanszp/serverless",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.4",
|
|
4
4
|
"description": "Alan's serverless utils and middlewares.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@alanszp/core": "^6.0.0",
|
|
36
|
-
"@alanszp/shared-context": "^6.0.
|
|
36
|
+
"@alanszp/shared-context": "^6.0.4",
|
|
37
37
|
"@alanszp/typeorm": "^6.0.0",
|
|
38
38
|
"@middy/core": "^3.0.3",
|
|
39
39
|
"@middy/util": "^3.0.3",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"cuid": "^2.1.8",
|
|
42
42
|
"lodash": "^4.17.21"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "f11faa4b3583d683efc45e30d0817e4d3a9786a2"
|
|
45
45
|
}
|
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import { MiddlewareObj } from "@middy/core";
|
|
2
|
-
import { createConnection, ConnectionOptions } from "typeorm";
|
|
2
|
+
import { createConnection, getConnection, ConnectionOptions } from "typeorm";
|
|
3
|
+
|
|
4
|
+
function hasConnection(name: string | undefined): boolean {
|
|
5
|
+
try {
|
|
6
|
+
return Boolean(getConnection(name));
|
|
7
|
+
} catch (error) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
3
11
|
|
|
4
12
|
export function initializeDBMiddleware<TEvent, TResult>(
|
|
5
13
|
connectionOptions: ConnectionOptions
|
|
6
14
|
): MiddlewareObj<TEvent, TResult> {
|
|
7
15
|
return {
|
|
8
|
-
before: async (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
16
|
+
before: async (): Promise<void> => {
|
|
17
|
+
if (!hasConnection(connectionOptions.name)) {
|
|
18
|
+
await createConnection(connectionOptions);
|
|
19
|
+
}
|
|
12
20
|
},
|
|
13
21
|
};
|
|
14
22
|
}
|