zidbih/filequent

A lightweight file-based ORM for PHP using JSON files, inspired by Eloquent.

v0.2.0 2025-06-21 22:21 UTC

This package is auto-updated.

Last update: 2025-07-23 17:46:42 UTC


README

A lightweight, file-based ORM-like system for PHP
Developed by Zidbih

Filequent allows you to build and interact with data models in a clean, Eloquent-style syntax โ€” without using a traditional database. Data is saved to structured JSON files, making this ideal for local development, prototypes, lightweight apps, or embedded systems.

๐Ÿš€ Features

  • Eloquent-style model structure
  • Stores data as JSON files (no database required)
  • Simple CRUD support: create, find, all, update, delete, where, etc.
  • Relationship support: hasMany, hasOne, belongsTo
  • Default and custom foreign key handling
  • Extensible and easy to test
  • Fully tested with PHPUnit

๐Ÿ“ฆ Installation

Install via Composer:

composer require zidbih/filequent

๐Ÿงฑ Defining Models

Create model classes by extending Zidbih\Filequent\Filequent and define:

  • $collection โ€” JSON file name (without .json)
  • $basePath (optional) โ€” directory where the file is saved
use Zidbih\Filequent\Filequent;

class User extends Filequent
{
    protected static string $collection = 'users';
    protected static ?string $basePath = __DIR__ . '/data';

    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function latestPost()
    {
        return $this->hasOne(Post::class);
    }
}

class Post extends Filequent
{
    protected static string $collection = 'posts';
    protected static ?string $basePath = __DIR__ . '/data';

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

โœจ CRUD Operations

Create

$user = User::create(['name' => 'Alice']);

Read

$found = User::find($user->getAttribute('id'));// by ID
$allUsers = User::all();// all records

Update

$user->update(['name' => 'Updated']);

Delete

$user->delete();

๐Ÿ” Query Builder

Filequent supports method chaining similar to Eloquent:

$results = User::where('name', '=', 'Alice')->get();

$firstMatch = User::where('age', '>', 25)->first();

$likeMatches = User::where('name', 'LIKE', 'Ah%')->get();

Supported operators: =, !=, >, <, LIKE

๐Ÿ”— Relationships

hasMany

$user = User::find(1);
$posts = $user->posts(); // Returns all posts with user_id = $user->id

hasOne

$post = $user->latestPost(); // Returns one related Post

belongsTo

$post = Post::find(1);
$user = $post->user(); // Returns the User who owns the post

Custom Foreign Key

You can specify a custom foreign key:

return $this->belongsTo(User::class, 'custom_user_id');

๐Ÿ’พ Where Is Data Stored?

By default, JSON files are saved in a /data folder under the project root.

your-project/
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ users.json
โ”‚   โ””โ”€โ”€ posts.json

You can override this using the static $basePath property on each model.

๐Ÿงช Testing

Filequent includes a fully-featured test suite using PHPUnit.

Setup

vendor/bin/phpunit

Structure

tests/
โ”œโ”€โ”€ FilequentTest.php
โ”œโ”€โ”€ testData/
โ”‚   โ”œโ”€โ”€ users.json
โ”‚   โ””โ”€โ”€ posts.json

The tests cover:

  • File-level operations (insert/read)
  • All CRUD methods
  • Advanced query logic (chaining, LIKE)
  • Relationship resolution
  • Custom foreign keys
  • Error cases and edge handling

๐Ÿ“‚ Example Usage

$user = User::create(['name' => 'Ahmed']);

Post::create([
    'title' => 'Hello World',
    'body' => 'Welcome to Filequent!',
    'user_id' => $user->getAttribute("id"),
]);

// Retrieve all posts by this user
$posts = $user->posts();

// Get the user for a post
$post = Post::find(1);
$owner = $post->user();

๐Ÿง‘โ€๐Ÿ’ป Author

Zidbih
GitHub: https://github.com/medmahmoudhdaya

๐Ÿ“„ License

This package is open-sourced software licensed under the MIT license.

๐Ÿค Contributing

Pull requests, bug reports, and suggestions are welcome!

If you like the package, consider โญ starring it on GitHub and sharing it with others.

Filequent โ€” Simplify data storage, without a database.