hanneskod/yaysondb

以json数组形式存储数据的平面文件数据库

2.2.0 2019-07-15 16:44 UTC

This package is auto-updated.

Last update: 2024-08-23 04:24:43 UTC


README

Packagist Version Build Status Quality Score

纯PHP编写的平面文件数据库。

为什么?

部分是为了学习练习,部分是因为我需要一些命令行脚本的简单PHP数据库。

功能

安装

composer require hanneskod/yaysondb

使用方法

设置

Yaysondb 作为多个集合的处理程序。

use hanneskod\yaysondb\Yaysondb;
use hanneskod\yaysondb\Engine\FlysystemEngine;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;

$db = new Yaysondb([
    'table' => new FlysystemEngine(
        'data.json',
        new Filesystem(new Local('path-to-files'))
    )
]);

通过属性或 collection() 方法访问 collection

$db->table === $db->collection('table');

创建

$db->table->insert(['name' => 'foobar']);

事务

使用 commit()reset()inTransaction() 提交或回滚更改

$db->table->commit();

并发保护

当使用 flysystem 引擎时,Yaysondb 支持有限的并发保护。在每次读取时都会计算后端文件的哈希值,如果哈希值已更改,任何写入操作都将失败。

读取

使用 Operators 类创建搜索文档。

use hanneskod\yaysondb\Operators as y;

// Find all documents with an address in new york
$result = $db->table->find(
    y::doc([
        'address' => y::doc([
            'town' => y::regexp('/new york/i')
        ])
    ])
);

// The result set is filterable
foreach ($result->limit(2) as $id => $doc) {
    // iterate over the first 2 results
}

搜索文档

在创建搜索文档时可用以下操作符

更新

Collection::update() 接受两个参数。一个搜索文档和一个值数组。匹配搜索文档的文档将使用提供的值进行更新。

删除

Collection::delete() 仅接受一个搜索文档作为参数。匹配搜索文档的文档将被移除。