Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A svelte-kit based alternative to phpMyAdmin with multi DB support

# TODO

- Switch to connection pool
- [x] Switch to connection pool

# Installation

Expand Down
57 changes: 19 additions & 38 deletions src/lib/db/mssql/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mssql from 'mssql';
import type { Database } from 'src/app';
import { Logger } from '../helper/helper';
import { poolManager } from '../pool_manager';

const logger = new Logger();

Expand All @@ -10,26 +11,17 @@ export async function get_all_dbs_mssql(
password: string,
port: string
): Promise<Array<string>> {
const sqlConfig = {
user: user,
password: password,
database: 'master', // this is the default database
server: ip,
port: port,
pool: {
max: 1,
min: 0,
idleTimeoutMillis: 30000
},
options: {
encrypt: true, // for azure
trustServerCertificate: true // change to true for local dev / self-signed certs
}
};
try {
// make sure that any items are correctly URL encoded in the connection string
await mssql.connect(sqlConfig);
const result = await mssql.query`SELECT name FROM master.dbo.sysdatabases`;
const pool = await poolManager.getMssqlPool({
type: 'mssql',
host: ip,
user: user,
password: password,
port: parseInt(port),
database: 'master'
});
const result = await pool.query`SELECT name FROM master.dbo.sysdatabases`;
return result.recordset.map((x: Database) => x.name);
} catch (err) {
logger.Error(err);
Expand All @@ -43,27 +35,16 @@ export async function create_db_mssql(
db: string,
port: string
) {

const sqlConfig = {
user: user,
password: password,
database: 'master', // this is the default database
server: ip,
port: port,
pool: {
max: 1,
min: 0,
idleTimeoutMillis: 30000
},
options: {
encrypt: true, // for azure
trustServerCertificate: true // change to true for local dev / self-signed certs
}
};
try {
// make sure that any items are correctly URL encoded in the connection string
await mssql.connect(sqlConfig);
await mssql.query('CREATE DATABASE ' + db);
const pool = await poolManager.getMssqlPool({
type: 'mssql',
host: ip,
user: user,
password: password,
port: parseInt(port),
database: 'master'
});
await pool.query('CREATE DATABASE ' + db);
} catch (err) {
logger.Error(err);
}
Expand Down
24 changes: 12 additions & 12 deletions src/lib/db/mysql/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mysql from 'mysql2/promise';
import { Logger } from '../helper/helper';
import { poolManager } from '../pool_manager';

const logger = new Logger();
export async function get_all_dbs_mysql(
Expand All @@ -11,19 +12,19 @@ export async function get_all_dbs_mysql(
try {
if (port == null) port = '3306';
const databases: Array<string> = [];
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: 'sys',
password: pass,
port: parseInt(port)
port: parseInt(port),
database: 'sys'
});

const [databases_raw] = await connection.query('SHOW DATABASES;'); // Get all databases
Array.from(databases_raw).forEach((db) => {
const [databases_raw] = await pool.query('SHOW DATABASES;'); // Get all databases
Array.from(databases_raw).forEach((db: any) => {
databases.push(db.Database);
});
connection.destroy();
return databases;
} catch (error) {
logger.Error(error);
Expand All @@ -39,16 +40,15 @@ export async function create_db_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
password: pass,
database: 'sys', // Default database of MySQL, we don't know what db is selected since we want to create a new one
port: parseInt(port)
port: parseInt(port),
database: 'sys'
});
connection.connect();
await connection.query('CREATE DATABASE ' + db);
connection.destroy();
await pool.query('CREATE DATABASE ' + db);
} catch (error) {
logger.Error(error);
}
Expand Down
31 changes: 16 additions & 15 deletions src/lib/db/mysql/record.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mysql from 'mysql2/promise';
import { Logger } from '../helper/helper';
import { poolManager } from '../pool_manager';

const logger = new Logger();
export async function records_mysql(
Expand All @@ -16,7 +17,8 @@ export async function records_mysql(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rows: Array<any> = [];

const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
Expand All @@ -25,15 +27,14 @@ export async function records_mysql(
});

// get version
const [records, cols_raw] = await connection.query('SELECT * FROM ' + table);
const [records, cols_raw] = await pool.query('SELECT * FROM ' + table);
if (records instanceof Array)
// Get all records
for (let i = 0; i < records.length; i++) {
rows.push(records[i]);
}

Array.from(cols_raw).forEach((col) => cols.push(col.name));
connection.destroy(); // We need to close the connection to prevent saturation of max connections
return { cols: cols, rows: rows, cols_raw: cols_raw };
} catch (error) {
logger.Error(error);
Expand All @@ -50,7 +51,8 @@ export async function struct_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
Expand All @@ -59,8 +61,7 @@ export async function struct_mysql(
});

// get version
const [fields] = await connection.query('SHOW FIELDS FROM ' + table);
connection.destroy(); // We need to close the connection to prevent saturation of max connections
const [fields] = await pool.query('SHOW FIELDS FROM ' + table);
return fields;
} catch (error) {
logger.Error(error);
Expand All @@ -77,15 +78,15 @@ export async function add_record_mysql(
port: string
) {
try {
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
password: pass,
port: parseInt(port)
});
await connection.query('INSERT INTO ' + table + ' SET ?', JSON.parse(records));
connection.destroy(); // We need to close the connection to prevent saturation of max connections
await pool.query('INSERT INTO ' + table + ' SET ?', JSON.parse(records));
} catch (error) {
logger.Error(error);
}
Expand All @@ -101,16 +102,16 @@ export async function delete_record_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
password: pass,
port: parseInt(port)
});

await connection.query(query);
connection.destroy(); // We need to close the connection to prevent saturation of max connections
await pool.query(query);
} catch (error) {
logger.Error(error);
}
Expand All @@ -126,16 +127,16 @@ export async function update_record_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
password: pass,
port: parseInt(port)
});

await connection.query(query);
connection.destroy(); // We need to close the connection to prevent saturation of max connections
await pool.query(query);
} catch (error) {
logger.Error(error);
}
Expand Down
39 changes: 20 additions & 19 deletions src/lib/db/mysql/table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mysql from 'mysql2/promise';
import { parse_query } from '../helper/helper';
import { Logger } from '../helper/helper';
import { poolManager } from '../pool_manager';

const logger = new Logger();
export async function get_all_tables_mysql(
Expand All @@ -13,7 +14,8 @@ export async function get_all_tables_mysql(
try {
if (port == null) port = '3306';
const tables: Array<string> = []; // The tables in database
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
Expand All @@ -22,10 +24,9 @@ export async function get_all_tables_mysql(
});

// get version
const [tables_raw] = await connection.query('SHOW TABLES;');
const [tables_raw] = await pool.query('SHOW TABLES;');

Array.from(tables_raw).forEach((table) => tables.push(Object.values(table)[0])); // Get all tables in a db
connection.destroy(); // We need to close the connection to prevent saturation of max connections
Array.from(tables_raw).forEach((table: any) => tables.push(Object.values(table)[0] as string)); // Get all tables in a db
return tables;
} catch (error) {
logger.Error(error);
Expand All @@ -43,7 +44,8 @@ export async function create_table_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
Expand All @@ -54,8 +56,7 @@ export async function create_table_mysql(
fields.forEach((field) => (query += field + ','));
query = query.slice(0, -1) + '';
query += ')';
await connection.query(query);
connection.destroy(); // We need to close the connection to prevent saturation of max connections
await pool.query(query);
} catch (error) {
logger.Error(error);
}
Expand All @@ -71,15 +72,15 @@ export async function drop_table_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
password: pass,
port: parseInt(port)
});
await connection.query('DROP TABLE ' + table);
connection.destroy(); // We need to close the connection to prevent saturation of max connections
await pool.query('DROP TABLE ' + table);
} catch (error) {
logger.Error(error);
}
Expand All @@ -96,16 +97,16 @@ export async function delete_field_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
password: pass,
port: parseInt(port)
});

await connection.query('ALTER TABLE ' + table + ' DROP COLUMN ' + col); // Drop a column in a table
connection.destroy(); // We need to close the connection to prevent saturation of max connections
await pool.query('ALTER TABLE ' + table + ' DROP COLUMN ' + col); // Drop a column in a table
} catch (error) {
logger.Error(error);
}
Expand All @@ -121,15 +122,15 @@ export async function truncate_table_mysql(
) {
try {
if (port == null) port = '3306';
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
password: pass,
port: parseInt(port)
});
await connection.query('TRUNCATE TABLE ' + table);
connection.destroy(); // We need to close the connection to prevent saturation of max connections
await pool.query('TRUNCATE TABLE ' + table);
} catch (error) {
logger.Error(error);
}
Expand All @@ -150,15 +151,15 @@ export async function search_in_table_mysql(
const rows = Object.values(records);
let query = parse_query(keys, rows, table);
query = query.replace('DELETE FROM', 'SELECT * FROM');
const connection = await mysql.createConnection({
const pool = poolManager.getMySQLPool({
type: 'mysql',
host: ip,
user: user,
database: db,
password: pass,
port: parseInt(port)
});
const [rows_from_db] = await connection.query(query);
connection.destroy(); // We need to close the connection to prevent saturation of max connections
const [rows_from_db] = await pool.query(query);
return rows_from_db;
} catch (error) {}
}
Loading