@devbro/pashmak 0.1.30 → 0.1.32
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.
|
@@ -5,7 +5,7 @@ import { PostgresqlConnection } from "@devbro/neko-sql";
|
|
|
5
5
|
import { BaseModel } from "@devbro/neko-orm";
|
|
6
6
|
import { ctx } from "@devbro/neko-context";
|
|
7
7
|
import { config } from "@devbro/neko-config";
|
|
8
|
-
import { Global } from "./global.
|
|
8
|
+
import { Global } from "./global.mjs";
|
|
9
9
|
class DatabaseServiceProvider extends Middleware {
|
|
10
10
|
static {
|
|
11
11
|
__name(this, "DatabaseServiceProvider");
|
|
@@ -21,19 +21,20 @@ class DatabaseServiceProvider extends Middleware {
|
|
|
21
21
|
}
|
|
22
22
|
BaseModel.setConnection(async () => {
|
|
23
23
|
const key = ["database", "default"];
|
|
24
|
-
let rc
|
|
25
|
-
|
|
24
|
+
let rc;
|
|
25
|
+
try {
|
|
26
26
|
rc = ctx().get(key);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
ctx().set(key, rc);
|
|
33
|
-
} catch {
|
|
27
|
+
} catch {
|
|
28
|
+
if (Global.has(key)) {
|
|
29
|
+
rc = Global.get(key);
|
|
30
|
+
} else {
|
|
31
|
+
rc = await this.getConnection(db_configs["default"]);
|
|
34
32
|
Global.set(key, rc);
|
|
35
33
|
}
|
|
36
34
|
}
|
|
35
|
+
if (!rc) {
|
|
36
|
+
throw new Error("unable to start default database connection");
|
|
37
|
+
}
|
|
37
38
|
return rc;
|
|
38
39
|
});
|
|
39
40
|
await next();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/DatabaseServiceProvider.mts"],"sourcesContent":["import { Middleware } from \"@devbro/neko-router\";\nimport { Request, Response } from \"@devbro/neko-router\";\nimport { PostgresqlConnection } from \"@devbro/neko-sql\";\nimport { PoolConfig } from \"pg\";\nimport { Connection } from \"@devbro/neko-sql\";\nimport { BaseModel } from \"@devbro/neko-orm\";\nimport { ctx } from \"@devbro/neko-context\";\nimport { config } from \"@devbro/neko-config\";\nimport { Global } from \"./global.
|
|
1
|
+
{"version":3,"sources":["../src/DatabaseServiceProvider.mts"],"sourcesContent":["import { Middleware } from \"@devbro/neko-router\";\nimport { Request, Response } from \"@devbro/neko-router\";\nimport { PostgresqlConnection } from \"@devbro/neko-sql\";\nimport { PoolConfig } from \"pg\";\nimport { Connection } from \"@devbro/neko-sql\";\nimport { BaseModel } from \"@devbro/neko-orm\";\nimport { ctx } from \"@devbro/neko-context\";\nimport { config } from \"@devbro/neko-config\";\nimport { Global } from \"./global.mjs\";\n\nexport class DatabaseServiceProvider extends Middleware {\n async call(\n req: Request,\n res: Response,\n next: () => Promise<void>,\n ): Promise<void> {\n const db_configs: Record<string, PoolConfig & { name: string }> =\n config.get(\"databases\");\n\n const conns = [];\n try {\n for (const [name, db_config] of Object.entries(db_configs)) {\n const conn = await this.getConnection(db_config);\n ctx().set([\"database\", name], conn);\n conns.push(conn);\n }\n BaseModel.setConnection(async () => {\n const key = [\"database\", \"default\"];\n let rc: Connection | undefined;\n\n try {\n rc = ctx().get<Connection>(key);\n } catch {\n // context is not active\n if (Global.has(key)) {\n rc = Global.get<Connection>(key);\n } else {\n rc = await this.getConnection(db_configs[\"default\"]);\n Global.set(key, rc);\n }\n }\n\n if (!rc) {\n throw new Error(\"unable to start default database connection\");\n }\n\n return rc!;\n });\n await next();\n } finally {\n for (const conn of conns) {\n await conn.disconnect();\n }\n }\n }\n\n private static instance: DatabaseServiceProvider;\n\n async register(): Promise<void> {}\n\n static getInstance(): DatabaseServiceProvider {\n if (!DatabaseServiceProvider.instance) {\n DatabaseServiceProvider.instance = new DatabaseServiceProvider();\n }\n return DatabaseServiceProvider.instance;\n }\n\n async getConnection(db_config: PoolConfig): Promise<PostgresqlConnection> {\n const conn = new PostgresqlConnection(db_config);\n if (!(await conn.connect())) {\n throw new Error(\"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ACjE;AACA,WAAO,wBAAwB;AAAA,EACjC;AAAA,EAEA,MAAM,cAAc,WAAsD;AACxE,UAAM,OAAO,IAAI,qBAAqB,SAAS;AAC/C,QAAI,CAAE,MAAM,KAAK,QAAQ,GAAI;AAC3B,YAAM,IAAI,MAAM,mCAAmC;AAAA,IACrD;AACA,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -100,19 +100,20 @@ var DatabaseServiceProvider = class _DatabaseServiceProvider extends Middleware
|
|
|
100
100
|
}
|
|
101
101
|
import_neko_orm.BaseModel.setConnection(async () => {
|
|
102
102
|
const key = ["database", "default"];
|
|
103
|
-
let rc
|
|
104
|
-
|
|
103
|
+
let rc;
|
|
104
|
+
try {
|
|
105
105
|
rc = (0, import_neko_context.ctx)().get(key);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
(0, import_neko_context.ctx)().set(key, rc);
|
|
112
|
-
} catch {
|
|
106
|
+
} catch {
|
|
107
|
+
if (Global.has(key)) {
|
|
108
|
+
rc = Global.get(key);
|
|
109
|
+
} else {
|
|
110
|
+
rc = await this.getConnection(db_configs["default"]);
|
|
113
111
|
Global.set(key, rc);
|
|
114
112
|
}
|
|
115
113
|
}
|
|
114
|
+
if (!rc) {
|
|
115
|
+
throw new Error("unable to start default database connection");
|
|
116
|
+
}
|
|
116
117
|
return rc;
|
|
117
118
|
});
|
|
118
119
|
await next();
|
package/dist/bin/index.cjs
CHANGED
|
@@ -2906,19 +2906,20 @@ var init_DatabaseServiceProvider = __esm({
|
|
|
2906
2906
|
}
|
|
2907
2907
|
import_neko_orm.BaseModel.setConnection(async () => {
|
|
2908
2908
|
const key = ["database", "default"];
|
|
2909
|
-
let rc
|
|
2910
|
-
|
|
2909
|
+
let rc;
|
|
2910
|
+
try {
|
|
2911
2911
|
rc = (0, import_neko_context6.ctx)().get(key);
|
|
2912
|
-
}
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
(0, import_neko_context6.ctx)().set(key, rc);
|
|
2918
|
-
} catch {
|
|
2912
|
+
} catch {
|
|
2913
|
+
if (Global.has(key)) {
|
|
2914
|
+
rc = Global.get(key);
|
|
2915
|
+
} else {
|
|
2916
|
+
rc = await this.getConnection(db_configs["default"]);
|
|
2919
2917
|
Global.set(key, rc);
|
|
2920
2918
|
}
|
|
2921
2919
|
}
|
|
2920
|
+
if (!rc) {
|
|
2921
|
+
throw new Error("unable to start default database connection");
|
|
2922
|
+
}
|
|
2922
2923
|
return rc;
|
|
2923
2924
|
});
|
|
2924
2925
|
await next();
|