commandstring/jsondb
最新稳定版本:v1.1.1
Composer 安装命令:
composer require commandstring/jsondb
包简介
An easier way to use JSON for a database
README 文档
README
A fully customizable JSON Database system structured similarly to MySQL
Creating a database
<?php use CommandString\JsonDb\Structure\Database; use CommandString\JsonDb\Structure\Table; class YourDatabase extends Database { protected static array $tableClasses = []; }
Create a class that extends CommandString\JsonDb\Structure\Database and add the property shown above. We'll come back to it later
Creating a table
<?php use CommandString\JsonDb\Structure\Table; class YourTable extends Table { protected static string $name = "yourtable"; protected static string $fileLocation = __DIR__."/yourtable.json"; protected static array $columns = []; }
Create a class that extends CommandString\JsonDb\Structure\Table like the one above. The first property is how the table will be identified, the second is where the table's json will be stored/retrieved and I'll come back to the columns property later as well. However, for the tableClasses property in your database class, you will want to add your table class to it like so...
// ... protected static array $tableClasses = [YourDatabase::class]; // ...
and append any future tables you create to this property
Creating a column
use CommandString\JsonDb\Structure\Column; use CommandString\JsonDb\Structure\DataTypes; class YourColumn extends Column { protected static DataTypes $type = DataTypes::STRING; protected static string $name = "yourcolumn"; protected static array $enumValues = []; protected static string|int|float|null $default; protected static bool $nullable = false; protected static bool $unique = false; }
Create a class that extends CommandString\JsonDb\Structure\Column then add the properties shown above. The available DataTypes are string, int, float, and enum. If the type is enum then make sure to add the values this column can be inside the enumValues array.
Special Notes
- I would recommend adding magic methods and public constants to your classes to take advantage of intellesense while coding, checkout the TestDb for an example
- Column and Table names will always be lowercased, so I would recommend having them lowercased by default to prevent any confusion.
Creating Rows
$db = new YourDatabase; // you can also pass JSON encoder flags into the constructor $row = $db->yourtable->newRow(); $row->setColumn($db->yourtable::COLUMN_NAME, "<value>") // you can pass a string for the column name but I recommend having constants has mentioned earlier $row = $row->store(); // returns that same row but this is associated to specific point in the JSON file
Fetching rows
/** * @var Row[] */ $results = $db->yourtable->newQuery()->whereAnd($db->yourtable::COLUMN_NAME, Operators::EQUAL_TO, "<value>")->execute();
Updating rows
$row = $results[0]; $row->setColumn($db->yourtable::COLUMN_NAME, "<new value>"); $row->store(); // updated the row
commandstring/jsondb 适用场景与选型建议
commandstring/jsondb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 commandstring/jsondb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 commandstring/jsondb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-05