|
1 | 1 | import { createPool } from 'mysql2/promise';
|
2 | 2 | import { parseTypes } from './parser';
|
3 |
| -import { ConnectionStringParser } from 'connection-string-parser'; |
4 | 3 |
|
5 | 4 | const connectionString = GetConvar('mysql_connection_string', '');
|
6 | 5 |
|
7 | 6 | if (connectionString === '') {
|
8 | 7 | throw new Error(`Set up mysql_connection_string in server.cfg`);
|
9 | 8 | }
|
10 | 9 |
|
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 | +})(); |
24 | 26 |
|
25 |
| -// @TODO: refactor acceptable options when using semicolon connection; this or stuff is getting old |
26 | 27 | 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 | + }); |
65 | 33 | };
|
66 | 34 |
|
67 | 35 | export const pool = createConnection();
|
0 commit comments