The smallest PHP lightweight NoSQL database library, flat file JSON.
The main goal of Jisly is to allow you to quickly start your project with the possibility of memory and flat-file storage using a NoSQL (document oriented) query syntax.
Concurrent access is managed !
This lib can be found on Packagist
composer require r0mdau/jisly:^3.0
Jisly relies on the PSR-4 specification for autoloading classes from file paths.
use Jisly\JislyCollection;
public function saveCart(): void {
$jisly = new JislyCollection("/tmp/data", "cart");
...
}
- Each document has a unique identifier called
_rid. Searches match_ridexactly ("0123"does not find"123"), while other attributes use loose comparison ("123"finds123). - Each collection is physically represented by a file.
- The files are stored in a single working directory. The Jisly class is instantiated with the path to this directory as a parameter.
- Each time you CRUD something, all datas are stored in memory.
- And datas are saved on filesystem.
$directory contains the path to the directory where the files (=collections) of the data model will be stored.
$database = new Jisly($directory);$name contains the name of the collection we want to request. Example : user.
Returns an object JislyCollection :
$database->collection($name);Warning : each first time you access a collection, all datas are stored in memory.
PREAMBLE :
The Insert, Update, Delete methods return a boolean, true if the action went well, false otherwise.
Insert the array into the specified collection in JSON format and assigns a unique _rid identifier to the document if
it has not been specified :
$successBool = $database->collection($file)->insert(
[
"name" => "Lucas",
"firstname" => "Georges"
]
);You must first find all documents to delete to provide the _rid attribute to the delete method.
Remove the only document in the collection which has the value $rid to the attribute _rid :
$successBool = $database->collection($file)->delete($rid);Returns all documents in the collection in an array() of objects :
$results = $database->collection($file)->find();Return all documents in the collection that have a name attribute with Lucas as value in an array() of objects :
$results = $database->collection($file)->find(
[
"name" => "Lucas"
]
);Return the first document that has a name attribute with 19 as an object value, or null if none matches :
$result = $database->collection($file)->findOne(
[
"name" => 19
]
);These two logical operators can be used on find and findOne methods.
If no logical operator is provided, OR is used.
Return all documents in the collection that have a name attribute with Lucas OR a firstname attribute with
Georges as values in an array() of objects :
$result = $database->collection($file)->find(
[
"firstname" => "Georges",
"name" => "Lucas"
], JislyCollection::LOGICAL_OR
);Return the first document in the collection that have a name attribute with Lucas AND a firstname attribute with
Georges as an object value :
$result = $database->collection($file)->findOne(
[
"firstname" => "Georges",
"name" => "Lucas"
], JislyCollection::LOGICAL_AND
);For the modification, the documents concerned are entirely replaced by the second array() given in parameter. The
document keeps its _rid, even if the array omits it or gives another one.
You must first find all the documents to replace to provide the _rid attribute to the update method.
Modify the only document in the collection whose value $rid to the _rid attribute :
$successBool = $database->collection($file)->update(
$rid,
[
"firstname" => "Georges",
"name" => "Lucas"
]
);The benchmark suite lives in benchmark/ and runs with PHPBench:
composer bench
Each operation is timed against collections of 100, 1,000 and 10,000 documents. Indicative results (mode per call, PHP 8.3, x86_64 cloud VM, local disk):
| Operation | 100 docs | 1,000 docs | 10,000 docs |
|---|---|---|---|
| Open a collection | 0.13 ms | 1.2 ms | 15 ms |
insert() |
0.01 ms | 0.01 ms | 0.01 ms |
find() all |
< 1 µs | < 1 µs | < 1 µs |
find() with OR |
0.06 ms | 0.6 ms | 7.7 ms |
find() with AND |
0.07 ms | 0.6 ms | 8.0 ms |
findOne() by _rid |
< 2 µs | < 2 µs | < 2 µs |
update() |
0.63 ms | 3.0 ms | 36 ms |
delete() |
0.63 ms | 3.2 ms | 32 ms |
Opening a collection reads the whole file, searches scan every document, and update() / delete() rewrite the
whole file, so these grow linearly with the collection size. insert() only appends a line and a search on _rid
only looks up that document, so their cost stays flat.