PHPackages                             nemirovskiy/medoo-repository - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. nemirovskiy/medoo-repository

ActiveLibrary[Database &amp; ORM](/categories/database)

nemirovskiy/medoo-repository
============================

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

0.1.0(3mo ago)03MITPHPPHP &gt;=8.2

Since Mar 11Pushed 3mo agoCompare

[ Source](https://github.com/Nemirovskiy/Medoo_repository)[ Packagist](https://packagist.org/packages/nemirovskiy/medoo-repository)[ Docs](https://github.com/nemirovskiy/medoo-repository)[ RSS](/packages/nemirovskiy-medoo-repository/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Medoo Repository Base
---------------------

[](#medoo-repository-base)

Base repository implementation for [Medoo](https://medoo.in) 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
------------

[](#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
-----------

[](#basic-usage)

### 1. Configure Medoo

[](#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`

[](#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

[](#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
------------

[](#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
----------------

[](#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
--------

[](#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.

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance80

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

104d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4c1cb510d88f0fb6bb96d191fcecd37c30d5e9a13657201cf5a08fc94f5c55f9?d=identicon)[It-Nikola](/maintainers/It-Nikola)

---

Top Contributors

[![Nemirovskiy](https://avatars.githubusercontent.com/u/28603108?v=4)](https://github.com/Nemirovskiy "Nemirovskiy (2 commits)")

---

Tags

phpdatabaseormrepositorymedoo

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nemirovskiy-medoo-repository/health.svg)

```
[![Health](https://phpackages.com/badges/nemirovskiy-medoo-repository/health.svg)](https://phpackages.com/packages/nemirovskiy-medoo-repository)
```

###  Alternatives

[crcms/repository

Detached model and controller warehouse data provider

674.4k5](/packages/crcms-repository)[larachimp/mango-repo

Simple repository package for Laravel 5.

317.2k](/packages/larachimp-mango-repo)[modul-is/orm

Lightweight hybrid ORM/Explorer

1118.7k](/packages/modul-is-orm)[bauer01/unimapper

Universal mapping tool for collecting data from different sources

102.6k6](/packages/bauer01-unimapper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
