Jisly

Jisly is a PHP lightweight NoSQL database library, flat file JSON that allow you to quickly start your project with the possibility of memory and flat file
storage using a NoSQL (document oriented) query syntax with managed concurrent access. Usually for small applications developers used to choose Sqlite as a quick and easy to use file database, which could also scale. But having a NoSQL database could also be an interesting alternative especially if it’s using both flat file and memory.

Getting started with Jisly

Jisly relies on the PSR-4 specification for autoloading classes from file paths. You can install the library with Composer using:

composer require r0mdau/jisly:^2.0

Then to initiatilize the library you need to set the $directory which contains the path to the directory where the files (=collections) of the data model will be stored. Then we can access a collection by requesting the name of the collection that we want for example :

$database = new Jisly($directory);
$database->collection($name);

This will return a JislyCollection object. Notice that each first time you access a collection, all datas are stored in memory. Then you can do all usual database operation like insert, update, delete, select. For example to 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"
  ]
);

Notice that the insert return only boolean, if the write operation have been done successfully or not. To update or delete you will need the _rid of the document, so you will need to search the document to perform any operation on it :

// Find all documents in a collection
$results = $database->collection($file)->find();
// Find all documents with the name Lucas
$results = $database->collection($file)->find(
  [
    "name" => "Lucas"
  ]
);
// Find one document with the name 19
$result = $database->collection($file)->findOne(
  [
    "name" => 19
  ]
);

There are only two logical operators AND and OR that could used on the find and findOne methods :

$result = $database->collection($file)->find(
  [
    "firstname" => "Georges", 
    "name" => "Lucas"
  ], JislyCollection::LOGICAL_OR
);

$result = $database->collection($file)->findOne(
  [
    "firstname" => "Georges", 
    "name" => "Lucas"
  ], JislyCollection::LOGICAL_AND
);

The find method will retrun array of results with _rid as keys, or the first element of the array for findOne. So now you can perform Update and delete on the document using :

// Update Document $rid
$successBool = $database->collection($file)->update(
  $rid,
  [
    "firstname" => "Georges", 
    "name" => "Lucas"
  ]
);

// Then Delete it
$successBool = $database->collection($file)->delete($rid);

That’s all, as simple as that ! There is no advanced query features or Upsert like methods, so if you need something simple with only basic features Jisly will definitely be an interesting solution. The idea is that each time you CRUD something, all datas are stored in memory so the access will be fast, and all data are saved to filesystem for dat aconsistency.

Jisly is an open source PHP library released under Apache-2.0 License. More information at https://github.com/r0mdau/jisly

LEAVE A REPLY

Please enter your comment!
Please enter your name here