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.

Fund package maintenance!
rubenperezlopez

Installs: 761

Dependents: 1

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/almacil/php-database

1.0.1 2021-08-06 19:42 UTC

This package is not auto-updated.

Last update: 2025-10-26 14:50:06 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