@dascompany/database 4.2.4 → 4.2.5
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 -7
- package/dist/ConnectionFactory.js +3 -18
- package/dist/Pool.d.ts +2 -0
- package/dist/Pool.js +17 -0
- package/dist/PoolConnection.d.ts +13 -0
- package/dist/PoolConnection.js +36 -0
- package/package.json +1 -1
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Connection from "./Connection";
|
|
1
|
+
import Connection from './Connection';
|
|
3
2
|
export default class ConnectionFactory {
|
|
4
|
-
private static configName;
|
|
5
|
-
private static configField;
|
|
6
|
-
private static configPath;
|
|
7
|
-
private static pool;
|
|
8
3
|
static createConnection(): Promise<Connection>;
|
|
9
|
-
protected static getDatabaseConfig(): Promise<DatabaseConfig>;
|
|
10
4
|
}
|
|
@@ -1,23 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { readFile } from 'fs/promises';
|
|
1
|
+
import Connection from './Connection';
|
|
2
|
+
import PoolConnection from './PoolConnection';
|
|
4
3
|
export default class ConnectionFactory {
|
|
5
|
-
static configName = 'config.json';
|
|
6
|
-
static configField = 'database';
|
|
7
|
-
static configPath = './server/';
|
|
8
|
-
static pool;
|
|
9
4
|
static async createConnection() {
|
|
10
|
-
const
|
|
11
|
-
if (!this.pool || await this.pool.query('SELECT 1').catch(() => false))
|
|
12
|
-
this.pool = await Database.connect(databaseConfig);
|
|
13
|
-
const client = await this.pool.connect();
|
|
5
|
+
const client = await PoolConnection.getClient();
|
|
14
6
|
return new Connection(client);
|
|
15
7
|
}
|
|
16
|
-
static async getDatabaseConfig() {
|
|
17
|
-
let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
|
|
18
|
-
if (this.configField) {
|
|
19
|
-
config = config[this.configField];
|
|
20
|
-
}
|
|
21
|
-
return config;
|
|
22
|
-
}
|
|
23
8
|
}
|
package/dist/Pool.d.ts
ADDED
package/dist/Pool.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PoolClient } from 'pg';
|
|
2
|
+
export default class PoolConnection {
|
|
3
|
+
private static pool;
|
|
4
|
+
private static poolPromise;
|
|
5
|
+
private static readonly configName;
|
|
6
|
+
private static readonly configField;
|
|
7
|
+
private static readonly configPath;
|
|
8
|
+
static getClient(): Promise<PoolClient>;
|
|
9
|
+
private static getPool;
|
|
10
|
+
private static ensurePool;
|
|
11
|
+
private static init;
|
|
12
|
+
private static getDatabaseConfig;
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import Database from './Database';
|
|
3
|
+
export default class PoolConnection {
|
|
4
|
+
static pool;
|
|
5
|
+
static poolPromise;
|
|
6
|
+
static configName = 'config.json';
|
|
7
|
+
static configField = 'database';
|
|
8
|
+
static configPath = './server/';
|
|
9
|
+
static async getClient() {
|
|
10
|
+
const pool = await this.getPool();
|
|
11
|
+
return pool.connect();
|
|
12
|
+
}
|
|
13
|
+
static async getPool() {
|
|
14
|
+
if (this.pool) {
|
|
15
|
+
return this.pool;
|
|
16
|
+
}
|
|
17
|
+
return this.pool = await this.ensurePool();
|
|
18
|
+
}
|
|
19
|
+
static async ensurePool() {
|
|
20
|
+
if (!this.poolPromise) {
|
|
21
|
+
this.poolPromise = this.init();
|
|
22
|
+
}
|
|
23
|
+
return this.poolPromise;
|
|
24
|
+
}
|
|
25
|
+
static async init() {
|
|
26
|
+
const config = await this.getDatabaseConfig();
|
|
27
|
+
return Database.connect(config);
|
|
28
|
+
}
|
|
29
|
+
static async getDatabaseConfig() {
|
|
30
|
+
let config = JSON.parse(await readFile(`${this.configPath}${this.configName}`, "utf8"));
|
|
31
|
+
if (this.configField) {
|
|
32
|
+
config = config[this.configField];
|
|
33
|
+
}
|
|
34
|
+
return config;
|
|
35
|
+
}
|
|
36
|
+
}
|