@dascompany/database 4.2.7 → 5.0.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.
- package/dist/ConnectionFactory.d.ts +1 -1
- package/dist/ConnectionFactory.js +2 -2
- package/dist/PoolConnection.d.ts +4 -3
- package/dist/PoolConnection.js +21 -15
- package/dist/query/QueryParams.js +1 -1
- package/package.json +1 -1
- package/dist/Pool.d.ts +0 -2
- package/dist/Pool.js +0 -17
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import Connection from './Connection';
|
|
2
2
|
import PoolConnection from './PoolConnection';
|
|
3
3
|
export default class ConnectionFactory {
|
|
4
|
-
static async createConnection() {
|
|
5
|
-
const client = await PoolConnection.getClient();
|
|
4
|
+
static async createConnection(tenantName) {
|
|
5
|
+
const client = await PoolConnection.getClient(tenantName);
|
|
6
6
|
return new Connection(client);
|
|
7
7
|
}
|
|
8
8
|
}
|
package/dist/PoolConnection.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { PoolClient } from 'pg';
|
|
2
2
|
export default class PoolConnection {
|
|
3
|
-
private static
|
|
4
|
-
private static
|
|
3
|
+
private static poolMap;
|
|
4
|
+
private static poolPromiseMap;
|
|
5
5
|
private static readonly configName;
|
|
6
6
|
private static readonly configField;
|
|
7
7
|
private static readonly configPath;
|
|
8
|
-
static
|
|
8
|
+
private static readonly defaultName;
|
|
9
|
+
static getClient(tenantName?: string): Promise<PoolClient>;
|
|
9
10
|
private static getPool;
|
|
10
11
|
private static ensurePool;
|
|
11
12
|
private static init;
|
package/dist/PoolConnection.js
CHANGED
|
@@ -1,36 +1,42 @@
|
|
|
1
1
|
import { readFile } from 'fs/promises';
|
|
2
2
|
import Database from './Database';
|
|
3
3
|
export default class PoolConnection {
|
|
4
|
-
static
|
|
5
|
-
static
|
|
4
|
+
static poolMap = new Map();
|
|
5
|
+
static poolPromiseMap = new Map();
|
|
6
6
|
static configName = 'config.json';
|
|
7
7
|
static configField = 'database';
|
|
8
8
|
static configPath = './server/';
|
|
9
|
-
static
|
|
10
|
-
|
|
9
|
+
static defaultName = 'default';
|
|
10
|
+
static async getClient(tenantName = this.defaultName) {
|
|
11
|
+
const pool = await this.getPool(tenantName);
|
|
11
12
|
return pool.connect();
|
|
12
13
|
}
|
|
13
|
-
static async getPool() {
|
|
14
|
-
if (this.
|
|
15
|
-
return this.
|
|
14
|
+
static async getPool(tenantName) {
|
|
15
|
+
if (this.poolMap.has(tenantName)) {
|
|
16
|
+
return this.poolMap.get(tenantName);
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
+
const pool = await this.ensurePool(tenantName);
|
|
19
|
+
this.poolMap.set(tenantName, pool);
|
|
20
|
+
return pool;
|
|
18
21
|
}
|
|
19
|
-
static async ensurePool() {
|
|
20
|
-
if (!this.
|
|
21
|
-
this.
|
|
22
|
+
static async ensurePool(tenantName) {
|
|
23
|
+
if (!this.poolPromiseMap.has(tenantName)) {
|
|
24
|
+
this.poolPromiseMap.set(tenantName, this.init(tenantName));
|
|
22
25
|
}
|
|
23
|
-
return this.
|
|
26
|
+
return this.poolPromiseMap.get(tenantName);
|
|
24
27
|
}
|
|
25
|
-
static async init() {
|
|
26
|
-
const config = await this.getDatabaseConfig();
|
|
28
|
+
static async init(tenantName) {
|
|
29
|
+
const config = await this.getDatabaseConfig(tenantName);
|
|
27
30
|
return Database.connect(config);
|
|
28
31
|
}
|
|
29
|
-
static async getDatabaseConfig() {
|
|
32
|
+
static async getDatabaseConfig(tenantName) {
|
|
30
33
|
let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
|
|
31
34
|
if (this.configField) {
|
|
32
35
|
config = config[this.configField];
|
|
33
36
|
}
|
|
37
|
+
if (tenantName != this.defaultName) {
|
|
38
|
+
config = config[tenantName];
|
|
39
|
+
}
|
|
34
40
|
return config;
|
|
35
41
|
}
|
|
36
42
|
}
|
|
@@ -24,7 +24,7 @@ export default class QueryParams {
|
|
|
24
24
|
let sqlParams = [];
|
|
25
25
|
parameters.forEach((parameter) => {
|
|
26
26
|
if ((Array.isArray(parameter.value) && parameter.value.length > 0) ||
|
|
27
|
-
(!Array.isArray(parameter.value) && parameter.value))
|
|
27
|
+
(!Array.isArray(parameter.value) && (parameter.value || parameter.value === false)))
|
|
28
28
|
sqlParams.push(QueryParamsCreator.createParam(parameter));
|
|
29
29
|
});
|
|
30
30
|
return sqlParams;
|
package/package.json
CHANGED
package/dist/Pool.d.ts
DELETED
package/dist/Pool.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import Database from './Database';
|
|
2
|
-
import { readFile } from 'fs/promises';
|
|
3
|
-
let pool = null;
|
|
4
|
-
let poolPromise = null;
|
|
5
|
-
export async function getPool() {
|
|
6
|
-
if (pool)
|
|
7
|
-
return pool;
|
|
8
|
-
if (!poolPromise) {
|
|
9
|
-
poolPromise = (async () => {
|
|
10
|
-
const raw = await readFile('./server/config.json', 'utf8');
|
|
11
|
-
const config = JSON.parse(raw).database;
|
|
12
|
-
return Database.connect(config);
|
|
13
|
-
})();
|
|
14
|
-
}
|
|
15
|
-
pool = await poolPromise;
|
|
16
|
-
return pool;
|
|
17
|
-
}
|