almacil/php-database
最新稳定版本:1.0.1
Composer 安装命令:
composer require 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.
README 文档
README
🗃 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.
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);
Made with ❤️ by developer for developers
统计信息
- 总下载量: 820
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-06