Introducing MonkeyslegionDB: A Specialized Database Connection Manager for PHP

yorchperaza

Jorge Peraza

Posted on August 31, 2023

Introducing MonkeyslegionDB: A Specialized Database Connection Manager for PHP

MonkeyslegionDB serves as a specialized database connection manager tailored for the Monkeyslegion PHP framework. It offers an extensive toolkit for managing database connections, constructing queries, executing transactions, and maintaining logs.

🌟 Features

  • Connection Pooling: Optimize your database connections for reuse.
  • Query Builder: Create SQL queries in a programmatic way.
  • Transactions: Perform a set of operations as a single unit.
  • Logging: Keep track of SQL queries and debug effortlessly.

🛠 Requirements

  • PHP 8.2 or higher
  • Composer for managing dependencies

📦 Installation

To get started with MonkeyslegionDB, execute the following Composer command:

composer require monkeyslegion/monkeyslegiondb
Enter fullscreen mode Exit fullscreen mode

🚀 Usage

Connection Pooling

Before using the ConnectionPool, you'll need to set the database configuration. You can do this once, perhaps in your framework's bootstrap file.

use Monkeyslegion\\MonkeyslegionDB\\ConnectionPool;

ConnectionPool::setConfig([
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'your_database',
    'username' => 'your_username',
    'password' => 'your_password',
    'options' => [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]
]);
Enter fullscreen mode Exit fullscreen mode

To get a connection:

use Monkeyslegion\\MonkeyslegionDB\\ConnectionPool;

$connection = ConnectionPool::getConnection();
Enter fullscreen mode Exit fullscreen mode

After you're done with the connection, you can release it back to the pool:

ConnectionPool::releaseConnection($connection);
Enter fullscreen mode Exit fullscreen mode

Functions

  • setConfig(array $config): Configure your database settings.
  • getConnection(): Fetch a PDO connection from the pool.
  • releaseConnection(PDO $connection): Return a PDO connection to the pool.

Query Building

use Monkeyslegion\\MonkeyslegionDB\\QueryBuilder;

$query = (new QueryBuilder())
.select('*')
.from('users')
.where('id', '=', 1)
.build();
Enter fullscreen mode Exit fullscreen mode

Functions

  • select(string $columns): Choose the columns to select.
  • from(string $table): Specify the table to query.
  • where(string $column, string $operator, mixed $value): Add a WHERE clause.
  • build(): Generate the SQL query.

Transactions

use Monkeyslegion\\MonkeyslegionDB\\Transaction;

$transaction = new Transaction($connection);
$transaction->begin();
// ... perform operations
$transaction->commit();
Enter fullscreen mode Exit fullscreen mode

Functions

  • begin(): Start a new transaction.
  • commit(): Finalize the current transaction.
  • rollback(): Undo the current transaction.

Logging

use Monkeyslegion\\MonkeyslegionDB\\Logger;

$dbLogger = new Logger($yourPsr3Logger);
$dbLogger->logQuery('SELECT * FROM users WHERE id = ?', [1]);
Enter fullscreen mode Exit fullscreen mode

Functions

  • logQuery(string $query, array $params = []): Log an executed SQL query.
  • logError(string $message): Log any error messages.

📚 Documentation

For in-depth documentation, please visit our official documentation.

🤝 Contributing

We're open to contributions! Check out our contributing guidelines for more information.

📜 License

MonkeyslegionDB is open-source and licensed under the MIT license.

💖 💪 🙅 🚩
yorchperaza
Jorge Peraza

Posted on August 31, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related