For a project that generates static HTML I needed a data source, so I created this simple library for storing data in JSON. It is intended only for CLI usage by a single client, not for websites!
- Install the latest version with
composer require mhujer/json-simple-db
- Use it according to the example bellow and check the docblocks
<?php
require_once 'vendor/autoload.php';
//initialize DB
$db = new JsonSimpleDb\Db('./foo');
//initialize table
if (!$db->tableExists('mytable')) {
$db->createTable('mytable');
}
$table = $db->getTable('mytable');
//get items count
$table->count(); //0
//insert into table
$table->insert([
'id' => '1',
'name' => 'foo',
]);
//find by array - like in MongoDB
$items = $table->find(['id' => '1']);
/*
array(1) {
[0] =>
array(2) {
'id' =>
string(1) "1"
'name' =>
string(3) "foo"
}
}
*/
//update record
$table->update(['id' => '1'], ['name' => 'boo']);
//delete record
$table->delete(['id' => '1']);
//persist the data to file - don't forget this :-)
$table->persist();
JSON SimpleDB works with PHP 5.6 or PHP 7.
Bugs and feature request are tracked on GitHub
Martin Hujer - mhujer@gmail.com - http://www.martinhujer.cz
- Stored JSON is pretty printed
- Added posibility to delete records
- Comparator does strict matching
- initial release