vollborn/local-db

v1.0.0 2021-12-30 17:09 UTC

This package is auto-updated.

Last update: 2024-09-21 21:16:54 UTC


README

LocalDB 是一个小型包,用于以 Laravel Eloquent 类似的方式读取和写入 JSON 文件。
它最初是为无法连接到传统数据库的小型网络服务器开发的。


安装

此包可通过 composer 获取。

composer require vollborn/local-db

配置

设置基本路径

在基本路径中存储所有 JSON 文件。默认值为 ../storage/local-db

如果需要,您可以更改它

LocalDB::setBasePath(<your path>);

注册表

每个表或 JSON 文件都需要预先注册。

LocalDB::table('test', static function (Table $table) {
    $table->int('int')->autoincrements();
    $table->array('array')->nullable();
    $table->boolean('boolean');
    $table->float('float');
    $table->string('string');
});

如你所见,有多个数据类型可供选择

此外,整数可以有 自动增长
所有数据类型也可以是 可空的


用法

创建数据

LocalDB::query('test')->create([
    'float' => 1.00,
    'string' => null,
    'boolean' => false,
    'array' => []
]);

获取数据

您可以获取所有数据...

LocalDB::query('test')->get();

或者只获取第一个条目...

LocalDB::query('test')->first();

数据也可以被过滤。

LocalDB::query('test')->where('float', '=', 1.00)->get();

可用操作符

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

此外,数据可以被排序。

// ascending
LocalDB::query('test')->orderBy('float')->get();

// descending
LocalDB::query('test')->orderBy('float', true)->get();

还有一些数值运算可用。

// minimal value
LocalDB::query('test')->min('float');

// maximal value
LocalDB::query('test')->max('float');

// average value
LocalDB::query('test')->avg('float');

// summed value
LocalDB::query('test')->sum('float');

更新数据

LocalDB::query('test')
    ->where('boolean', '=', false)
    ->update([
        'boolean' => true
    ]);

删除数据

LocalDB::query('test')
    ->where('boolean', '=', false)
    ->delete();