irfan-dahir/folderdb

Flat-file JSON database

v1.0.1 2020-06-09 07:34 UTC

This package is auto-updated.

Last update: 2024-10-09 17:20:52 UTC


README

Version Average time to resolve an issue Average time to resolve an issue stable MIT License

FolderDB is a flat-file JSON database with usage similarity to MongoDB's PHP API.

It saves data into directories as files with key/value pairs in JSON format. The "key" is the file name where the "value" is JSON data.

FolderDB uses magic methods to automatically create or manage "collections"/directories.

composer require irfan-dahir/folderdb

$client->users->insert(
    'username',
    \FolderDb\Document::fromArray([
        'id' => '123',
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => 'john@example.com'
    ])
);

$user = $client->users->get('username');

echo $user->email; // "john@example.com"

// To Array
echo $user->toArray()['email'];

Getting Started

require_once __DIR__ .'/vendor/autoload.php';

// Create a client and pass the path to the database folder
$client = new \FolderDb\Client('/path/to/database');

Create a Folder

$client->users;

Insert data

$data = [
    'id' => '123',
    'first_name' => 'John',
    'last_name' => 'Doe',
    'email' => 'john@example.com'
];

// `new \FolderDb\Document()` takes JSON string directly, so we have to convert it to array
$client->users->insert(
    'username',
    \FolderDb\Document::fromArray($data)
);

Count

echo $client->users->count(); // 2

Get document

$user = $client->users->get('username'); // returns `\FolderDb\Document`

// Access your entry as an object
echo $user->email; // "john@example.com"


// Access your entry as an array
$userArray = $user->toArray();
echo $userArray['email']; // "john@example.com"

Get all documents in a folder

$user = $client->users->getAll(); // returns array of `\FolderDb\Document`

Check if document exists

$client->users->exists('username'); // returns boolean

Delete "Collection"

⚠️ This method deletes the "Collection" folder and it's contents. ⚠️

$user->delete(); // returns boolean

Dependencies

Issues

Please create an issue for any bugs/security risks/etc