Welcome Guest, Not a member yet? Register   Sign In
Filebase.php - File-based database system
#1

Hello guys, I hope you're doing fine.

I have created a simple class to use on some of my projects to use file-based database system so the client would use a cloud service to sync his data .. etc - I named it Filebase

What is Filebase?
This class allows you to use CodeIgniter without any database server. It brings a homemade file-based, serverless database mechanism.

How it works?

- Once used, Filebase will automatically create a folder with the database name you provide. If you do not provide any, the database name main will be used.
- Each library using this class should have (preferably) its own table, which means its own folder.
- Each record will be store d as a serialized array using in-order:
  1. igbinary_serialize()igbinary_unserialize() if they exist (Look for php_igbinary extension).
  2. bson_encode() / bson_decode() if they exist (php_mongo extension).
  3. json_encode() / json_decode() if none of the function above are available.
  4. serialize() / unserialize() if REALLY none is available even json_encode or json_decode.
How to install?
1. Put the php files "Filebase.php" anywhere you want as long as you will correctly include it.

2. Create a library (CodeIgniter) and include Filebase like so:

Code:
require APPPATH.'third_party/Filebase/Filebase.php';


(assuming that it is inside third_party folder)

3. In library's constructor, after declaring $table property, you can put:

Code:
$this->table = new Filebase(array(
   'path'        => $dbpath,
   'table'       => $tablename,
   'safe_delete' => TRUE/FALSE
), $database);

$dbpath : path to your writable database folder.
$tablename : library's table name.
safe_delete : whether to delete OR hide records (deleted => TRUE)
$database : your database name (default: main)


4. Once done, you can use the $table object like so:
  • $this->table->insert($data) where $data is the array to insert
  • $this->table->update($id, $data) & $this->table->update_all($data, $where)
  • $this->table->delete($id), ...->delete_all($where)
  • ... etc
Feel free to test it a tell me if there is anything I should add/fix/change/remove.

Link to pastebin: HERE
Follow me on Github, you may find interesting things!
Reply
#2

What stops you to use pdo sqlite driver instead?
Reply
#3

This is such a great idea. There are so many websites that have no need for a full mysql database because there is not that much complex data storage - but they need the convenience of being able to update / delete etc the data. This combined with page caching could be a very lightweight yet still very fast loading solution.

What specifically are you thinking when you say 'a cloud service to sync the data' ? How would that work?
Reply
#4

I explain:

A client needed a certain web application so I made it! The problem was with "what he asked" ! The application needs to be installed on local server, accessible by all devices connected to the same router. He has more than one server, a single master database with multiple slaves! BUT, the application needs run even if the internet is off, even for days!

Let's say I use SQLite (@ivantcholakov), if I put the file on dropbox (or another cloud service provider), SQLite is a single file, the sync will create a second file with v2, v3 ... vn right? How to avoid this knowing that the database is shared between all servers? (keep in mind that there might be no internet .. etc)

I looked around, I found lots of solutions, replications and other things, mongodb, couchdb ... So i decided to create my own, serverless, no setup or less setup, fast enough ...

And here I come to @cartalot's question !

All data are stored in a single folder (on the cloud) shared between all servers locally ! Any record inserted into the database will be automatically synchronized as soon as the internet is back!

I hope I could explain the idea behind and I apologize for my english.
Follow me on Github, you may find interesting things!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB