almacil/php-database

一个简单的平面文件数据库,仅使用PHP和平面文件来持久化数据。在没有其他数据库可用或小型项目中是完美的解决方案。

1.0.1 2021-08-06 19:42 UTC

This package is not auto-updated.

Last update: 2024-09-29 09:18:54 UTC


README

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

🗃 Almacil PHP 数据库

这是一个简单的 平面文件 NoSQL 类型的数据库,用PHP实现,用于小型项目,没有第三方依赖,数据以纯JSON文件存储。


你想贡献力量吗?
Donate 1€

特性

  • 轻量级且安全
  • 易于开始
  • 无外部依赖
  • CRUD(创建、读取、更新、删除)操作
  • 支持多个数据库和表/集合

安装

可以使用Composer进行安装

composer requiere almacil/php-database

使用方法

摘要

// 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();

创建实例

创建 \Almacil\Database 的实例。我们可以使用斜杠来维护数据库和集合的顺序,形成一个层次结构。我们还可以决定目录对应于数据库,并在另一个目录中为另一个数据库创建另一个实例。

// 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';

查找

// ... after insert

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

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

插入

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

更新

// ... 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);

删除

当我们删除一个元素时,我们实际上并没有真正删除它,而是将 _removed_at 字段设置为 microtime() 的值。如果我们想永久删除元素,我们将第三个参数发送为 true。

// ... 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);

你想贡献力量吗?
Donate 1€

由开发者用 ❤️ 为开发者制作