nemirovskiy/medoo-repository

Base repository implementation for Medoo with declarative table schema, strict field/type validation and simple schema installation.

Maintainers

Package info

github.com/Nemirovskiy/Medoo_repository

Homepage

Issues

pkg:composer/nemirovskiy/medoo-repository

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

0.1.0 2026-03-11 05:07 UTC

This package is auto-updated.

Last update: 2026-05-11 05:44:55 UTC


README

Base repository implementation for Medoo that:

  • Stores table schema (fields, types and options) in PHP arrays.
  • Validates fields and types on create/update operations.
  • Provides simple table installation (auto‑generated CREATE TABLE SQL from schema).

This library is useful when you want a lightweight, type‑checked repository layer on top of Medoo, without a full ORM.

Installation

Install via Composer:

composer require nemirovskiy/medoo-repository

Or, when developing locally, add the repository to your composer.json and require it.

Basic usage

1. Configure Medoo

use Medoo\Medoo;

$database = new Medoo([
    'type' => 'mysql',
    'host' => '127.0.0.1',
    'database' => 'test',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8mb4',
]);

2. Extend BaseRepository

namespace App\Repository;

use Medoo\Medoo;
use Medoo\Repository\BaseRepository;

final class UserRepository extends BaseRepository
{
    public function __construct(Medoo $db)
    {
        parent::__construct($db);

        $this->table = 'users';

        $this->schema = [
            'id' => [
                'type'           => 'int',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'email' => [
                'type'     => 'string',
                'length'   => 255,
                'nullable' => false,
                'unique'   => true,
            ],
            'name' => [
                'type'     => 'string',
                'length'   => 255,
                'nullable' => false,
            ],
            'age' => [
                'type'     => 'int',
                'unsigned' => true,
                'nullable' => true,
            ],
            'active' => [
                'type'     => 'bool',
                'nullable' => false,
                'default'  => 1,
            ],
            'created_at' => [
                'type'     => 'datetime',
                'nullable' => false,
                'default'  => 'CURRENT_TIMESTAMP',
            ],
        ];

        $this->fillable = ['email', 'name', 'age', 'active'];

        $this->required = ['email', 'name'];
    }
}

3. Install table and use the repository

use App\Repository\UserRepository;

$users = new UserRepository($database);

// Create table if it does not exist
$users->install();

// Create user (fields and types are validated)
$id = $users->create([
    'email' => 'john.doe@example.com',
    'name'  => 'John Doe',
    'age'   => 30,
]);

// Load user
$user = $users->findById($id);

// Update user
$users->update($id, [
    'age'    => 31,
    'active' => false,
]);

// Delete user
// $users->delete($id);

Key features

  • Declarative schema
    Define your table columns in a PHP array: type, length, nullable, default, unique, indexes, auto_increment, etc.

  • Strict validation
    On create and update:

    • Only fillable fields are accepted.
    • Required fields must be present.
    • Types are strictly validated (int, float, bool, string, datetime).
    • Values outside schema cause an exception.
  • Schema installation (MySQL only)
    install() builds a CREATE TABLE IF NOT EXISTS SQL statement based on schema and executes it via Medoo.
    Only for MySQL/MariaDB. For PostgreSQL, SQLite or other databases, do not call install() / uninstall() — create and drop tables via your own migrations or override generateCreateTableSql().

Database support

  • CRUD (create, findById, update, delete) works with any database Medoo supports (MySQL, PostgreSQL, SQLite, etc.).
  • install() and uninstall() generate and run MySQL-only SQL. Use them only with MySQL/MariaDB; for other DBs use migrations or override the schema methods.

Examples

There are runnable examples in the examples/ directory:

  • examples/UserRepository.php – demo user repository
  • examples/demo.php – demo script that installs the table and performs basic CRUD

Run:

php examples/demo.php

Make sure your database configuration in demo.php matches your local environment.