devfake / novus
PHP 的 JSON 文件数据库
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2020-02-17 04:36:39 UTC
README
Novus 是一个 PHP 的 JSON 文件数据库。使用此软件包进行快速原型设计和测试您的应用程序,无需配置 mysql(或其他)数据库。
语法与更典型的 SQL 类似,而不是 ORM。
警告:该软件包不完整,缺少一些重要功能。
- 入门
- 快速概述
- 第一步
- 选项
- 参数值
- 创建表
- 添加字段
- 重命名字段
- 删除字段
- 插入
- 选择
- 排序
- 限制
- WHERE 条件
- 更新
- 删除和移除
- 最后一个主键
- 下一个主键
- 重命名主键
- 最后和第一个
- 查找和 FindOrFail
入门
要求
- PHP 5.4+
- Composer
安装
安装 Novus 最简单的方法是使用 Composer。将其添加到您的 composer.json
文件,并运行 $ composer update
{ "require": { "devfake/novus": "dev-master" } }
如果您不想使用 Composer,功能版本将包含引导文件。
快速概述
让我们快速了解一下语法
$novus = new \Devfake\Novus\Database(); // Create a new 'users' table. $novus->table('users')->create('username, password, email'); // Add more fields. $novus->table('users')->addFields('activated'); // Insert data. $novus->table('users')->insert('username = Arya, email = a.stark@winterfell.com, password = n33dl3, activated = 1'); // Select all data. $data = $novus->table('users')->select(); // Select only username and email. $data = $novus->table('users')->select('username, email'); // Select all data from 'users' where id = 1. $data = $novus->table('users')->where('id = 1')->select(); // Update username. $novus->table('users')->update('username = Jon'); // Delete all data from 'users'. $novus->table('users')->delete(true); // Delete all data from 'users' with soft delete. $novus->table('users')->delete(); // Remove complete 'users' file. $novus->table('users')->remove(); // Limit and order data. $novus->table('users')->where('id > 4')->limit(5)->orderBy('id desc')->select()
第一步
一旦创建了对象,您就可以正常使用它们。
但是,有一条小规则:您必须始终在开始时指定您正在处理的表,使用 table()
$novus = new \Devfake\Novus\Database(); $novus->table('myTable')->myMethods();
但是,您也可以直接指定对象的一个表
$myTable = new \Devfake\Novus\Database('myTable'); $myTable->myMethods(); $users = new \Devfake\Novus\Database('users'); $users->orderBy('username, email')->select('username, email, date');
如果您只处理几个表,或者想使您的代码更易读,则推荐使用这种方式。当然,您要写的也少。
选项
您可以在实例化类时传递一些选项。
// Pass a single string to specify directly a table. $novus = new \Devfake\Novus\Database('myTable'); // Or pass a array with conditions. $novus = new \Devfake\Novus\Database([ 'table' => 'myTable', 'path' => 'myPathForDatabaseFiles', 'primaryKey' => 'Number' ]);
数据库文件的 path
相对于您的根文件夹(或您的 composers vendor
文件夹所在的目录)。
默认文件夹是 database
。有一个 saves
文件夹来保存您的软删除。
primaryKey
仅用于 create()
方法。
参数值
// First, pass a string as argument and separate the keywords with a comma. $novus->table('users')->create('username, email, password'); // Or, pass a array as argument. $novus->table('tableName')->create(['username', 'email', 'password']);
它们之间有什么区别?第一种方法清晰且快速易打。
如果您的键中有逗号,请使用第二种方法。但是,请尽量确保您的字段名中没有逗号(或其他特殊字符)。第二种方法将用于插入或更新数据。
创建表
让我们创建一个用户表,并在整个文档中与之一起工作。对于文档,我们将使用 table()
来使其更详细。
// Create a new file without fields. $novus->table('users')->create(); // Create a new file with fields. $novus->table('users')->create('field1, field2'); // Or with the array spelling. $novus->table('users')->create(['field1', 'field2']);
就这样!您只需要运行一次 create()
方法。
现在有一个新的 database/users.json
文件。让我们打开它
{"table":"users","id":1,"fields":[["id"]],"data":[]} // Format it a bit for the documentation { "table": "users", "id": 1, "fields": [ ["id"] ], "data": [] }
那么,我们表格中有什么?首先,我们有"table"
,这是我们的表名,在这个例子中是"users"
。然后是我们的主键,"id"
。它们会自动增加。
接下来是我们的"fields"
。主键会默认添加。
最后,我们有我们的"data"
。由于我们没有输入数据,这个字段是空的。正如你所见,"fields"
和"data"
都是包含其他数组的数组。
添加字段
$novus->table('users')->addFields('username, email'); $novus->table('users')->addFields(['username', 'email']);
重命名字段
$novus->table('users')->renameFields('username = users, email = mail'); $novus->table('users')->renameFields(['username' => 'users', 'email' => 'mail']);
删除字段
removeFields()
方法还会删除相关的数据。
$novus->table('users')->removeFields('username, email'); $novus->table('users')->removeFields(['username', 'email']);
插入
$novus->table('users')->insert('username = devfake, email = devfakeplus@googlemail.com'); $novus->table('users')->insert(['username' => 'devfake', 'email' => 'devfakeplus@googlemail.com']);
选择
// Select all data from 'users'. $data = $novus->table('users')->select(); $data = $novus->table('users')->select('*'); // Select only username and email. $data = $novus->table('users')->select('username, email'); $data = $novus->table('users')->select(['username', 'email']); // Select all data from where id = 1. $data = $novus->table('users')->where('id = 1')->select(); // Iterate over $data foreach($data as $content) { echo $content['username']; }
排序
使用orderBy()
方法对输出进行排序。
// Order by id ASC and username DESC. $order = $novus->table('users')->orderBy('id asc, username desc')->select(); // Or with array spelling. $order = $novus->table('users')->orderBy(['id' => 'asc', 'username' => 'desc'])->select(); // ASC is default passed. $order = $novus->table('users')->orderBy('id, username desc')->select(); $order = $novus->table('users')->orderBy(['id', 'username' => 'desc')->select(); // ASC and DESC are case insensitive. $order = $novus->table('users')->orderBy('id DESC, username ASC')->select();
限制
1 => first_user, 2 => second_user, 3 => third_user, 4 => fourth_user, 5 => fifth_user, 6 => sixth_user, 7 => seventh_user
这些是用户表的伪数据。通过它们,我们演示了limit()
方法。
// Select the first three data. $limit = $novus->table('users')->limit(3)->select(); 1 => first_user, 2 => second_user, 3 => third_user // Select the next four data after the first two. $limit = $novus->table('users')->limit(2, 4)->select(); 3 => third_user, 4 => fourth_user, 5 => fifth_user, 6 => sixth_user // Select the last three data in reverse. $limit = $novus->table('users')->limit(3, true)->select(); 5 => fifth_user, 6 => sixth_user, 7 => seventh_user // Select the last four data in reverse before the last two. $limit = $novus->table('users')->limit(2, 4, true)->select(); 2 => second_user, 3 => third_user, 4 => fourth_user, 5 => fifth_user
反向模式返回升序数据。用例例如聊天。
如果你不希望这样,可以按顺序排序,例如按id。
// Select the last four data without reverse. $limit = $novus->table('users')->limit(4, true)->orderBy('id DESC')->select(); 7 => seventh_user, 6 => sixth_user, 5 => fifth_user, 4 => fourth_user
orderBy()
方法是在limit()
之前还是之后写都没有关系。
WHERE条件
警告:此方法目前非常有限。它仅适用于select()
并且只执行一个查询。但我很快会修复它。
$data = $novus->table('users')->where('id = 1')->select(); $data = $novus->table('users')->where('username = Arya')->select(); $data = $novus->table('users')->where('id > 10')->select();
使用<=, >=, !=, =, >
和<
。
更新
警告:update()
目前将更新所有数据。我将很快使用where()
修复它。
$novus->table('users')->update('username = newUsername, email = newEmail'); $novus->table('users')->update(['username' => 'newUsername', 'email' => 'newEmail']);
删除和删除
如果你使用delete()
或remove()
,novus将在saves
文件夹中创建一个备份文件。这个文件夹在开始时创建,位于数据库文件夹内。
将true
作为参数传递以避免软删除。
删除
使用delete()
你可以删除数据集。
警告:此方法目前非常有限,并从表中删除所有数据。我将很快使用where()
修复它。
$novus->table('users')->delete(); $novus->table('users')->delete(true);
删除
使用remove()
你可以删除完整的表文件。
$novus->table('users')->remove(); $novus->table('users')->remove(true);
最后主键
返回最后一条数据的主键。如果数据为空,则方法返回null
。
$key = $novus->table('users')->lastPrimaryKey();
下一个主键
返回下一个插入数据的主键。
$key = $novus->table('users')->nextPrimaryKey();
重命名主键
更改表的主键。这不会影响由选项定义的主键。
$novus->table('users')->renamePrimaryKey('number');
最后和第一
$first = $novus->table('users')->first(); echo $first['username']; $last = $novus->table('users')->last(); echo $last['username'];
查找和查找失败
通过主键查找数据。如果没有找到数据,find()
将返回一个空数组,而 findOrFail()
将返回一个异常。
$find = $novus->table('users')->find(1); echo $find['username']; $find = $novus->table('users')->findOrFail(2); echo $find['username'];
待办事项
- 完成
where()
条件。 - 字段类型。
- 编写测试。
- 自带的自动加载器。
- 对象访问。