Download the PHP package aalfiann/filebase without Composer
On this page you can find all versions of the php package aalfiann/filebase. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aalfiann/filebase
More information about aalfiann/filebase
Files in aalfiann/filebase
Package filebase
Short Description A Simple but Powerful Flat File Database Storage.
License MIT
Homepage https://github.com/aalfiann/filebase
Informations about the package filebase
Filebase
This is forked from filebase/Filebase and based from core version 1.0.20
, I don't contribute to original project, because I have different plan which is need to refactor and may broke the original project.
What is the plan for this project ?
- Refactoring code to achieve high performance with low memory footprint
- Focus on optimizing data into big scale
- Only update for security patch or critical bugs
- New feature will follow the original project
Version number is changed
I start to refactor filebase/Filebase from core version 1.0.20
, then I make release again with new version number 1.0.0
A Simple but Powerful Flat File Database Storage. No need for MySQL or an expensive SQL server, in fact, you just need your current site or application setup. All database entries are stored in files (formatted the way you like).
You can even modify the raw data within the files themselves without ever needing to use the API. And even better you can put all your files in version control and pass them to your team without having out-of-sync SQL databases.
Doesn't that sound awesome?
With Filebase, you are in complete control. Design your data structure the way you want. Use arrays and objects like you know how in PHP. Update and share your data with others and teams using version control. Just remember, upgrading your web/apache server is a lot less than your database server.
Works with PHP 5.6 and PHP 7+
Features
Filebase is simple by design, but has enough features for the more advanced.
- Key/Value and Array-based Data Storing
- Querying data
- Custom filters
- Caching (queries)
- Database Backups
- Formatting (encode/decode)
- Validation (on save)
- CRUD (method APIs)
- File locking (on save)
- Intuitive Method Naming
Installation
Use Composer to install package.
Run composer require aalfiann/Filebase:^1.0
If you do not want to use composer, download the files, and include it within your application, it does not have any dependencies, you will just need to keep it updated with any future releases.
Usage
(1) Config Options
The config is required when defining your database. The options are optional since they have defaults.
Usage Example (all options)
Name | Type | Default Value | Description |
---|---|---|---|
dir |
string | current directory | The directory where the database files are stored. |
backupLocation |
string | current directory (/backups ) |
The directory where the backup zip files will be stored. |
format |
object | \Filebase\Format\Json |
The format class used to encode/decode data |
validate |
array | Check Validation Rules for more details | |
cache |
bool | true | Stores query results into cache for faster loading. |
cache_expires |
int | 60 | How long caching will last (in seconds) |
pretty |
bool | false | Store the data for human readability? Pretty Print |
safe_filename |
bool | true | Automatically converts the file name to a valid name (added: 1.0.13) |
read_only |
bool | false | Prevents the database from creating/modifying files or directories (added: 1.0.14) |
(2) Formatting
Format Class is what defines the encoding and decoding of data within your database files.
You can write your own or change the existing format class in the config. The methods in the class must be static
and the class must implement \Filebase\Format\FormatInterface
The Default Format Class: JSON
Additional Format Classes: YAML
(3) GET (and methods)
After you've loaded up your database config, then you can use the get()
method to retrieve a single document of data.
If document does not exist, it will create a empty object for you to store data into. You can then call the save()
method and it will create the document (or update an existing one).
get()
returns \Filebase\Document
object and has its own methods which you can call.
Method | Details |
---|---|
save() |
Saves document in current state |
delete() |
Deletes current document (can not be undone) |
toArray() |
Array of items in document |
getId() |
Document Id |
createdAt() |
Document was created (default Y-m-d H:i:s) |
updatedAt() |
Document was updated (default Y-m-d H:i:s) |
field() |
You can also use . dot delimiter to find values from nested arrays |
isCache() |
(true/false) if the current document is loaded from cache |
filter() |
Refer to the Custom Filters |
Example:
(4) Create | Update | Delete
As listed in the above example, its very simple. Use $item->save()
, the save()
method will either Create or Update an existing document by default. It will log all changes with createdAt
and updatedAt
. If you want to replace all data within a single document pass the new data in the save($data)
method, otherwise don't pass any data to allow it to save the current instance.
(5) Database Methods
Here is a list of methods you can use on the database class.
Method | Details |
---|---|
version() |
Current version of your Filebase library |
get($id) |
Refer to get() |
has($id) |
Check if a record exist returning true/false |
findAll() |
Returns all documents in database |
count() |
Number of documents in database |
flush(true) |
Deletes all documents. |
flushCache() |
Clears all the cache |
truncate() |
Deletes all documents. Alias of flush(true) |
query() |
Refer to the Queries |
backup() |
Refer to the Backups |
Examples
(6) Validation (optional)
When invoking save()
method, the document will be checked for validation rules (if set).
These rules MUST pass in order for the document to save.
In the above example name
, description
, emails
and config
array keys would be replaced with your own that match your data. Notice that config
has a nested array settings
, yes you can nest validations.
Validation rules:
Name | Allowed Values | Description |
---|---|---|
valid.type |
string , str , integer , int , array |
Checks if the property is the current type |
valid.required |
true , false |
Checks if the property is on the document |
(7) Custom Filters
NOTE Custom filters only run on a single document
Item filters allow you to customize the results, and do simple querying within the same document. These filters are great if you have an array of items within one document. Let's say you store "users" as an array in users.json
, then you could create a filter to show you all the users that have a specific tag, or field matching a specific value.
This example will output all the emails of users who are blocked.
(8) Queries
Queries allow you to search multiple documents and return only the ones that match your criteria.
If caching is enabled, queries will use findAll()
and then cache results for the next run.
To run the query use results()
or if you only want to return the first item use first()
Query Methods:
These methods are optional and they are stackable
Method | Arguments | Details |
---|---|---|
select() |
array or string (comma separated) |
Select only the fields you wish to return (for each document), usage: field1,field2 |
where() |
mixed |
array for simple "equal to" OR where($field, $operator, $value) |
andWhere() |
mixed |
see where() , uses the logical AND |
orWhere() |
mixed |
see where() , this uses the logical OR |
limit() |
int limit, int offset |
How many documents to return, and offset |
orderBy() |
field , sort order |
Order documents by a specific field and order by ASC or DESC |
delete() |
Closure |
Ability to Bulk-delete all items that match |
The below methods execute the query and return results (do not try to use them together)
Method | Details |
---|---|
count() |
Counts and returns the number of documents in results. |
first() |
Returns only the first document in results. |
last() |
Returns only the last document in results. |
results() |
This will return all the documents found and their data as an array. Passing the argument of false will be the same as resultDocuments() (returning the full document objects) |
resultDocuments() |
This will return all the documents found and their data as document objects, or you can do results(false) which is the alias. Also you are able to force sort ASC or DESC directly from document object name like __id ,__created_at and __updated_at . Note: Using this force sort with offset will not sort all documents. Example to Use: ->resultDocuments('__id','DESC') . (added: 1.0.12) |
Comparison Operators:
Name | Details |
---|---|
= or == |
Equality |
=== |
Strict Equality |
!= |
Not Equals |
NOT |
Not Equals (same as != ) |
!== |
Strict Not Equals |
> |
Greater than |
>= |
Greater than or equal |
< |
Less than |
<= |
Less than or equal |
IN |
Checks if the value is within a array |
LIKE |
case-insensitive regex expression search |
NOT LIKE |
case-insensitive regex expression search (opposite) |
REGEX |
Regex search |
(9) Caching
If caching is enabled, it will automatically store your results from queries into sub-directories within your database directory.
Cached queries will only be used if a specific saved cache is less than the expire time, otherwise it will use live data and automatically replace the existing cache for next time use.
(10) Database Backups
By default you can backup your database using $db->backup()->create()
, this will create a .zip
file of your entire database based on your dir
path.
Methods:
These methods can be used when invoking backup()
on your Database
.
create()
Creates a backup of your database (in your backup location.zip
)clean()
Purges all existing backups (.zip
files in your backup location)find()
Returns anarray
of all existing backups (array key bytime()
when backup was created)rollback()
Restore an existing backup (latest available), replaces existing databasedir
Example:
Why Filebase?
Filebase was built for the flexibility to help manage simple data storage without the hassle of a heavy database engine. The concept of Filebase is to provide very intuitive API methods, and make it easy for the developer to maintain and manage (even on a large scale).
Inspired by Flywheel and Flinetone.
How Versions Work
Versions are as follows: Major.Minor.Patch
- Major: Rewrites with completely new code-base.
- Minor: New Features/Changes that breaks compatibility.
- Patch: New Features/Fixes that does not break compatibility.
Filebase will work-hard to be backwards-compatible when possible.
Sites and Users of Filebase
- Grayscale Inc
- VIP Auto
- Ideal Internet
- OnlineFun
- PuzzlePlay
- Square Media LLC
- My Map Directions
- Discount Savings
- Vivint - Smart Homes
If you are using Filebase – send in a pull request and we will add your project here.
Contributions
Anyone can contribute to Filebase. Please do so by posting issues when you've found something that is unexpected or sending a pull request for improvements.
License
Filebase is open-sourced software licensed under the MIT license.