hanneskod / yaysondb
Flat file db storing data as json arrays
Installs: 1 824
Dependents: 2
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.0
- league/flysystem: ^1.0
Requires (Dev)
- league/flysystem-memory: ^1.0
README
Flat file db in pure php.
Why?
Partly as a learning exercise, partly since I needed a simple and PHP only DB for some cli scripts.
Features
- Powerfull searches using search documents
- Supports limits, ordering and custom filtering expressions
- Multiple filesystem support through Flysystem
- Simple transaction support
- Validates that source has not been altered before writing
- Fast logging with the dedicated LogEngine
Installation
composer require hanneskod/yaysondb
Usage
Setup
Yaysondb
works as a handler for multiple collections.
use hanneskod\yaysondb\Yaysondb; use hanneskod\yaysondb\Engine\FlysystemEngine; use League\Flysystem\Filesystem; use League\Flysystem\Adapter\Local; $db = new Yaysondb([ 'table' => new FlysystemEngine( 'data.json', new Filesystem(new Local('path-to-files')) ) ]);
Access collection
through property or
collection()
$db->table === $db->collection('table');
Create
$db->table->insert(['name' => 'foobar']);
Transactions
Commit or rollback changes using commit()
, reset()
and inTransaction()
$db->table->commit();
Concurrency protection
Yaysondb supports limited concurrency protection when using the flysystem engine. A hash of the backend file is calculated at each read and any write action will fail if the hash has changed.
Read
Create search documents using the Operators
class.
use hanneskod\yaysondb\Operators as y; // Find all documents with an address in new york $result = $db->table->find( y::doc([ 'address' => y::doc([ 'town' => y::regexp('/new york/i') ]) ]) ); // The result set is filterable foreach ($result->limit(2) as $id => $doc) { // iterate over the first 2 results }
The search document
The following operators are available when creating search documents:
Update
Collection::update()
takes two arguments. A search document and an array
of values. Documents matching the search document are updated with the supplied
values.
Delete
Collection::delete()
takes a search document as sole argument. Documents
matching the search document are removed.