memran / marwa-db
Framework-agnostic, PSR-centric DB library with query builder, ORM, schema and migrations. Ships a CLI using symfony/console.
v1.0.0
2023-05-01 19:54 UTC
Requires
- php: >= 7.2.0
- ext-pdo: *
Requires (Dev)
- phpstan/phpstan: ^0.10.3
- phpunit/phpunit: 8
- squizlabs/php_codesniffer: ^3.3
- symfony/var-dumper: 4.4.x-dev
README
memran/marwa-db is a PSR-compliant, framework-agnostic, Laravel-style database library built on top of PDO.
It includes a fluent query builder, Eloquent-style ORM, schema builder, migrations, and connection load balancing.
📌 Features
- Multiple Connections with load balancing & retry policies
- Fluent Query Builder — chainable, secure, prepared statements
- Eloquent-style ORM:
- Auto timestamps (
created_at
,updated_at
) - Soft deletes
fillable
/guarded
attributes for mass assignment protection- Relationships:
hasOne
,hasMany
,belongsTo
,belongsToMany
- Eager loading (
with()
,load()
)
- Auto timestamps (
- Schema Builder:
- Create/drop tables
- Foreign keys
- Indexes (
primary
,unique
,index
) - Column modifiers (
nullable
,default
,after
)
- Migrations CLI:
make:migration
migrate
,migrate:rollback
,migrate:refresh
- Seeder Support with Faker
- Debug Panel — view executed queries & timings
- PSR-3 Logging integration
📦 Installation
composer require memran/marwa-db
⚙ Configuration
config/database.php
<?php return [ 'default' => [ 'driver' => 'mysql', 'host' => '127.0.0.1', 'port' => 3306, 'database' => 'app', 'username' => 'root', 'password' => '', 'charset' => 'utf8mb4', 'retry' => 3, 'retry_delay' => 300, 'debug' => true, ], ];
🚀 CLI Usage
php bin/marwa-db list
Create migration:
php bin/marwa-db make:migration create_users_table
Run migrations:
php bin/marwa-db migrate
Rollback:
php bin/marwa-db migrate:rollback
🛠 Query Builder Examples
use Marwa\DB\Facades\DB; $users = DB::table('users') ->where('status', 'active') ->orderBy('created_at', 'desc') ->limit(5) ->get();
DB::table('users')->insert([ 'name' => 'Jane Doe', 'email' => 'jane@example.com', ]);
🏷 ORM Examples
use App\Models\User; // Create record $user = User::create([ 'name' => 'John Doe', 'email' => 'john@example.com' ]); // Find & update $user = User::find(1); $user->email = 'new@example.com'; $user->save(); // Soft delete $user->delete();
🔗 Relationships
class User extends Model { public function posts() { return $this->hasMany(Post::class); } } class Post extends Model { public function author() { return $this->belongsTo(User::class, 'user_id'); } }
🏗 Schema Builder
use Marwa\DB\Schema\Schema; Schema::create('users', function($table) { $table->increments('id'); $table->string('name')->nullable(); $table->string('email')->unique(); $table->timestamps(); });
📋 Function Reference
Query Builder
table($name)
— Selects tableselect(...$columns)
— Selects specific columnswhere($column, $operator, $value)
— Adds WHERE clauseorWhere(...)
— Adds OR WHERE clauseorderBy($column, $direction)
— Sort resultsgroupBy($column)
— Group resultslimit($n)
— Limit rowsget()
— Fetch resultsfirst()
— Fetch first rowinsert($data)
— Insert new record(s)update($data)
— Update record(s)delete()
— Delete record(s)
ORM
find($id)
— Find by primary keyall()
— Get all rowscreate($attributes)
— Insert & return modelsave()
— Save changesdelete()
— Delete (with soft delete if enabled)with($relations)
— Eager load relations
Schema Builder
create($table, $callback)
— Create new tabledrop($table)
— Drop table- Column types:
string
,integer
,text
,boolean
,timestamp
, etc. - Modifiers:
nullable()
,default($value)
,after($column)
🐞 Debugging
Enable query debug in config:
'debug' => true
View queries:
use Marwa\DB\Support\DebugPanel; DebugPanel::render();
📜 License
MIT — See LICENSE for details.