vollborn / local-db
v1.0.0
2021-12-30 17:09 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
README
LocalDB is a small package to read and write JSON files in a Laravel Eloquent like style.
It was originally developed for small web servers that cannot connect to classic databases.
Installation
This package ist available via composer.
composer require vollborn/local-db
Configuration
Set the base path
In the base path all json files are stored. The default value is ../storage/local-db.
You can change it if you need to:
LocalDB::setBasePath(<your path>);
Register a table
Every table or json file needs to be registered beforehand.
LocalDB::table('test', static function (Table $table) { $table->int('int')->autoincrements(); $table->array('array')->nullable(); $table->boolean('boolean'); $table->float('float'); $table->string('string'); });
As you can see, there are multiple data types available:
In addition, integers can have autoincrements.
All data types can also be nullable.
Usage
Create data
LocalDB::query('test')->create([ 'float' => 1.00, 'string' => null, 'boolean' => false, 'array' => [] ]);
Get data
You can either get all data...
LocalDB::query('test')->get();
or just the first entry...
LocalDB::query('test')->first();
The data can also be filtered.
LocalDB::query('test')->where('float', '=', 1.00)->get();
Available operators:
- =
- !=
- <
- >
- <=
- >=
Furthermore, the data can be ordered.
// ascending LocalDB::query('test')->orderBy('float')->get(); // descending LocalDB::query('test')->orderBy('float', true)->get();
There are some numeric operations available too.
// 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');
Update data
LocalDB::query('test') ->where('boolean', '=', false) ->update([ 'boolean' => true ]);
Delete data
LocalDB::query('test') ->where('boolean', '=', false) ->delete();