Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mysql addition #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"devDependencies": {
"@octokit/core": "^3.2.0",
"lerna": "^4.0.0"
},
"dependencies": {
"mysql2": "^3.11.5"
}
}
148 changes: 116 additions & 32 deletions packages/common-ts/src/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,129 @@
import { Sequelize } from 'sequelize'
import { Sequelize } from 'sequelize';
import mysql from 'mysql2/promise';

interface ConnectOptions {
host: string
port?: number
username: string
password: string
database: string
sslEnabled?: boolean
logging?: (sql: string, timing?: number) => void
poolMin?: number
poolMax?: number
host: string;
port?: number;
username: string;
password: string;
database: string;
sslEnabled?: boolean;
logging?: (sql: string, timing?: number) => void;
poolMin?: number;
poolMax?: number;
}

export const connectDatabase = async (options: ConnectOptions): Promise<Sequelize> => {
const { host, username, password, database, logging } = options
/**
* Establishes a database connection using Sequelize (PostgreSQL by default).
* @param options - Configuration for the connection.
* @returns Sequelize instance.
*/
export const connectDatabaseSequelize = async (options: ConnectOptions): Promise<Sequelize> => {
const {
host,
port = 5432, // Default PostgreSQL port
username,
password,
database,
sslEnabled = false,
logging,
poolMin = 0,
poolMax = 10,
} = options;

try {
const sequelize = new Sequelize({
dialect: 'postgres',
host,
port,
username,
password,
database,
ssl: sslEnabled,
pool: {
max: poolMax,
min: poolMin,
},
logging,
});

// Test the connection
await sequelize.authenticate();
console.info('✅ Sequelize connection established successfully.');

// Use port 5432 by default
const port = options.port || 5432
const poolMin = options.poolMin || 0
const poolMax = options.poolMax || 10
const sslEnabled = options.sslEnabled || false
return sequelize;
} catch (error) {
console.error('❌ Error connecting to the database with Sequelize:', error.message);
throw new Error('Failed to connect using Sequelize.');
}
};

// Connect to the database
const sequelize = new Sequelize({
dialect: 'postgres',
/**
* Establishes a database connection using pure MySQL.
* @param options - Configuration for the connection.
* @returns MySQL connection instance.
*/
export const connectDatabaseMySQL = async (options: ConnectOptions): Promise<mysql.Connection> => {
const {
host,
port,
port = 3306, // Default MySQL port
username,
password,
database,
ssl: sslEnabled,
pool: {
max: poolMax,
min: poolMin,
},
sslEnabled = false,
logging,
})
} = options;

// Test the connection
await sequelize.authenticate()
try {
const connectionConfig: mysql.ConnectionOptions = {
host,
port,
user: username,
password,
database,
ssl: sslEnabled ? { rejectUnauthorized: false } : undefined,
};

// All good, return the connection
return sequelize
}
const connection = await mysql.createConnection(connectionConfig);

// Test the connection
await connection.ping();
console.info('✅ MySQL connection established successfully.');

// Log successful connection if logging function is provided
if (logging) logging('MySQL connection established.');

return connection;
} catch (error) {
console.error('❌ Error connecting to the database with MySQL:', error.message);
throw new Error('Failed to connect using MySQL.');
}
};

// Unified export for flexibility and scalability
export const DatabaseConnector = {
sequelize: connectDatabaseSequelize,
mysql: connectDatabaseMySQL,
};

/**
* Example Usage:
*
* import { DatabaseConnector } from './path-to-file';
*
* // For Sequelize (PostgreSQL)
* const sequelizeConnection = await DatabaseConnector.sequelize({
* host: 'localhost',
* username: 'user',
* password: 'password',
* database: 'my_database',
* });
*
* // For pure MySQL
* const mysqlConnection = await DatabaseConnector.mysql({
* host: 'localhost',
* username: 'user',
* password: 'password',
* database: 'my_database',
* });
*/
Loading