jajo/jsondb
Composer 安装命令:
composer require jajo/jsondb
包简介
A PHP Class that reads JSON file as a database. Use for sample DBs
README 文档
README
A PHP Class that reads JSON file as a database. Use for sample DBs.
Usage
Install package composer require jajo/jsondb
Initialize
<?php use Jajo\JSONDB; $json_db = new JSONDB( __DIR__ ); // Or passing the directory of your json files with no trailing slash, default is the current directory. E.g. new JSONDB( '/var/www/html/json_files' )
Inserting
Insert into your new JSON file. Using users.json as example here
NB: Columns inserted first will be the only allowed column on other inserts
<?php $json_db->insert( 'users.json', [ 'name' => 'Thomas', 'state' => 'Nigeria', 'age' => 22 ] );
Get
Get back data, just like MySQL in PHP
All columns:
<?php $users = $json_db->select( '*' ) ->from( 'users.json' ) ->get(); print_r( $users );
Custom Columns:
<?php $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->get(); print_r( $users );
Where Statement:
This WHERE works as AND Operator at the moment or OR
<?php $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->get(); print_r( $users ); // Defaults to Thomas OR Nigeria $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ] ) ->get(); print_r( $users ); // Now is THOMAS AND Nigeria $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ], 'AND' ) ->get(); print_r( $users );
Where Statement with regex:
By passingJSONDB::regex to where statement, you can apply regex searching. It can be used for implementing LIKE or REGEXP_LIKE clause in SQL.
$users = $json_db->select( 'name, state' ) ->from( "users" ) ->where( array( "state" => JSONDB::regex( "/ria/" )), JSONDB::AND ) ->get(); print_r( $users ); // Outputs are rows which contains "ria" string in "state" column.
Order By:
Thanks to Tarun Shanker for this feature. By passing the order_by() method, the result is sorted with 2 arguments of the column name and sort method - JSONDB::ASC and JSONDB::DESC
<?php $users = $json_db->select( 'name, state' ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->order_by( 'age', JSONDB::ASC ) ->get(); print_r( $users );
Updating Row
You can also update same JSON file with these methods
<?php $json_db->update( [ 'name' => 'Oji', 'age' => 10 ] ) ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->trigger();
Without the where() method, it will update all rows
Deleting Row
<?php $json_db->delete() ->from( 'users.json' ) ->where( [ 'name' => 'Thomas' ] ) ->trigger();
Without the where() method, it will deletes all rows
Exporting to MySQL
You can export the JSON back to SQL file by using this method and providing an output
<?php $json_db->to_mysql( 'users.json', 'users.sql' );
Disable CREATE TABLE
<?php $json_db->to_mysql( 'users.json', 'users.sql', false );
Exporting to XML
Tarun Shanker also provided a feature to export data to an XML file
<?php if( $json_db->to_xml( 'users.json', 'users.xml' ) ) { echo 'Saved!'; }
jajo/jsondb 适用场景与选型建议
jajo/jsondb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 179.74k 次下载、GitHub Stars 达 221, 最近一次更新时间为 2018 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jajo/jsondb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jajo/jsondb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 179.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 223
- 点击次数: 15
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-11