Skip to content

Configuration

Jason Napolitano edited this page Aug 29, 2023 · 7 revisions

DatabaseFactory is extremely customizable. This means, that we can customize everything from the way we handle connections, to how we execute queries. All of this can be done by using a custom configuration class and passing it into the instance of DatabaseFactory we are working with.

Creating Config Classes

Configuration classes are classes that we can use to change the default behavior of DatabaseFactory. All configuration classes we pass into our library MUST extend DatabaseFactory\Config in order for DatabaseFactory to register them.

Developer's Note:

If a custom configuration class does not extend the base configuration class, a QueryBuilderException will be thrown. Below is an example of an empty configuration class that complies with this rule.

class CustomConfig extends DatabaseFactory\Config
{
    // ...
}

Once this class has been created, we can use it to modify the default behavior of DatabaseFactory, as we'll see below.

Creating Query Modules

DatabaseFactory is unique in that it uses a modular approach to query execution. This means that each statement that is executed, is in fact a separate module, or class. In order to inject your own custom modules, you can build your configuration class like so:

class CustomConfig extends DatabaseFactory\Config
{
    protected static array $modules = [
        // ...
    ];
}

In the above example, the $modules array has been added into our custom configuration class. With this, we can assign custom queries to be executed when we run the default query builder or the ORM. For example, below is a custom JOIN module.

Developer's Note:

It is important to notice that all custom modules, MUST extend the DatabaseFactory\Config\BaseBuilder class, and also MUST implement the DatabaseFactory\Contracts\SQLStatementInterface in order for them to be registered by the library.

class CustomJoin extends DatabaseFactory\Modules\BaseBuilder implements DatabaseFactory\Contracts\SQLStatementInterface
{
    public function statement(string $table, ...$params): string
    {
         return static::select($params[2] ?? '*') .
                static::from($table) . static::RIGHT .
                static::join($params[0], $params[1]);
    }
}

To implement this module all we need to do is add it to the $modules array, like so:

protected static array $modules = [
    'join' => CustomJoin::class
];

Now, every time we call the join() method when using the ORM or the query builder, we will instead be executing the custom query. Take note that this custom query will only be executed on our current DatabaseFactory instance, and not any new ones we create.

The next question would be; How do we extend our current DatabaseFactory instance and assign our custom configuration class, to utilize these features? Continue reading and we'll discuss that next.

Implementation

In order to extend DatabaseFactory's default behavior, we will need to add the new configuration class to our current library instance. This can very easily be done in one of two ways.

# assign it on the Facades\DB::table() method
$users = DatabaseFactory\Facades\DB::table('users', new CustomConfig());

# OR

# assign it to the db_factory() helper
$users = db_factory('users', new CustomConfig())
# then, build your query ...
$users->join('users_roles', ['users.id', 'users_roles.user_id']);
Clone this wiki locally