imrgrgs/flatbase

此包最新版本(dev-master)没有提供许可信息。

独立的可查询平面文件数据库

dev-master 2019-08-30 12:45 UTC

This package is auto-updated.

Last update: 2024-09-29 05:02:41 UTC


README

Flatbase 是一个用 PHP 编写的平面文件数据库,旨在实现

  • 轻量级
  • 非常易于安装,配置最少/无配置
  • 简单直观的 API
  • 适用于小型数据集、低负载应用程序、测试/原型设计

示例用法

<?php

$storage = new Flatbase\Storage\Filesystem('/path/to/storage/dir');
$flatbase = new Flatbase\Flatbase($storage);

$flatbase->insert()->in('users')
    ->set(['name' => 'Mark', 'height' => "6'4"])
    ->execute();

$flatbase->read()->in('users')
    ->where('name', '=', 'Mark')
    ->first();
// (array) ['name' => 'Mark', 'height' => "6'4"]

安装

composer require imrgrgs/flatbase

使用

读取

从一个集合中获取所有记录

$flatase->read()->in('users')->get(); // Flatbase\Collection

只读取符合特定标准的记录

$flatbase->read()->in('users')->where('id', '==', '5')->get();

我们支持所有您期望的比较运算符

  • =
  • !=
  • ==
  • !==
  • <
  • >

您可以随意链式使用多个 where() 条件

$flatbase->read()
    ->in('users')
    ->where('age', '<', 40)
    ->where('age', '>', 20)
    ->where('country', '==', 'UK')
    ->get();

限制返回的记录

$flatbase->read()->in('users')->limit(10)->get(); // Get the first 10 records
$flatbase->read()->in('users')->skip(5)->limit(10)->get(); // Skip the first 5, then return the next 10
$flatbase->read()->in('users')->first(); // Get the first record

排序记录

$flatbase->read()->in('users')->sort('age')->get(); // Sort by age in ascending order
$flatbase->read()->in('users')->sortDesc('age')->get(); // Sort by age in descending order

只获取记录数

$flatbase->read()->in('users')->count();

删除

删除集合中的所有记录

$flatbase->delete()->in('users')->execute();

或只是部分记录

$flatbase->delete()->in('users')->where('id', '==', 5)->execute();

插入

$flatbase->insert()->in('users')->set([
    'name' => 'Mark',
    'country' => 'UK',
    'language' => 'English'
])->execute();

更新

更新集合中的所有记录

$flatbase->update()->in('users')->set(['country' => 'IE',])->execute();

或只是部分记录

$flatbase->update()
    ->in('users')
    ->set(['country' => 'IE',])
    ->where('name', '==', 'Mark')
    ->execute();

SQL 快速参考

命令行界面

Flatbase 包含一个名为 flatbase 的命令行界面,用于在应用程序外部快速操作数据。

php vendor/bin/flatbase read users

安装

要使用 CLI,您必须定义存储目录的路径。这可以通过在调用 flatbase 的目录(通常是应用程序根目录)中的 flatbase.json 文件来完成。

{
    "path": "some/path/to/storage"
}

或者,在执行命令时简单地包含 --path 选项。例如

php vendor/bin/flatbase read users --path="some/path/to/storage"

使用

# Get all records
php flatbase read users

# Get the first record in a collection
php flatbase read users --first

# Count the records in a collection
php flatbase read users --count

# Get users matching some where clauses
php flatbase read users --where "name,==,Mark" --where "age,<,30"

# Update some record(s)
php flatbase update users --where "age,<,18" --where "age,>,12" ageGroup=teenager

# Insert a new record
php flatbase insert users name=Mark age=25 country=UK

# Delete some record(s)
php flatbase delete users --where "name,==,Mark"

有关 CLI 的更多信息,请使用其中一个 help 命令

php flatbase help
php flatbase read --help
php flatbase update --help
php flatbase insert --help
php flatbase delete --help

为什么使用平面文件数据库?

平面文件数据库有哪些优点?

开始使用非常简单

只需将 imrgrgs/flatbase 添加到您的 composer.json 文件中,您就可以开始了。无需运行任何其他服务。

无模式

您不必担心定义模式、编写迁移脚本或任何其他无聊的事情。只需实例化 Flatbase 并开始提供数据。这在开发/原型设计新功能时尤其有用。

存储普通的 PHP 对象

数据存储在原生 PHP 序列化数组中,使用 PHPSerializer。这意味着您可以直接将普通的 PHP 对象存储到数据库中。

$flatbase->insert()->in('users')->set([
    'id' => 1,
    'name' => 'Mark',
    'added' => new DateTime()
])->execute();

$record = $flatbase->read()->in('users')->where('id', '==', 1)->first();
var_dump($record['added']); // DateTime

这也意味着您可以在任何时候轻松地反序列化(unserialize())您的数据,而无需通过 Flatbase,如果您愿意的话。

注意:虽然序列化是可能的,但在生产环境中使用时要格外小心。请记住,如果您序列化了对象,并且后来删除或移动了其实例的类,您将无法对其进行反序列化。存储标量数据始终是更安全的替代方案。

实际上并不慢

好吧,这个标题有点诱人。考虑到这是一个平面文件数据库,一些操作的速度非常快。在一个普通的 Ubuntu 桌面开发环境中,它可以在 1 秒内处理大约 50,000 个 "插入" 操作。不,这仍然远远不及 MySQL 或 Mongo 这样的数据库,但它比大多数人需要的要多得多。

读取数据确实要慢得多,尽管我们可以优化很多地方,但最终您必须接受这永远不会是一个高性能的持久解决方案。

许可

Flatbase 在 MIT 许可证下授权 - 请参阅 LICENSE 文件以获取详细信息