patryknamyslak / patbase
A highly configurable DB facade and Database interaction interface (Patbase)
Installs: 73
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/patryknamyslak/patbase
Requires
- ext-pdo: *
- vlucas/phpdotenv: ^5.6
README
Stop hardcoding database drivers. Patbase provides a clean, type-safe interface for working with any PDO-supported database.
Features
✅ DB Facade - DB Facade + Query builder for faster ship times! \n
✅ 9 Database Drivers - MySQL, PostgreSQL, SQLite, SQL Server, Oracle, and more
✅ Type-Safe - PHP 8.4 enums prevent invalid drivers
✅ Zero Config - Automatic DSN generation and default ports
✅ Lazy Loading - Connect only when needed
✅ Clean API - Fluent, modern PHP interface
✅ Exception Handling - Detailed error messages
Installation
composer require patryknamyslak/patbase
Quick Start
DB Facade + Query Builder
use PatrykNamyslak\Patbase\Facades\DB; use PatrykNamyslak\Patbase\Enums\WhereOperator; // SELECT * FROM `blog` WHERE views < :views; $selectQuery = DB::select([*])->from("blog")->where("views", WhereOperator::LESS_THAN, 500)->all() // The all() method runs the query by automatically swapping in the parameters in for the value you set in the where part of the chain! DB::insert()->into("table")->set("title", "New Post title"); DB::upsert(["title"])->into("table")->...; // OR DB::insertOrUpdate() DB::delete()->from("blog")->where("title", WhereOperator::LIKE, "Flag API"); DB::update()->table("table")->...;
Patbase - PDO Abstraction
MySQL
use PatrykNamyslak\Patbase\Patbase; use PatrykNamyslak\Patbase\Enums\DatabaseDriver; $db = new Patbase( driver: DatabaseDriver::MYSQL, database: 'myapp', username: 'root', password: 'Password123@', host: 'localhost', port: NULL, // NULL (or omit as the default is NULL) or your custom port, NULL will default to the port based on driver type i.e MYSQL = 3306 ); $users = $db->query("SELECT * FROM `users`;")->fetchAll();
PostgreSQL
$db = new Patbase( driver: DatabaseDriver::POSTGRES, database: 'myapp', username: 'postgres', password: 'secret', host: 'localhost' // port: 5432 (automatic) );
SQLite
$db = new Patbase( driver: DatabaseDriver::SQL_LITE, database: './database.db' // No username/password needed );
Prepared Statements
$user = $db->prepare( "SELECT * FROM users WHERE email = :email", [':email' => 'user@example.com'] )->fetch();
Lazy Loading
// Don't connect yet $db = new Patbase( driver: DatabaseDriver::MYSQL, database: 'myapp', username: 'root', password: 'secret', autoConnect: false ); // Optionally modify DSN $db->dsn('mysql:host=127.0.0.1;port=3307;dbname=myapp'); // Connect when ready (or auto-connects on first query) $db->connect();
Supported Databases
| Database | Driver Enum | Default Port |
|---|---|---|
| MySQL / MariaDB | DatabaseDriver::MYSQL |
3306 |
| PostgreSQL | DatabaseDriver::POSTGRES |
5432 |
| SQLite | DatabaseDriver::SQL_LITE |
N/A (file) |
| SQL Server | DatabaseDriver::MS_SQL_SERVER |
1433 |
| SQL Server (Linux) | DatabaseDriver::MS_SQL_SERVER_LINUX |
1433 |
| Oracle | DatabaseDriver::ORACLE |
1521 |
| Firebird | DatabaseDriver::FIREBIRD |
3050 |
| IBM DB2 | DatabaseDriver::IBM_DB2 |
50000 |
| ODBC | DatabaseDriver::OPEN_DATABASE_CONNECTIVITY |
50000 |
Advanced Usage
Custom PDO Options
$db = new Patbase( driver: DatabaseDriver::MYSQL, database: 'myapp', username: 'root', password: 'secret', options: [ PDO::ATTR_PERSISTENT => true, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4" ] );
Check Connection Status
if (!$db->isConnected()) { $db->connect(); }
Access PDO Directly (Advanced)
$pdo = $db->connection(); // Use native PDO methods if needed
License
MIT
Author
Patryk Namyslak - GitHub
AI Notice
This codebase is not written by Artificial intelligence, Only PARTS of the README were generated by Copilot but it used my documented code as reference to make it, so you can say it just turned my scattered documentation into one concise doc.