Download the PHP package flatbase/flatbase without Composer
On this page you can find all versions of the php package flatbase/flatbase. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package flatbase
Flatbase
Flatbase is a flat file database written in PHP which aims to be:
- Lightweight
- Very easy to install, with minimal/no configuration
- Simple intuitive API
- Suitable for small data sets, low-load applications, and testing/prototyping
Example Usage
Installation
composer require flatbase/flatbase
Usage
Reading
Fetch all the records from a collection:
Reading only data matching a certain criteria:
We support all the comparison operators you'd expect:
=
!=
==
!==
<
>
You can chain as many where()
conditions as you like:
Limit the returned records:
Sort the records:
Just get a count of records:
Deleting
Delete all records in a collection:
Or just some records:
Inserting
Updating
Update all records in a collection:
Or just some records:
SQL Cheat Sheet
SQL Statement | Flatbase Query |
---|---|
SELECT * FROM posts |
$flatbase->read()->in('posts')->get(); |
SELECT * FROM posts LIMIT 0,1 |
$flatbase->read()->in('posts')->first(); |
SELECT * FROM posts WHERE id = 5 |
$flatbase->read()->in('posts')->where('id', '==', 5)->get(); |
SELECT * FROM posts WHERE views > 500 |
$flatbase->read()->in('posts')->where('views', '>', 500)->get(); |
SELECT * FROM posts WHERE views > 50 AND id = 5 |
$flatbase->read()->in('posts')->where('views', '>', 50)->where('id', '==', '5')->get(); |
UPDATE posts SET title = 'Foo' WHERE content = 'bar' |
$flatbase->update()->in('posts')->set(['title' => 'var'])->where('content', '==', 'bar')->execute(); |
DELETE FROM posts WHERE id = 2 |
$flatbase->delete()->in('posts')->where('id', '==', 2)->execute(); |
INSERT INTO posts SET title='Foo', content='Bar' |
$flatbase->insert()->in('posts')->set(['title' => 'Foo', 'content' => 'Bar')->execute(); |
Command Line Interface
Flatbase includes a command line interface flatbase
for quick manipulation of data outside of your application.
Installation
To use the CLI, you must define the path to your storage directory. This can either be done with a flatbase.json
file in the directory you call flatbase from (usually your application root):
Alternatively, simply include the --path
option when issuing commands. Eg:
Demo
Usage
For more info on the CLI, use one of the help
commands
Why use a flat file database?
What are some of the advantages of a flat file database?
It's really easy to get started
Just add flatbase/flatbase
to your composer.json
and you're rolling. No need for any other services running.
It's schema-less
You don't have to worry about defining a schema, writing migration scripts, or any of that other boring stuff. Just instantiate Flatbase
and start giving it data. This is particularly useful when developing/prototyping new features.
Store plain old PHP objects
Data is stored in a native PHP serialized array using PHPSerializer. This means that you can store plain old PHP objects straight to the database:
It also means that you can, at any point, easily unserialize() your data without having to go through Flatbase if you wish.
Note: Although serializing is possible, be careful when using this in production. Remember that if you serialize an object, and then, later on, delete or move the class it was an instance of, you won't be able to un-serialize it. Storing scalar data is always a safer alternative.
It isn't actually that slow
Ok, that's a bit of a baiting title. Some operations are remarkably quick considering this is a flat file database. On a mediocre Ubuntu desktop development environment, it can process around 50,000 "inserts" in 1 second. No, that is still nowhere near a database like MySQL or Mongo, but it's a hell of a lot more than most people need.
Reading data out is certainly a lot slower, and although there are lots of places we can optimise, ultimately you'd need to accept this is never going to be a high-performance solution for persistence.
Author
Adam Nicholson - [email protected]
Contributing
Contributions are welcome, and they can be made via GitHub issues or pull requests.
License
Flatbase is licensed under the MIT License - see the LICENSE.txt
file for details