-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
366 lines (315 loc) · 12.4 KB
/
server.js
File metadata and controls
366 lines (315 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
const mysql = require('mysql2/promise');
const schemaCache = {};
const connectionString = GetConvar('mysql_connection_string', 'string');
if (!connectionString) throw new Error('Connection string not provided. \n Please add the following to the top of the server.cfg file and restart ezsql: \n set mysql_connection_string "mysql://user:password@host/database_name"');
function parseConnectionString(connectionString) {
const match = connectionString.match(/mysql:\/\/([^:]+)(?::([^@]+))?@([^/]+)\/([^?]*)(\?charset=(.+))?/);
if (!match) throw new Error('Invalid connection string format.');
const [, user, password = '', host, database, , charset = 'utf8mb4'] = match;
return { user, password, host, database, charset };
}
const { user, password, host, database, charset } = parseConnectionString(connectionString);
if (!user || !host || !database) throw new Error('Invalid connection string format. Please ensure it follows the pattern: mysql://user:password@host/database_name?charset=utf8mb4');
const pool = mysql.createPool({
host,
user,
password,
database,
port: 3306,
connectionLimit: 50,
queueLimit: 0,
waitForConnections: true,
charset,
connectTimeout: 10000,
idleTimeout: 60000,
enableKeepAlive: true,
keepAliveInitialDelay: 10000
});
const connection = pool.query('SELECT 1');
if (!connection) throw new Error('Failed to establish a connection to the database. Please check your connection string and database server status.');
on("onResourceStop", (resourceName) => {
if (resourceName === "ezsql") pool.end().then(() => console.log('MySQL connection pool closed.')).catch(err => console.error('Error closing MySQL connection pool:', err));
});
function validateDataTypes(tableName, entryData) {
const tableSchema = schemaCache[tableName];
if (!tableSchema) throw new Error(`Schema for table ${tableName} not found.`);
for (const [column, value] of Object.entries(entryData)) {
const columnSchema = tableSchema.find(col => col.column_name === column);
if (!columnSchema) throw new Error(`Column ${column} does not exist in table ${tableName}.`);
const expectedType = columnSchema.data_type.toLowerCase();
const actualType = typeof value;
if (
(expectedType.includes('int') && actualType !== 'number') ||
(expectedType.includes('varchar') && actualType !== 'string') ||
(expectedType.includes('text') && actualType !== 'string') ||
(expectedType.includes('float') && actualType !== 'number')
) {
throw new Error(`Type mismatch for column ${column}: expected ${expectedType}, got ${actualType}.`);
}
}
}
global.exports('Initialize', async (schema) => {
try {
for (const [tableName, columns] of Object.entries(schema)) {
schemaCache[tableName] = columns; // Cache schema
const columnDefinitions = columns.map(col => {
let def = `${col.column_name} ${col.data_type}`;
if (col.is_primary_key) def += ' PRIMARY KEY';
if (col.auto_increment) def += ' AUTO_INCREMENT';
if (col.unique) def += ' UNIQUE';
if (col.default) def += ` DEFAULT ${mysql.escape(col.default)}`;
if (col.foreign_key) {
def += `, FOREIGN KEY (${col.column_name}) REFERENCES ${col.foreign_key.table} (${col.foreign_key.column})`;
if (col.foreign_key.on_delete) def += ` ON DELETE ${col.foreign_key.on_delete}`;
if (col.foreign_key.on_update) def += ` ON UPDATE ${col.foreign_key.on_update}`;
}
return def;
}).join(', ');
const query = `CREATE TABLE IF NOT EXISTS ${tableName} (${columnDefinitions})`;
await pool.query(query);
}
return true;
} catch (err) {
throw new Error(`Failed to Initialize Table: ${err}`);
}
});
global.exports('AddEntry', async (tableName, entryData) => {
try {
validateDataTypes(tableName, entryData);
const columnKeys = Object.keys(entryData);
const columns = columnKeys.join(', ');
const placeholders = columnKeys.map(() => '?').join(', ');
const values = Object.values(entryData);
const query = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
const conn = await pool.getConnection();
if (!conn) throw new Error('Failed to establish a connection to the database.');
await conn.query(query, values);
conn.release();
return true;
} catch (err) {
throw new Error(`Failed to add entry: ${err}`);
}
});
global.exports('UpdateEntry', async (tableName, entryData, id) => {
try {
validateDataTypes(tableName, entryData);
const columnKeys = Object.keys(entryData);
const updates = columnKeys.map(col => `${col} = ?`).join(', ');
const values = [...Object.values(entryData), id];
const query = `UPDATE ${tableName} SET ${updates} WHERE id = ?`;
const conn = await pool.getConnection();
if (!conn) throw new Error('Failed to establish a connection to the database.');
const [result] = await conn.query(query, values);
conn.release();
if (result.affectedRows === 0) {
throw new Error(`No entry found with id ${id} in table ${tableName}.`);
}
return true;
} catch (err) {
throw new Error(`Failed to update entry: ${err}`);
}
});
global.exports('DeleteEntry', async (tableName, id) => {
try {
const query = `DELETE FROM ${tableName} WHERE id = ?`;
const conn = await pool.getConnection();
if (!conn) throw new Error('Failed to establish a connection to the database.');
const [result] = await conn.query(query, [id]);
conn.release();
if (result.affectedRows === 0) throw new Error(`No entry found with id ${id} in table ${tableName}.`);
return true;
} catch (err) {
throw new Error(`Failed to delete entry: ${err}`);
}
});
global.exports('GetAllEntries', async (tableName, returnColumns = null, cb = null) => {
try {
const columns = returnColumns ? returnColumns.join(', ') : '*';
const query = `SELECT ${columns} FROM ${tableName}`;
const conn = await pool.getConnection();
if (!conn) throw new Error('Failed to establish a connection to the database.');
const [rows] = await conn.query(query);
conn.release();
if (cb) {
cb(rows);
return;
}
return rows;
} catch (err) {
throw new Error(`Failed to get all entries: ${err}`);
}
});
global.exports('GetAllEntriesByData', async (tableName, entryData, returnColumns = null) => {
try {
validateDataTypes(tableName, entryData);
const columns = returnColumns ? returnColumns.join(', ') : '*';
const columnKeys = Object.keys(entryData);
const conditions = columnKeys.map(col => `${col} = ?`).join(' AND ');
const values = Object.values(entryData);
const query = `SELECT ${columns} FROM ${tableName} WHERE ${conditions}`;
const conn = await pool.getConnection();
if (!conn) throw new Error('Failed to establish a connection to the database.');
const [rows] = await conn.query(query, values);
conn.release();
return rows;
} catch (err) {
throw new Error(`Failed to get entries by data: ${err}`);
}
});
global.exports('GetFirstEntryByData', async (tableName, entryData, returnColumns = null) => {
let conn;
try {
if (entryData) validateDataTypes(tableName, entryData);
const columns = returnColumns ? returnColumns.join(', ') : '*';
const columnKeys = Object.keys(entryData || {});
const conditions = columnKeys.length > 0
? columnKeys.map(col => `${col} = ?`).join(' AND ')
: '1=1';
const values = Object.values(entryData || {});
const query = `SELECT ${columns} FROM ${tableName} WHERE ${conditions} LIMIT 1`;
conn = await pool.getConnection();
if (!conn) throw new Error('Failed to establish a connection to the database.');
const [rows] = await conn.query(query, values);
return rows.length > 0 ? rows[0] : null;
} catch (err) {
throw new Error(`Failed to get first entry by data: ${err}`);
} finally {
if (conn) conn.release();
}
});
global.exports('Query', (query, parameters = [], cb) => {
if (typeof parameters === 'function') {
cb = parameters;
parameters = [];
}
if (typeof cb === 'function') {
let conn;
pool.getConnection()
.then(connection => {
conn = connection;
return conn.query(query, parameters);
})
.then(([rows]) => {
let result;
if (rows && !Array.isArray(rows)) {
result = {
...rows,
insertId: rows.insertId || 0,
affectedRows: rows.affectedRows || 0
};
} else if (Array.isArray(rows)) {
result = rows;
} else {
result = { insertId: 0, affectedRows: 0 };
}
if (conn) conn.release();
cb(result);
})
.catch(err => {
throw new Error(`Failed to execute query: ${err}`);
if (conn) conn.release();
cb(null);
});
return;
}
return (async () => {
let conn;
try {
conn = await pool.getConnection();
const [rows, fields] = await conn.query(query, parameters);
if (rows && !Array.isArray(rows)) {
return {
...rows,
insertId: rows.insertId || 0,
affectedRows: rows.affectedRows || 0
};
} else if (Array.isArray(rows)) {
return rows;
} else {
return { insertId: 0, affectedRows: 0 };
}
} catch (err) {
throw new Error(`Failed to execute query: ${err}`);
} finally {
if (conn) conn.release();
}
})();
});
global.exports('Transaction', async (queries, parameters = []) => {
let conn;
try {
if (!Array.isArray(queries)) throw new Error('Queries parameter must be an array');
conn = await pool.getConnection();
await conn.beginTransaction();
const results = [];
for (let i = 0; i < queries.length; i++) {
const query = queries[i];
const params = Array.isArray(parameters[i]) ? parameters[i] : [];
try {
const [result] = await conn.query(query, params);
results.push(result);
} catch (queryErr) {
await conn.rollback();
throw new Error(`Query #${i+1} failed: ${queryErr.message}`);
}
}
await conn.commit();
return results.length > 0 ? results : true;
} catch (err) {
if (conn) {
try {
await conn.rollback();
} catch (rollbackErr) {
console.error('Error during rollback:', rollbackErr);
}
}
console.error(`Transaction failed: ${err.message}`);
throw new Error(`Failed to execute transaction: ${err.message}`);
} finally {
if (conn) conn.release();
}
});
global.exports('PreparedStatement', async (query, parameters = []) => {
let conn;
try {
conn = await pool.getConnection();
const [rows] = await conn.execute(query, parameters);
return rows;
} catch (err) {
throw new Error(`Failed to execute prepared statement: ${err}`);
} finally {
if (conn) conn.release();
}
});
// Add a function to check pool status
global.exports('GetPoolStatus', () => {
// Return pool status synchronously (not as a promise)
return {
threadId: pool.threadId,
config: pool.config,
activeConnections: pool._allConnections?.length || 0,
idleConnections: pool._freeConnections?.length || 0,
totalConnections: pool._allConnections?.length || 0
};
});
// Add periodic connection cleanup function
setInterval(() => {
try {
// Get pool status directly without using the export
const status = {
threadId: pool.threadId,
config: pool.config,
activeConnections: pool._allConnections?.length || 0,
idleConnections: pool._freeConnections?.length || 0,
totalConnections: pool._allConnections?.length || 0
};
// If we're getting close to the connection limit, force a cleanup
if (status.activeConnections > 0.8 * (status.config?.connectionLimit || 50)) {
console.log(`^3MySQL connection pool getting high (${status.activeConnections}/${status.config?.connectionLimit || 50}), forcing cleanup...^7`);
// The following will force unused connections to be released
pool.query('SELECT 1').catch(err => console.error('Error in connection cleanup:', err));
}
} catch (err) {
console.error('Error during connection pool maintenance:', err);
}
}, 30000); // Run every 30 seconds