A simple file-based database storing data in JSON.
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., (*1)
use \DoruDB\Database; // if no arguments are passed to constructor the database is stored in 'db' folder $db = new Database('path/to/db');
$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']);
// 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; } ]]);
$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., (*2)
Only sorting by ID is supported for now. Use 'invert' option to use descending sorting., (*3)
$users = $db->findAll('user', ['invert' => true]);
// deletes one document $db->delete('user', 1337); // deletes all documents $db->truncate('user');
Index support is still experimental. Use with care. Indices are used to speed up filter, limit and offset operations., (*4)
// 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');
This project is licensed under the terms of MIT license., (*5)