Download the PHP package peak/database without Composer
On this page you can find all versions of the php package peak/database. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download peak/database
More information about peak/database
Files in peak/database
Informations about the package database
Peak/Database
The purposes of this package are:
- provide generic agnostic database tools for DDD and Clean architecture
- provide database migration with Phinx migration
- facilitate the integration of Laravel Database in non-laravel project
Installation
composer require peak/database
Laravel Database Usage
That's it! Check out Laravel Query Builder for more info on how to make queries.
Database Migration Usage
For database migrations, create a file at the root of your project name . This file should return an array of Phinx configuration.
This phinx.php
above will allow the usage of Laravel Database directly in your migrations with the help of LaravelPhinxMigration
:
Generic tools
The purpose of generic tools is to help you express a query without being attached to a particular database framework or any database at all.
- Use
QueryFilters
to express "where" statements. - Use
QueryPagination
to express pagination like order by and limit/offset statement.
It is important to note that those generic tools doesn't to do anything by themselves. They serve mainly as "boundary" interfaces between a domain or use case and your actual real database or storage implementation. In your final implementation, you will need builder/helper to translate generic expression to your actual database framework/orm.
Example of creating generic query "where" filters and generic query "pagination"
Pass the $queryFilters
and $queryPagination
to a use case. This will help to create a boundary between use cases and repositories because the use case doesn't have to know the details of your implementation (database framework/orm, etc)
And finally, we use LaravelGenericHelper
in our repository implementation to transform generic QueryFiltersInterface
to actual laravel query builder "where" expressions;
We could simply use laravel query builder directly in our use case but this could also tie the code to much to specific database library (here laravel database). By using generic query filters and pagination, it becomes really easy to tests repository and use cases without a real database connection.
Important security information on pagination and filters with Laravel Database
From Laravel Database docs:
The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings. But:
"PDO does not support binding column names. Therefore, you should never allow user input to dictate the column names referenced by your queries, including "order by" columns, etc. If you must allow the user to select certain columns to query against, always validate the column names against a white-list of allowed columns."
If you let your user choose the column names, you should create a class that extends AbstractRestrictedQueryPagination
to protected from unwanted column names.
The same can be applied to query filters columns and operators with AbstractRestrictedQueryFilters
: