Database Integration

Lithe offers flexibility in connecting to various databases. Integrating seamlessly with popular ORMs like Eloquent, Lithe also supports native PHP drivers such as MySQLi and PDO, ensuring broad compatibility with various database management systems.

Environment Configuration

To configure the database connection environment in Lithe, you can adjust the environment variables present in the .env file at the root of your project:

DB_CONNECTION_METHOD=pdo
DB_CONNECTION=mysql
DB_HOST=localhost
DB_NAME=lithe
DB_USERNAME=root
DB_PASSWORD=
DB_SHOULD_INITIATE=true

Where:

  • DB_CONNECTION_METHOD: Defines the method of database connection used by Lithe. It can be configured to eloquent, pdo, mysqli, or other custom connection methods defined in your application.

  • DB_CONNECTION: Specifies the type of database that Lithe should connect to. For example, mysql, pgsql, sqlite, among others.

  • DB_HOST: Indicates the address of the server where the database is hosted. Typically set to localhost for local connections or an IP address for remote connections.

  • DB_NAME: The name of the database that Lithe should use to store application data.

  • DB_USERNAME: The username used to authenticate the connection to the database.

  • DB_PASSWORD: The password associated with the username for authenticating the connection to the database. If left blank, it indicates that no password is configured for the specified user.

  • DB_SHOULD_INITIATE: Indicates whether Lithe should automatically initiate the database connection when loading the application. Can be set to true for automatic initiation or false for manual initiation at specific points in the code.

These configurations are crucial for establishing an effective and secure connection with the defined database, ensuring that Lithe can interact with application data appropriately during development and execution.

Be sure to replace placeholders (localhost, lithe, root, and empty password) with your actual database credentials. It is highly recommended to set a strong password to protect your credentials and ensure the security of the database connection.

Configuration for Eloquent

To configure Lithe to use Eloquent, follow these steps:

Step 1: Set the environment variables in the .env file:

DB_CONNECTION_METHOD=eloquent
DB_CONNECTION=mysql

Step 2: Install Eloquent using Composer:

composer require illuminate/database

Learn more about Eloquent at: Complete Eloquent Documentation

Configuration for MySQLi

To configure Lithe to use MySQLi, set the environment variables as follows:

Step 1: Configure the environment variables in the .env file:

DB_CONNECTION_METHOD=mysqli
DB_CONNECTION=mysql

Learn more about MySQLi in the PHP documentation: PHP Manual - MySQLi

These detailed steps make it easy to configure each ORM or database driver with Lithe, allowing you to choose and set up the most suitable connection method for your application.


Connection Initialization

After configuring the .env file with the necessary database connection variables, Lithe uses the Manager class to load these configurations and establish the corresponding connection.

\Lithe\Database\Manager Class and initialize Method

The Manager class in Lithe is a component responsible for managing database connection operations. It provides a crucial method called initialize, which initializes and returns the configured connection, ready to be used by the application.

The initialize method prepares and establishes the connection as configured by the environment variables. This includes setting up the connection type (eloquent, pdo, mysqli) and specific database details (such as host, database name, username, and password).

Using the initialize Method

In the src/database/config/database.php file, the initialize method is called by default to configure the database connection:

use Lithe\Database\Manager as DB;

return DB::initialize();

This method returns the configured connection, ready to be used by the application. It is essential that this method is present and called to ensure that the connection is available for database operations like migrations.

Application Usage

After initializing the connection using the static initialize() method of the Manager class, the configured connection will be available via the DB_CONNECTION constant. This allows you to execute database queries and operations efficiently and securely.

Using Eloquent for queries:

use Illuminate\Database\Capsule\Manager as Database;

$users = Database::table('users')->get();

Using conventional methods with PDO:

$pdo = DB_CONNECTION;
$users = $pdo->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC);

This approach facilitates Lithe's integration with different database technologies, providing flexibility and efficiency in web application development.


Configuring Other ORMs or Drivers

To use other ORMs or database drivers with Lithe, you can configure them flexibly using the static configure method of the Lithe\Database\Manager class in the src/database/config/database.php file. This method allows you to add new connection configurations in a customized manner.

Structure of the configure Method

The configure method takes two parameters:

  1. Configuration Name: A unique identifier for your configuration, which will be used later to reference this connection.

  2. Configuration Callback: A callback function that receives a configuration array as a parameter. Inside the callback, you can define and return the configured instance of your ORM or database driver.

Custom Configuration

In the src/database/config/database.php file, add a custom configuration as follows:

use Lithe\Database\Manager as DB;

DB::configure('my_custom_orm', function ($config) {
    // Custom configuration for your ORM or driver
    return new MyCustomORM([
        'driver' => $config['driver'],
        'host' => $config['host'],
        'database' => $config['name'],
        'username' => $config['username'],
        'password' => $config['password'],
    ]);
});

In the example above, my_custom_orm is the name of the configuration you are creating. The callback receives a $config array containing the connection details (such as connection type, host, database name, username, and password), which are used to initialize and return the instance of your custom ORM or database driver.

Integration with Environment Variables

After configuring the configure method in the database.php file, you need to associate the specific database details with environment variables in the .env file.

DB_CONNECTION_METHOD=my_custom_orm
DB_CONNECTION=mysql
DB_HOST=localhost
DB_NAME=my_database
DB_USERNAME=root
DB_PASSWORD=

After setting up the .env, Lithe will automatically load these environment variables during application initialization. The Lithe Manager class will use these configurations to initialize and establish the connection to the database as configured.

This process ensures that the configuration defined in the configure method is used in conjunction with environment variables, providing flexibility and efficient management of custom connections in your Lithe project.


Troubleshooting

If you encounter issues connecting to the database:

  • Check if the configurations and environment variables are correct.
  • Verify for any syntax errors in your queries or SQL code.

Lithe offers a simple and flexible approach to managing connections with various databases and ORMs, facilitating the development of robust applications.