PHP code example of web-complete / micro-db

1. Go to this page and download the library: Download web-complete/micro-db library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

web-complete / micro-db example snippets


$microDb = new MicroDb(__DIR__ . '/storage', 'mydb1');

$microDb->setType('runtime');

$usersCollection = $microDb->getCollection('users');

$id = $usersCollection->insert(['name' => 'John Smith', 'some_data' => [1,2,3]]);

$id = $usersCollection->insert(['name' => 'John Smith', 'some_data' => [1,2,3]], "uid");

$usersCollection->insertBatch(
    ['name' => 'John Smith 1', 'some_data' => [1,2,3]],
    ['name' => 'John Smith 2', 'some_data' => [3,4,5]],
    ['name' => 'John Smith 3', 'some_data' => [5,6,7]],
);

$filter = function ($item) {
    return $item['id'] == 2;
};
$usersCollection->update($filter, ['name' => 'John Smith 2 updated']);

$filter = function ($item) {
    return $item['last_visit'] < $newYear;
};
$usersCollection->update($filter, ['active' => false]);

$filter = function ($item) {
    return $item['id'] == 2;
};
$usersCollection->delete($filter);

$filter = function ($item) {
    return $item['active'] == true;
};
$activeUsers = $usersCollection->fetchAll($filter);

$filter = function ($item) {
    return $item['active'] == true;
};
$sort = function ($item1, $item2) {
    return $item1['last_visit'] <=> $item2['last_visit'];
};
$activeUsers = $usersCollection->fetchAll($filter, $sort);

...
$activeUsers = $usersCollection->fetchAll($filter, $sort, 20, 100);

...
$activeUser = $usersCollection->fetchOne($filter, $sort);

$user = $usersCollection->fetchOne(function ($item) {
    return $item['id'] == 15;
});

$collection->drop();