FlatDB is a key based storage engine. FlatDB is suitable for write and retrieve small sets of data that can persist between the application execution cycle.
FlatDB was designed as a lightweight method for storing reduced quantity of data. For large scale data storage that requires advanced search capabilities it is recommended to use a RDBMS like MariaDB or PostgreSQL.
The FlatDB core library contains two base drivers:
Storage the data into a file using the native PHP serialization format.
The class for this driver is: FlatDB_Serialize.
Storage the data into a file using JSON format.
The class for this driver is: FlatDB_JSON.
When a new instance is created, the driver object should be attached using depency injection.
Examples:
// FlatDB instance using the JSON driver (Singleton)
FlatDB::instance()->attach(new FlatDB_JSON('database.json'));
// FlatDB instance using the Serialization driver
$flatdb = new FlatDB();
$flatdb->attach(new FlatDB_Serialize('database.ser');
It is possible to instantiate different FlatDB instances. In order to achieve that one unique identifier should be passed to the instance method. Example:
// First instance
FlatDB::instance('db1')->attach(new FlatDB_Serialize('database1.ser'));
// Second instance
FlatDB::instance('db2')->attach(new FlatDB_Serialize('database2.ser'));
// Third instance
FlatDB::instance('db3')->attach(new FlatDB_JSON('database3.json'));
// Write data in memory
FlatDB::instance()->write(['level1' => ['level2' => 'foo']]);
// Save to disk
FlatDB::instance()->flush();
// Read everything
$data = FlatDB::instance()->read();
// Write changes to persistent storage
$data = FlatDB::instance()->read('level1.level2');
Update and Delete is basically overwrite the array content. Example:
// Read everything
$data = FlatDB::instance()->read();
// Update
$data['level1']['level2'] = 'bar';
FlatDB::instance()->write($data);
// Delete
unset($data['level1']['level2'];
FlatDB::instance()->write($data);
// Write changes to persistent storage
FlatDB::instance->flush();
It possible to detach a driver and replace it with another one. Example:
// Attach a driver
FlatDB::instance()->attach(new FlatDB_Serialize('database.ser'));
// Detach the driver (Note that data in memory is deleted)
FlatDB::instance()->detach();
// Attach a new driver
FlatDB::instance()->attach(new FlatDB_JSON('database.ser'));