Skip to content

Commit 6cf2d10

Browse files
authored
Merge pull request #256 from JS-AK/feat/mysql/core-update
Feat/mysql/core update
2 parents 3782832 + 4ec647e commit 6cf2d10

File tree

112 files changed

+9274
-1199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+9274
-1199
lines changed

.eslintrc.cjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ module.exports = {
6161
},
6262
],
6363
"@typescript-eslint/semi": ["error", "always"],
64+
"@typescript-eslint/space-before-function-paren": [
65+
"warn",
66+
{
67+
anonymous: "never",
68+
asyncArrow: "always",
69+
named: "never",
70+
},
71+
],
6472
"@typescript-eslint/type-annotation-spacing": "error",
6573
"array-bracket-spacing": ["error", "never"],
6674
"arrow-parens": ["error", "always"],
@@ -123,14 +131,6 @@ module.exports = {
123131
functions: "always",
124132
keywords: "always",
125133
}],
126-
"space-before-function-paren": [
127-
"warn",
128-
{
129-
anonymous: "never",
130-
asyncArrow: "always",
131-
named: "never",
132-
},
133-
],
134134
"space-in-parens": ["error", "never"],
135135
"space-infix-ops": "error",
136136
},

package-lock.json

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
},
4444
"homepage": "https://github.com/JS-AK/db-manager#readme",
4545
"dependencies": {
46-
"@js-ak/mysql-migration-system": "1.1.0",
47-
"@js-ak/pg-migration-system": "1.4.0",
46+
"@js-ak/mysql-migration-system": "1.2.0",
47+
"@js-ak/pg-migration-system": "1.5.0",
4848
"@types/pg": "8.11.6",
4949
"mysql2": "3.10.1",
5050
"pg": "8.12.0"

src/lib/mysql/connection.ts

Lines changed: 157 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,156 @@ import { MYSQL } from "../../index.js";
44

55
const pools = new Map<string, mysql.Pool>();
66

7+
/**
8+
* Creates a MySQL connection with the given configuration.
9+
*
10+
* @param config - The configuration options for the MySQL connection.
11+
*
12+
* @returns The MySQL connection.
13+
*/
714
export const createConnection = async (config: mysql.ConnectionOptions): Promise<mysql.Connection> => {
815
return mysql.createConnection(config);
916
};
10-
export const getStandardPool = (config: MYSQL.ModelTypes.TDBCreds, poolName = "00"): mysql.Pool => {
11-
const { database, host, password, port, user } = config;
17+
18+
/**
19+
* Retrieves or creates a standard connection pool.
20+
*
21+
* @param config - The database credentials configuration object.
22+
* @param [poolName="00"] - The name suffix for the pool, default is "00".
23+
*
24+
* @returns The retrieved or newly created standard connection pool.
25+
*/
26+
export const getStandardPool = (
27+
config: MYSQL.ModelTypes.TDBCreds,
28+
poolName: string = "00",
29+
): mysql.Pool => {
1230
const poolNameResult = "st" + poolName;
13-
const credsString = `${poolNameResult}#${user}:${password}@${host}:${port}/${database}`;
1431

15-
const poolCandidate = pools.get(credsString);
32+
return getPool(config, poolNameResult);
33+
};
1634

17-
if (poolCandidate) {
18-
return poolCandidate;
19-
} else {
20-
const pool = mysql.createPool(config);
35+
/**
36+
* Retrieves or creates a transaction-specific connection pool.
37+
*
38+
* @param config - The database credentials configuration object.
39+
* @param [poolName="00"] - The name suffix for the pool, default is "00".
40+
*
41+
* @returns The retrieved or newly created transaction connection pool.
42+
*/
43+
export const getTransactionPool = (
44+
config: MYSQL.ModelTypes.TDBCreds,
45+
poolName: string = "00",
46+
): mysql.Pool => {
47+
const poolNameResult = "tr" + poolName;
2148

22-
pools.set(credsString, pool);
49+
return getPool(config, poolNameResult);
50+
};
2351

24-
return pool;
52+
/**
53+
* Removes and closes a standard connection pool.
54+
*
55+
* @param config - The database credentials configuration object.
56+
* @param [poolName="00"] - The name suffix for the pool, default is "00".
57+
*
58+
* @returns A promise that resolves when the pool is closed.
59+
*/
60+
export const removeStandardPool = async (
61+
config: MYSQL.ModelTypes.TDBCreds,
62+
poolName: string = "00",
63+
): Promise<void> => {
64+
const poolNameResult = "st" + poolName;
65+
66+
return removePool(config, poolNameResult);
67+
};
68+
69+
/**
70+
* Removes and closes a transaction-specific connection pool.
71+
*
72+
* @param config - The database credentials configuration object.
73+
* @param [poolName="00"] - The name suffix for the pool, default is "00".
74+
*
75+
* @returns A promise that resolves when the pool is closed.
76+
*/
77+
export const removeTransactionPool = async (
78+
config: MYSQL.ModelTypes.TDBCreds,
79+
poolName: string = "00",
80+
): Promise<void> => {
81+
const poolNameResult = "tr" + poolName;
82+
83+
return removePool(config, poolNameResult);
84+
};
85+
86+
/**
87+
* Gracefully shuts down all active connection pools.
88+
*
89+
* @returns A promise that resolves when all pools are closed.
90+
*/
91+
export const shutdown = async (): Promise<void> => {
92+
const poolShutdowns: Promise<void>[] = [];
93+
94+
for (const [_credsString, pool] of pools) {
95+
poolShutdowns.push(pool.end());
2596
}
97+
98+
try {
99+
await Promise.all(poolShutdowns);
100+
} finally {
101+
pools.clear();
102+
}
103+
};
104+
105+
/**
106+
* Creates a connection credentials string.
107+
* This string is used as a key in the pools map to identify different pools.
108+
*
109+
* @param poolName - The name prefix for the pool.
110+
* @param creds - The credentials object.
111+
* @param creds.user - The database user.
112+
* @param creds.password - The database password.
113+
* @param creds.host - The database host.
114+
* @param creds.port - The database port.
115+
* @param creds.database - The database name.
116+
*
117+
* @returns The constructed credentials string.
118+
*/
119+
const createCredsString = (
120+
poolName: string,
121+
creds: { user: string; password: string; host: string; port: number; database: string; },
122+
): string => {
123+
return `${poolName}#${creds.user}:${creds.password}@${creds.host}:${creds.port}/${creds.database}`;
124+
};
125+
126+
/**
127+
* Masks sensitive information in a connection string.
128+
*
129+
* @param credsString - The connection credentials string.
130+
*
131+
* @returns The credentials string with sensitive information masked.
132+
*/
133+
const _maskSensitiveInfo = (credsString: string): string => {
134+
return credsString.replace(/:.+@/, ":<hidden>@");
26135
};
27136

28-
export const getTransactionPool = (config: MYSQL.ModelTypes.TDBCreds, poolName = "00"): mysql.Pool => {
137+
/**
138+
* Retrieves or creates a named connection pool.
139+
*
140+
* @param config - The database credentials configuration object.
141+
* @param poolName - The name suffix for the pool.
142+
*
143+
* @returns The retrieved or newly created standard connection pool.
144+
*/
145+
const getPool = (
146+
config: MYSQL.ModelTypes.TDBCreds,
147+
poolName: string,
148+
): mysql.Pool => {
29149
const { database, host, password, port, user } = config;
30-
const poolNameResult = "tr" + poolName;
31-
const credsString = `${poolNameResult}#${user}:${password}@${host}:${port}/${database}`;
150+
const credsString = createCredsString(poolName, {
151+
database,
152+
host,
153+
password,
154+
port,
155+
user,
156+
});
32157

33158
const poolCandidate = pools.get(credsString);
34159

@@ -43,24 +168,26 @@ export const getTransactionPool = (config: MYSQL.ModelTypes.TDBCreds, poolName =
43168
}
44169
};
45170

46-
export const removeStandardPool = async (config: MYSQL.ModelTypes.TDBCreds, poolName = "00"): Promise<void> => {
47-
const { database, host, password, port, user } = config;
48-
const poolNameResult = "st" + poolName;
49-
const credsString = `${poolNameResult}#${user}:${password}@${host}:${port}/${database}`;
50-
51-
const pool = pools.get(credsString);
52-
53-
if (pool) {
54-
pools.delete(credsString);
55-
56-
await pool.end();
57-
}
58-
};
59-
60-
export const removeTransactionPool = async (config: MYSQL.ModelTypes.TDBCreds, poolName = "00"): Promise<void> => {
171+
/**
172+
* Removes and closes a named connection pool.
173+
*
174+
* @param config - The database credentials configuration object.
175+
* @param poolName - The name suffix for the pool.
176+
*
177+
* @returns A promise that resolves when the pool is closed.
178+
*/
179+
const removePool = async (
180+
config: MYSQL.ModelTypes.TDBCreds,
181+
poolName: string,
182+
): Promise<void> => {
61183
const { database, host, password, port, user } = config;
62-
const poolNameResult = "tr" + poolName;
63-
const credsString = `${poolNameResult}#${user}:${password}@${host}:${port}/${database}`;
184+
const credsString = createCredsString(poolName, {
185+
database,
186+
host,
187+
password,
188+
port,
189+
user,
190+
});
64191

65192
const pool = pools.get(credsString);
66193

0 commit comments

Comments
 (0)