punarinta / doru-db
A simple file-based database storing data in JSON.
Requires
- php: >=7.0.0
This package is not auto-updated.
Last update: 2025-03-30 05:33:32 UTC
README
A simple file-based database. Stores data in JSON format. Supports basic CRUD operations and concurrency. The name comes from a Proto-Indo-European word 'doru' which meant 'a tree'. This database is one tool in a bigger collection with this name.
Usage
Connecting to the database
use \DoruDB\Database; // if no arguments are passed to constructor the database is stored in 'db' folder $db = new Database('path/to/db');
Inserting a record
$user = $db->create('user', ['name' => 'That Guy']); // this will insert a document with a unique id and nothing else $user = $db->create('user'); // this will also insert a record, as update() works as 'upsert' if ID is given $user = $db->update('user', ['id' => 1337, 'name' => 'This Guy']);
Finding a record
// this is the fastest way to fetch a document, but you need to have an ID $user = $db->findById('user', 1337); // 'find' returns one record $users = $db->find('user', ['filter' => ['name' => 'This Guy']]); // 'findAll' returns all. NB: filter can use user-defined functions $users = $db->find('user', ['filter' => ['name' => function ($x) { return strpos($x, 'Guy') !== false; } ]]);
Limit/offset
$users = $db->findAll('user', ['offset' => 1, 'limit' => 1]);
Using limit/offset with filters on the fields that do not have indices is slower than just limit/offset or filters with indices, as the database has to read all documents first to apply limit and offset.
Sorting
Only sorting by ID is supported for now. Use 'invert' option to use descending sorting.
$users = $db->findAll('user', ['invert' => true]);
Removing a record
// deletes one document $db->delete('user', 1337); // deletes all documents $db->truncate('user');
Working with indices
Index support is still experimental. Use with care. Indices are used to speed up filter, limit and offset operations.
// add an index or update it automatically $db->rebuildIndex('user', 'lastLogin'); // update index manually with one or more documents $db->updateIndex('user', 'lastLogin', $someDocument); // remove index completely from the database $db->removeIndex('user', 'lastLogin');
ToDo
- make ID immutable
- unique indices
- improve index r/w performance
- sync index on document update
- counting with non-indexed filter
License
This project is licensed under the terms of MIT license.