almacil/php-database

A simple flat file database designed to persist data using just PHP and flat files. Perfect solution when no other database is available or for small projects.

1.0.1 2021-08-06 19:42 UTC

This package is not auto-updated.

Last update: 2025-06-22 13:12:56 UTC


README

GitHub last commit GitHub tag (latest by date) Packagist PHP Version Support GitHub

🗃 Almacil PHP Database

This is a simple flat file NoSQL like database implemented in PHP for small projects without any third-party dependencies that store data in plain JSON files.


Do you want to contribute?
Donate 1€

Features

  • Lightweight and Secure
  • Easy to get started
  • No external dependencies
  • CRUD (create, read, update, delete) operations
  • Supports multiple databases and tables/collections

Installation

Installation is possible using Composer

composer requiere almacil/php-database

Usage

Summary

// Create instance
$db = new \Almacil\Database($database);

// Basic
$db->find($collection, /* function to find */);
$db->insert($collection, $item);
$db->update($collection, /* function to find */, $update);
$db->remove($collection, /* function to find */, $permanent);

// More
$db->count($collection, /* function to find */);
$db->findOne($collection, /* function to find */);
$db->upsert($collection, /* function to find */, $update);
$db->drop($collection);
$db->newid();

Create instance

Create an instance of \Almacil\Database. Optionally, we can maintain the order of the databases and collections with a hierarchy using slash. We can also decide that the directory corresponds to a database and create another instance for another database in another directory.

// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';

// Directory containing the json files of databases
$database = __DIR__ . '/data';

// Create de instance
$db = new \Almacil\Database($database);

// "database/collection" or "collection" or "collection/subcollection"
$collection = 'mycollection/mysubcollection';

Find

// ... after insert

$find = new stdClass();
$find->_id = $newItem->_id;

$items = $db->update($collection, function($item) use ($find) {
    return $find->_id === $item->_id;
});

Insert

// ... after create instance $db
$item = new stdClass();
$item->name = 'Rubén';
$newItem = $db->insert($collection, $item);

Update

// ... after insert

$find = new stdClass();
$find->_id = $newItem->_id;

$update = new stdClass();
$update->name = 'Rubén Pérez';

$numberItemsUpdated = $db->update($collection, function($item) use ($find) {
    return $find->_id === $item->_id;
}, $update);

Remove

When we delete an element, we don't really delete it but we set the _removed_at field with the value of microtime(). If we want to delete the element permanently we will send true in the third argument.

// ... after insert

$find = new stdClass();
$find->_id = $newItem->_id;

$permanent = true;

$numberItemsRemoved = $db->remove($collection, function($item) use ($find) {
    return $find->_id === $item->_id;
}, $permanent);

Do you want to contribute?
Donate 1€

Made with ❤️ by developer for developers