@devbro/pashmak 0.1.32 → 0.1.34
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.
|
@@ -22,18 +22,13 @@ class DatabaseServiceProvider extends Middleware {
|
|
|
22
22
|
BaseModel.setConnection(async () => {
|
|
23
23
|
const key = ["database", "default"];
|
|
24
24
|
let rc;
|
|
25
|
-
|
|
25
|
+
if (ctx.isActive()) {
|
|
26
26
|
rc = ctx().get(key);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
Global.set(key, rc);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (!rc) {
|
|
36
|
-
throw new Error("unable to start default database connection");
|
|
27
|
+
} else if (Global.has(key)) {
|
|
28
|
+
rc = Global.get(key);
|
|
29
|
+
} else {
|
|
30
|
+
rc = await this.getConnection(db_configs["default"]);
|
|
31
|
+
Global.set(key, rc);
|
|
37
32
|
}
|
|
38
33
|
return rc;
|
|
39
34
|
});
|
|
@@ -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.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
|
|
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 if (ctx.isActive()) {\n rc = ctx().get<Connection>(key);\n } else 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 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(\"Failed to connect to the database\");\n }\n return conn;\n }\n}\n"],"mappings":";;AAAA,SAAS,kBAAkB;AAE3B,SAAS,4BAA4B;AAGrC,SAAS,iBAAiB;AAC1B,SA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D;AACA,WAAO;AAAA,EACT;AACF;","names":[]}
|
|
@@ -101,18 +101,13 @@ var DatabaseServiceProvider = class _DatabaseServiceProvider extends Middleware
|
|
|
101
101
|
import_neko_orm.BaseModel.setConnection(async () => {
|
|
102
102
|
const key = ["database", "default"];
|
|
103
103
|
let rc;
|
|
104
|
-
|
|
104
|
+
if (import_neko_context.ctx.isActive()) {
|
|
105
105
|
rc = (0, import_neko_context.ctx)().get(key);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
Global.set(key, rc);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
if (!rc) {
|
|
115
|
-
throw new Error("unable to start default database connection");
|
|
106
|
+
} else if (Global.has(key)) {
|
|
107
|
+
rc = Global.get(key);
|
|
108
|
+
} else {
|
|
109
|
+
rc = await this.getConnection(db_configs["default"]);
|
|
110
|
+
Global.set(key, rc);
|
|
116
111
|
}
|
|
117
112
|
return rc;
|
|
118
113
|
});
|
package/dist/bin/index.cjs
CHANGED
|
@@ -2907,18 +2907,13 @@ var init_DatabaseServiceProvider = __esm({
|
|
|
2907
2907
|
import_neko_orm.BaseModel.setConnection(async () => {
|
|
2908
2908
|
const key = ["database", "default"];
|
|
2909
2909
|
let rc;
|
|
2910
|
-
|
|
2910
|
+
if (import_neko_context6.ctx.isActive()) {
|
|
2911
2911
|
rc = (0, import_neko_context6.ctx)().get(key);
|
|
2912
|
-
}
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
Global.set(key, rc);
|
|
2918
|
-
}
|
|
2919
|
-
}
|
|
2920
|
-
if (!rc) {
|
|
2921
|
-
throw new Error("unable to start default database connection");
|
|
2912
|
+
} else if (Global.has(key)) {
|
|
2913
|
+
rc = Global.get(key);
|
|
2914
|
+
} else {
|
|
2915
|
+
rc = await this.getConnection(db_configs["default"]);
|
|
2916
|
+
Global.set(key, rc);
|
|
2922
2917
|
}
|
|
2923
2918
|
return rc;
|
|
2924
2919
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devbro/pashmak",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.34",
|
|
4
4
|
"description": "testing application for the entire repo",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -42,6 +42,11 @@
|
|
|
42
42
|
"import": "./dist/config.mjs",
|
|
43
43
|
"require": "./dist/config.js"
|
|
44
44
|
},
|
|
45
|
+
"./cache": {
|
|
46
|
+
"types": "./dist/cache.d.ts",
|
|
47
|
+
"import": "./dist/cache.mjs",
|
|
48
|
+
"require": "./dist/cache.js"
|
|
49
|
+
},
|
|
45
50
|
"./sql": {
|
|
46
51
|
"types": "./dist/sql.d.ts",
|
|
47
52
|
"import": "./dist/sql.mjs",
|