djaker-hakim / jmm
jmm json mini model this is a nosql file database
Requires
- php: ^8.2
- ext-pdo_sqlite: *
Requires (Dev)
- phpunit/phpunit: ^11.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
JMM is a small PHP library for working with JSON records through a lightweight, file-based database. It stores data in a single SQLite database file per database, with each table using a simple (id, body) schema where body contains the JSON record.
Status: experimental / mini storage library. It is best suited for small local datasets, prototypes, and simple tools. It is not a replacement for a full database application layer.
Features
- Single-file SQLite database storage
- Simple schema helpers for creating/dropping databases and tables
- Basic CRUD operations
- Fluent collection/query API (
where,select,sortBy, etc.) Recordobjects with convenient property access- Optional
Modelbase class for static/instance proxy calls
Requirements
- PHP
^8.2 ext-pdo_sqlite- Composer
Installation
composer require djaker-hakim/jmm
For local development in this repository:
composer install
Core concepts
JMM uses a single-file SQLite database layout:
<path>/
<database>.sqlite
Each JMM table is a SQLite table with this shape:
CREATE TABLE "users" ( id TEXT PRIMARY KEY, body TEXT NOT NULL );
- Database: one
.sqlitefile - Table: a SQLite table with
idandbodycolumns - Record: a JSON object stored in
body(including the primary key field)
Quick start
1. Create a database and table
<?php require 'vendor/autoload.php'; use stm\jmm\Schema; $schema = new Schema(__DIR__ . '/storage'); $schema->createDB('blog'); $schema->db('blog')->createTable('users');
This creates:
storage/
blog.sqlite
2. Insert records
<?php require 'vendor/autoload.php'; use stm\jmm\Database; $db = new Database(__DIR__ . '/storage'); $users = $db->db('blog')->table('users'); $users->create([ 'name' => 'Ada', 'email' => 'ada@example.com', ]);
By default, Database auto-generates a primary key (id) using uniqid().
3. Query records
$user = $db->db('blog')->table('users') ->where('name', 'Ada') ->first(); $adults = $db->db('blog')->table('users') ->where('age', '>=', 18) ->get(); $names = $db->db('blog')->table('users') ->select(['id', 'name']) ->get();
Records are loaded from the SQLite table and queried in memory by Collection. The id column is the primary key stored in SQLite; record fields live in the JSON body.
4. Update and delete
$users = $db->db('blog')->table('users'); // Update records matching the current query $users->where('id', $id)->update([ 'name' => 'Grace', ]); // Delete by primary key $users->delete($id); // Delete multiple primary keys $users->delete([$id1, $id2]);
API overview
Schema
Manages SQLite database files and tables.
use stm\jmm\Schema; $schema = new Schema(__DIR__ . '/storage'); $schema->createDB('blog'); // create blog.sqlite $schema->db('blog')->createTable('users'); // create users table (id, body) $schema->db('blog')->dropTable('users'); // drop users table $schema->dropDB('blog'); // delete blog.sqlite
Database
Extends Collection and handles reading/writing records in a SQLite table.
use stm\jmm\Database; $db = new Database(__DIR__ . '/storage'); $db->primaryKey = 'id'; // default: id $db->autoGenerate = true; // default: true $db->columns = ['name']; // optional required-column validation $users = $db->db('blog')->table('users'); $record = $users->create(['name' => 'Ada']);
Main methods:
db(string $name): statictable(string $name): staticcreate(array $data): Recordupdate(array $data): arraydelete(array|string|int $ids = ''): array|Record
Collection
Provides in-memory query and transformation helpers.
Common methods:
all(),get(),first(),last(),count(),isEmpty()where($key, $value)/where($key, $operator, $value)orWhere(...)select(['col1', 'col2'])filter(callable $callback)map(callable $callback, ...$arrays)sortBy($column, 'asc'|'desc')sortKeys('asc'|'desc')push($record),pop(),destroy()toJson(),toObject()
Supported where operators: ==, <, >, >=, <=, !=, like.
Record
Wraps a single associative record.
$record = $users->first(); $record->name; // property access $record->name = 'Grace'; // property assignment $record->toArray(); $record->toJson(); $record->has('name'); $record->only(['id', 'name']); $record->except(['email']);
Model
Base class for simple model-style access. Subclass it and configure properties:
<?php use stm\jmm\Model; class User extends Model { public string $path = __DIR__ . '/storage'; public string $db = 'blog'; public string $table = 'users'; public string $primaryKey = 'id'; public bool $autoGenerate = true; public array $columns = ['name', 'email']; } User::create(['name' => 'Ada', 'email' => 'ada@example.com']); $users = User::all();
Development
Run tests
composer install
composer test
The test suite uses PHPUnit.
Limitations / current caveats
- Field queries are in memory: records are loaded from SQLite and filtered by
Collection; there is no SQL field indexing/pushdown for JSON body fields. - Small-dataset focus: SQLite improves durability over plain JSON files, but JMM still loads table records into memory for collection queries.
- Simple ID model: the default design assumes a string primary key named
id. - Experimental API: behavior should be validated by tests before production use.
License
MIT