/SimpleDB

A NoSQL JSON Document Storage Class in PHP

Primary LanguagePHP

SimpleDB

SimpleDB is PHP class which creates a simple API for dealing with JSON document storage.

Folder Schema

PROJECT ROOT
    models
        Simple_DB.php
    json
        FOLDERS HERE
            FILES HERE

If you create the folder structure above and autoload or include this class, everything will work out of the box. If not, you will need to edit the constructor to tell the class where you want to store your files. The default location is a json folder in the project root. In other words, the storage is located at the same folder level as the folder which contains your classes.

If you are interested in protecting your data from prying eyes, use an .htaccess file in the json folder:

Order deny,allow
Deny from all

Basic Usage

Instantiate the class with the name of the type of object you are storing. If you are storing cars, the code would look like this:

$simpleDB = new Simple_DB('car');

This will create a folder for cars. You can think of that folder as a "table."

The class contains four basic methods which attempt to mimic HTTP methods:

Simple_DB::get($id);
Simple_DB::post($content);
Simple_DB::put($id, $content);
Simple_DB::delete($id);

Method Reference

get

If you have existing cars that you want to retrieve, you can do so like this:
$simpleDB->get();

Calling get without the ID parameter will return an array of objects containing all of that type of object. The code above will give you every car in storage. Calling get with an ID parameter returns a single object.

post

$simpleDB->post($content);

This method creates a new "row" or JSON document in storage. It creates an ID for the document and posts your object in plain text to that file. The file will be YOURID.json. You can pass post a string, array or object; but your data will get converted to a JSON object in the end.

put

$simpleDB->put($id, $content);

Similar to the post method, this is usually used to update an existing item in storage. This is not necessarily the case, however. You can use this method to write an item with a custom ID if you'd like.

delete

$simpleDB->delete($id);

The name says it all, and the ID is mandatory.

Advanced Usage

The class also contains a few more public methods that you may find useful.

query

$simpleDB->query($queryString);

This method is used to search for objects matching a certain criteria. The query string is formatted like a GET string. To continue with our car example, you might search for a car like so:

$query = $simpleDB->query('color=blue&manufacturer=Honda');

This will find all blue Hondas, returning an array of objects. Even though you may store complex multi-dimensional objects with this class, the query function will only search the top level. Make sure to include variables that need to be searched in the top level of your data schema to properly make use of this method.

returnSingleId

$car = $simpleDB->query('model=Civic');
$carId = $simpleDB->returnSingleId($car);

This method will give easy access to the ID of the object you retreived using get. Use it in a loop for multiple objects.

timestamp

$car = $simpleDB->query('model=Civic');
$carId = $simpleDB->returnSingleId($car);
$time = $simpleDB->timestamp($carId);

This is a simple method for retreiving the last modified date on the object. It makes use of PHP's filemtime().

getJSON

$car = $simpleDB->query('model=Civic');
$carId = $simpleDB->returnSingleId($car);
$json = $simpleDB->getJSON($carId);

This function returns the literal JSON string without decoding it.