SlimDB

A times developers usually have a problem of creating a small project just for the porpose of testing or learning a new feature in web development. The problem arises when there is a need to store a little bit of information to the database and you feel lazy to create a new database in SQL or MySQL. Document databases have been a really big solution to that problem but haven’t been so friendly to users. That is why i created SlimDB. Its a solution to all those problems and others.

Lets dive into it.

SlimDB
SlimDB is a simple inplementation of JSON document database purely written in PHP.

The PHP class can be used to manage the SlimDB file and its described below.

Open Database
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
?>
Output in db.slim
{"name":"db\r\n","tables":{},"data":{}}

Create Table
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
$db->newTable(‘{“name”: “tblusers”, “cols”: [“name”, “age”, “gender”]}’);
?>
Output in db.slim
{"name":"db\r\n","tables":{"tblusers":["_id","name","age", "gender", "created_at","updated_at"]},"data":{"tblusers":[]}}

Drop Table
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
$db->dropTable(“tblusers”);
?>
Output in db.slim
{"name":"db\r\n","tables":{},"data":{}}

Select Table
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
$db->setTable(“tblusers”);
?>
Insert Data
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
$db->setTable(“tblusers”)
->insert(‘{“name”: “Aminu Bakori”, “age”: “100”, “gender”: “Male”}’);
?>
Output in db.slim
{"name":"db\r\n","tables":{"tblusers":["_id","name","age", "gender", "created_at","updated_at"]},"data":{"tblusers":[{"_id": "53f934f03333340880001e9b","name": "Aminu Bakori","age": "100","gender": "Male","created_at":"2014-08-24 2:42:24","updated_at":"2014-08-24 3:22:48"}]}}

Select Data
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
$db->setTable(“tblusers”);

$data = $db->select(‘{“name”: “Aminu Bakori”, “age”: “100”}’);
var_dump($data);
?>
Output in browser
array (size=1) 0 => object(stdClass)[2] public '_id' => string '53f934f03333340880001e9b' (length=24) public 'name' => string 'Aminu Bakori' (length=12) public 'age' => string '100' (length=3) public 'created_at' => string '2014-08-24 2:42:24' (length=18) public 'updated_at' => string '2014-08-24 3:22:48' (length=18)

Update Data
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
$db->setTable(“tblusers”)
->update(‘{“where”: {“_id”: “53f934f03333340880001e9b”}, “set”: {“age”: “200”}}’);
?>
Output in db.slim
{"name":"db\r\n","tables":{"tblusers":["_id","name","age", "gender", "created_at","updated_at"]},"data":{"tblusers":[{"_id": "53f934f03333340880001e9b","name": "Aminu Bakori","age": "200","gender": "Male","created_at":"2014-08-24 2:42:24","updated_at":"2014-08-24 3:22:48"}]}}

Delete Data
<?php
require 'SlimDB.php';

$db = new SlimDB();
$db->open(“db.slim”);
$db->setTable(“tblusers”)
->delete(‘{“name”: “Aminu Bakori”}’);
?>

Output in db.slim

{"name":"db\r\n","tables":{"tblusers":["_id","name","age", "gender", "created_at","updated_at"]},"data":{"tblusers":[]}}
SlimDB has been very useful to me in a lot of Projects as i don’t need to do the nasty SQL to store little information on my server. Hope that helps alot.

I'm Aminu Bakori, software developer from Nigeria - A country in Africa. Yes Africa is not a country. I founded Payant.ng and some other amazing startups in Nigeria. If i'm not building softwares, you will see me hanging around the geeks at tech conferences. I love reading and teaching about software development.

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

About Me

About Me

I'm Aminu Bakori, software developer from Nigeria - A country in Africa. Yes Africa is not a country. I founded Payant.ng and some other amazing startups in Nigeria. If i'm not building softwares, you will see me hanging around the geeks at tech conferences. I love reading and teaching about software development.

Social Profiles