punarinta / doru-db
一个简单的基于文件的数据库,存储数据在 JSON 格式。
v0.1.1
2017-01-31 11:23 UTC
Requires
- php: >=7.0.0
This package is not auto-updated.
Last update: 2024-09-15 02:40:52 UTC
README
一个简单的基于文件的数据库。以 JSON 格式存储数据。支持基本的 CRUD 操作和并发。该名称来自印欧语系的单词 'doru',意为 '一棵树'。这个数据库是具有该名称的更大集合中的一个工具。
用法
连接到数据库
use \DoruDB\Database; // if no arguments are passed to constructor the database is stored in 'db' folder $db = new Database('path/to/db');
插入记录
$user = $db->create('user', ['name' => 'That Guy']); // this will insert a document with a unique id and nothing else $user = $db->create('user'); // this will also insert a record, as update() works as 'upsert' if ID is given $user = $db->update('user', ['id' => 1337, 'name' => 'This Guy']);
查找记录
// this is the fastest way to fetch a document, but you need to have an ID $user = $db->findById('user', 1337); // 'find' returns one record $users = $db->find('user', ['filter' => ['name' => 'This Guy']]); // 'findAll' returns all. NB: filter can use user-defined functions $users = $db->find('user', ['filter' => ['name' => function ($x) { return strpos($x, 'Guy') !== false; } ]]);
限制/偏移量
$users = $db->findAll('user', ['offset' => 1, 'limit' => 1]);
在使用没有索引的字段上的过滤器时,使用限制/偏移量比仅限制/偏移量或带有索引的过滤器慢,因为数据库必须首先读取所有文档,然后才能应用限制和偏移量。
排序
目前只支持按 ID 排序。使用 'invert' 选项进行降序排序。
$users = $db->findAll('user', ['invert' => true]);
删除记录
// deletes one document $db->delete('user', 1337); // deletes all documents $db->truncate('user');
使用索引
索引支持仍然是实验性的。请谨慎使用。索引用于加快过滤、限制和偏移量操作。
// add an index or update it automatically $db->rebuildIndex('user', 'lastLogin'); // update index manually with one or more documents $db->updateIndex('user', 'lastLogin', $someDocument); // remove index completely from the database $db->removeIndex('user', 'lastLogin');
待办事项
- 使 ID 不可变
- 唯一索引
- 提高索引读写性能
- 在文档更新时同步索引
- 使用非索引过滤器进行计数
许可协议
本项目遵循 MIT 许可协议。