Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vollborn/local-db

LocalDB is a small package to read and write JSON files in a Laravel Eloquent like style.
https://github.com/vollborn/local-db

composer database eloquent json laravel package packagist php php-json

Last synced: about 4 hours ago
JSON representation

LocalDB is a small package to read and write JSON files in a Laravel Eloquent like style.

Awesome Lists containing this project

README

        

# LocalDB

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](https://getcomposer.org/).

```shell
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:
```php
LocalDB::setBasePath();
```

### Register a table

Every table or json file needs to be registered beforehand.

```php
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:

| Name | Function |
|---------|----------|
| Integer | int |
| Array | array |
| Boolean | boolean |
| Float | float |
| String | string |

In addition, integers can have *autoincrements*.

All data types can also be *nullable*.


## Usage

### Create data

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

### Get data

You can either get all data...
```php
LocalDB::query('test')->get();
```

or just the first entry...
```php
LocalDB::query('test')->first();
```

The data can also be filtered.
```php
LocalDB::query('test')->where('float', '=', 1.00)->get();
```

Available operators:
- =
- !=
- <
- \>
- <=
- \>=

Furthermore, the data can be ordered.
```php
// ascending
LocalDB::query('test')->orderBy('float')->get();

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

There are some numeric operations available too.
```php
// 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

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

### Delete data

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