ronolo/json-store

一个轻量级的 JSON 文档存储库,可以像 NoSQL 数据库一样进行查询。

1.3.2 2020-10-09 07:51 UTC

This package is auto-updated.

Last update: 2024-09-29 05:48:58 UTC


README

使用任何类型的文件系统存储文档的文档存储库。它使用 https://flysystem.thephpleague.com/ 来抽象存储空间。

由于定义上,一个 存储库 就像数据库中的一个表。你可以将所有内容放入一个存储库,或者为不同的 JSON 对象创建多个存储库。

它使用类似 NoSQL 的查询系统对文档进行操作,并旨在使用非常低的内存占用(即不会将所有文档加载到内存中处理)。

注意:存在一个 ronolo/json-database 包,它使用 json-store 并通过文档关系(外键)和查询结果缓存对其进行扩展。

用法

首先创建一个 Config 对象。

然后指定要使用的适配器,将实际存储 JSON 文件到磁盘/云/内存/zip。请参阅 https://github.com/thephpleague/flysystem 以找到适合您需求的适配器。您必须使用正确的参数初始化适配器。

// First create the config object
$config = new Store\Config();
// Set the the adapter
$config->setAdapter(new Local('some/path/persons'));
// Secondly create the JsonDB
$store = new Store($config);

存储库现在已就绪。我们现在可以存储、读取、删除和更新文档。作为一个非常基本的用法,我们可以通过 ID 读取每个文档。

注意:更新始终是整个对象的更新。无法通过存储命令更新单个字段。

作为速度上的优势,存储库将所有文档 ID 保存在一个索引文件中,该文件将在存储库构造时加载。另一个速度上的优势是使用在 https://flysystem.thephpleague.com/v1/docs/advanced/caching/ 页面上找到的缓存和速度提升适配器。

$document = file_get_contents('file/with/json/object.json');
// store a document
$id = $store->put($document);
// read a document
$document = $store->read($id);
// update document
$document->foobar = "Heinz";
$store->put($document);
// remove document
$store->remove($id); 

也可以从存储库中以类似 CouchDB 的方式查询文档。

$query = new Store\Query($store);
$result = $query->find([
    "name" => "Bernd"
]);

// An iterator can be used to fetch one by one all documents

foreach ($result as $id => $document) {
    ; // do something with the document
}

已实现以下条件

[
    '$eq' => 'isEqual',
    '$neq' => 'isNotEqual',
    '$gt' => 'isGreaterThan',
    '$gte' => 'isGreaterThanOrEqual',
    '$lt' => 'isLessThan',
    '$lte' => 'isLessThanOrEqual',
    '$in'    => 'isIn',
    '$nin' => 'isNotIn',
    '$null' => 'isNull',
    '$n' => 'isNull',
    '$notnull' => 'isNotNull',
    '$nn' => 'isNotNull',
    '$contains' => 'contains',
    '$c' => 'contains',
    '$ne' => 'isNotEmpty',
    '$e' => 'isEmpty',
    '$regex' => 'isRegExMatch',
]

示例可以在 tests/src/query 的子目录中找到。

以下是一些示例

// SELECT * FROM store WHERE age = 20 OR age = 30 OR age = 40;
$query = new Store\Query($store);
$result = $query
    ->find([
        ["age" => 20],
        ["age" => 30],
        ["age" => 40]
    ])
    ->execute()
;

// SELECT index, guid FROM store ORDER BY index ASC LIMIT 60;
$query = new Store\Query($store);
$result = $query
    ->find([])
    ->fields(["index", "guid"])
    ->sort("index", "asc")
    ->limit(60)
    ->execute()
;

// SELECT * FROM store WHERE age = 20 AND phone = '12345' OR age = 40;
$query = new Store\Query($store);
$result = $query
    ->find([
        '$or' => [
            [
                "age" => [
                    '$eq' => 20,
                ],
                "phone" => [
                    '$eq' => "12345",
                ]
            ],
            [
                "age" => [
                    '$eq' => 40
                ]
            ]
        ]
    ])
    ->execute()
;

目标

  • 不需要像 SqlLite、Mysql、MongoDB、CouchDB 等实际的数据库)
  • PHP 7.2+
  • 文档存储库即 NoSQL
  • JSON 作为存储格式
  • (非常)低内存占用,即使对于大量结果
  • NoSQL 类型的查询语法(CouchDB 风格)
  • 通过 https://flysystem.thephpleague.com/ 抽象数据位置

限制

  • 底层 flysystem 适配器具有的任何限制
  • 如果创建唯一 ID 失败,将抛出异常