A simplistic PHP flat file database designed to get your application up and running fast. Please note this package is currently in development and is not yet at a release.
Install via composer:
composer require robert-grubb/filerdb-php
NOTE: Please make sure your database directory has correct permissions for READ and WRITE.
use FilerDB\Instance;
// Instantiate Database
$filerdb = new Instance([ 'root' => __DIR__ . '/database/' ]);
[
/**
* This is the root path for FilerDB.
*/
'root' => false,
/**
* If the root path does not exist, try
* and create it.
*/
'createRootIfNotExist' => false,
/**
* If the database does not exist, try
* and create it.
*/
'createDatabaseIfNotExist' => false,
/**
* If the collection does not exist, attempt
* to create it.
*/
'createCollectionIfNotExist' => false,
/**
* If the insert and update logic handles
* the createdAt and updatedAt timestamps
* automatically
*/
'includeTimestamps' => false
]
$filerdb->databases->create('dev');
$filerdb->databases->exists('dev'); // Returns true or false
$filerdb->databases->list(); // Returns array
$filerdb->databases->delete('dev');
Selecting a default database allows you to not have to specify the database on every call. Please refer to the below code on how to do that.
// Specify it in the configuration:
$filerdb = new Instance([
'path' => __DIR__ . '/database/',
'database' => 'database_name'
]);
or
$filerdb->selectDatabase('database_name');
With the above, you can now do the following:
$filerdb->collection('users')->all(); // Notice no ->database()
$filerdb->database('dev')->collections(); // Returns array
$filerdb->database('dev')->collectionExists('dev'); // Returns true of false
$filerdb->database('dev')->createCollection('users');
$filerdb->database('dev')->deleteCollection('users');
$filerdb->database('dev')->collection('users')->empty();
$filerdb->database('dev')->collection('users')->insert([
'username' => 'test',
'email' => 'test@test.com'
]);
// By a specific document
$filerdb
->database('dev')
->collection('users')
->id('ad23tasdg')
->update([
'username' => 'test2'
]);
// Where all usernames equal test
$filerdb
->database('dev')
->collection('users')
->filter(['username' => 'test'])
->update([
'username' => 'test2'
]);
// Specific document
$filerdb
->database('dev')
->collection('users')
->id('asdfwegd')
->delete();
// With filters
$filerdb
->database('dev')
->collection('users')
->filter(['username' => 'test'])
->delete();
$filerdb->database('dev')->collection('users')->all()
$filerdb
->database('dev')
->collection('users')
->id('asdf23g')
->get();
// Get users with username of test and greater than age of 10.
$filerdb
->database('dev')
->collection('users')
->filter(['username' => 'test'])
->filter([ ['age', '>', '10'] ])
->get();
// Only returns the username field
$filerdb
->database('dev')
->collection('users')
->get(['username']);
// Get users with username of test and greater than age of 10.
$filerdb
->database('dev')
->collection('users')
->orderBy('username', 'asc')
->get();
// Pull upto 10 documents
$filerdb
->database('dev')
->collection('users')
->limit(10)
->get();
// Pull upto 10 documents, but start at the
// 9th array key.
$filerdb
->database('dev')
->collection('users')
->limit(10, 9)
->get();
You can now programmatically backup your database. You can do so by using the following code:
$filerdb->backup->create('file_name_here.zip');
This was provided so you can manually backup your database via your own command line script, or automatically via a cron job, or something similar.