Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit f3fc016

Browse files
committed
refactor(pool): new createConnection function
Remove the connection string parsing and send the URI directly, if it exists. For semicolon connection strings, check for and replace aliases.
1 parent 3816272 commit f3fc016

File tree

3 files changed

+22
-54
lines changed

3 files changed

+22
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ dist
111111
.vscode-test
112112

113113
# yarn v2
114+
yarn.lock
114115
.yarn/cache
115116
.yarn/unplugged
116117
.yarn/build-state.yml

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
},
1414
"dependencies": {
1515
"@citizenfx/server": "^2.0.4793-1",
16-
"connection-string-parser": "^1.0.4",
1716
"mysql2": "https://github.com/overextended/node-mysql2.git",
1817
"postinstall-postinstall": "^2.1.0"
1918
},

src/pool.js

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,35 @@
11
import { createPool } from 'mysql2/promise';
22
import { parseTypes } from './parser';
3-
import { ConnectionStringParser } from 'connection-string-parser';
43

54
const connectionString = GetConvar('mysql_connection_string', '');
65

76
if (connectionString === '') {
87
throw new Error(`Set up mysql_connection_string in server.cfg`);
98
}
109

11-
const parseSemiColons = () => {
12-
const parts = connectionString.split(';');
13-
if (parseTypes.length === 1) {
14-
throw new Error(
15-
`Set up mysql_connection_string in correct format - 'username=root;password=password;database=db;host=127.0.0.1'`
16-
);
17-
}
18-
return parts.reduce((connectionInfo, parameter) => {
19-
const [key, value] = parameter.split('=');
20-
connectionInfo[key] = value;
21-
return connectionInfo;
22-
}, {});
23-
};
10+
const dbOptions = (() => {
11+
if (connectionString.includes('mysql://')) return { uri: connectionString };
12+
const options = connectionString
13+
.replace(/(?:host(?:name)|ip|server|data\s?source|addr(?:ess)?)=/gi, 'host=')
14+
.replace(/(?:user\s?(?:id|name)?|uid)=/gi, 'user=')
15+
.replace(/(?:pwd|pass)=/gi, 'password=')
16+
.replace(/(?:db)=/gi, 'database=')
17+
.split(';')
18+
.reduce((connectionInfo, parameter) => {
19+
const [key, value] = parameter.split('=');
20+
connectionInfo[key] = value;
21+
return connectionInfo;
22+
}, {});
23+
24+
return options;
25+
})();
2426

25-
// @TODO: refactor acceptable options when using semicolon connection; this or stuff is getting old
2627
const createConnection = () => {
27-
if (connectionString.includes('host=')) {
28-
const options = parseSemiColons();
29-
30-
return createPool({
31-
host: options.host || 'localhost',
32-
port: options.port || 3306,
33-
user: options.username || options.user || options.userid || options.uid || 'root',
34-
password: options.password || options.pass || options.pwd || '',
35-
database: options.endpoint || options.database,
36-
charset: 'utf8mb4_unicode_ci',
37-
connectTimeout: 30000,
38-
...options.options,
39-
namedPlaceholders: true,
40-
typeCast: parseTypes,
41-
});
42-
} else {
43-
const options = new ConnectionStringParser({
44-
scheme: 'mysql',
45-
hosts: [],
46-
}).parse(connectionString);
47-
48-
if (Object.keys(options).length === 0) {
49-
throw new Error(`Set up mysql_connection_string in correct format - 'mysql://user:password@host/database'`);
50-
}
51-
52-
return createPool({
53-
host: options.hosts[0].host || 'localhost',
54-
port: options.hosts[0].port || 3306,
55-
user: options.username || 'root',
56-
password: options.password || '',
57-
database: options.endpoint,
58-
charset: 'utf8mb4_unicode_ci',
59-
connectTimeout: 30000,
60-
...options.options,
61-
namedPlaceholders: true,
62-
typeCast: parseTypes,
63-
});
64-
}
28+
return createPool({
29+
...dbOptions,
30+
namedPlaceholders: true,
31+
typeCast: parseTypes,
32+
});
6533
};
6634

6735
export const pool = createConnection();

0 commit comments

Comments
 (0)