DEV Community

Cover image for Using Gitlab as JSONDB
Imam Ali Mustofa
Imam Ali Mustofa

Posted on

Using Gitlab as JSONDB

GitHub logo darkterminal / GitlabDB

A PHP Class that reads JSON file as a database. Use for sample DBs using Gitlab API inspire from donjajo/jsondb

GitlabDB

A PHP Class that reads JSON file as a database. Use for sample DBs using Gitlab API inspire from donjajo/php-jsondb

Usage

Install package

composer require darkterminal/GitlabDB
Enter fullscreen mode Exit fullscreen mode

Initialize

<?php
use GitlabDB\GitlabDB;

$options['personal_access_token']    = "YOUR_GITLAB_ACCESS_TOKEN";
$options['project_id']               = "YOUR_GITLAB_PROJECT_ID";
$options['branch']                   = "YOUR_GITLAB_BRANCH";
$options['cloud_url']                = "YOUR_GITLAB_URL";

$path = 'YOUR_PATH_ON_GITLAB';

$json_db = new GitlabDB( $options, $path ); // Or passing the file path of your json files with no trailing slash, default is the root directory. E.g.  new GitlabDB( $options, 'database' )
Enter fullscreen mode Exit fullscreen mode

Inserting

Insert into your new JSON file. Using users.json as example here

NB: Columns inserted first will be the only allowed column on other inserts

<?php
$json_db->insert( 'users.json'
    [
        'name' => 'Thomas',
        
Enter fullscreen mode Exit fullscreen mode

A PHP Class that reads JSON file as a database. Use for sample DBs using Gitlab API inspire from donjajo/php-jsondb

Usage

Install package

composer require darkterminal/GitlabDB
Enter fullscreen mode Exit fullscreen mode

Initialize

<?php
use GitlabDB\GitlabDB;

$options['personal_access_token']    = "YOUR_GITLAB_ACCESS_TOKEN";
$options['project_id']               = "YOUR_GITLAB_PROJECT_ID";
$options['branch']                   = "YOUR_GITLAB_BRANCH";
$options['cloud_url']                = "YOUR_GITLAB_URL";

$path = 'YOUR_PATH_ON_GITLAB';

$json_db = new GitlabDB( $options, $path ); // Or passing the file path of your json files with no trailing slash, default is the root directory. E.g.  new GitlabDB( $options, 'database' )
Enter fullscreen mode Exit fullscreen mode

Inserting

Insert into your new JSON file. Using users.json as example here

NB: Columns inserted first will be the only allowed column on other inserts

<?php
$json_db->insert( 'users.json',
    [
        'name' => 'Thomas',
        'state' => 'Nigeria',
        'age' => 22
    ]
);
Enter fullscreen mode Exit fullscreen mode

Get

Get back data, just like MySQL in PHP

All columns:
<?php
$users = $json_db->select( '*' )
    ->from( 'users.json' )
    ->get();
print_r( $users );
Enter fullscreen mode Exit fullscreen mode
Custom Columns:
<?php
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->get();
print_r( $users );

Enter fullscreen mode Exit fullscreen mode
Where Statement:

This WHERE works as AND Operator at the moment or OR

<?php
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->get();
print_r( $users );

// Defaults to Thomas OR Nigeria
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ] )
    ->get();
print_r( $users );

// Now is THOMAS AND Nigeria
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas', 'state' => 'Nigeria' ], 'AND' )
    ->get();
print_r( $users );


Enter fullscreen mode Exit fullscreen mode
Where Statement with regex:

By passingGitlabDB::regex to where statement, you can apply regex searching. It can be used for implementing LIKE or REGEXP_LIKE clause in SQL.

$users = $json_db->select( 'name, state' )
    ->from( "users" )
    ->where( array( "state" => GitlabDB::regex( "/ria/" )), GitlabDB::AND )
    ->get();
print_r( $users );
// Outputs are rows which contains "ria" string in "state" column.
Enter fullscreen mode Exit fullscreen mode
Order By:

Thanks to Tarun Shanker for this feature. By passing the order_by() method, the result is sorted with 2 arguments of the column name and sort method - GitlabDB::ASC and GitlabDB::DESC

<?php
$users = $json_db->select( 'name, state'  )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->order_by( 'age', GitlabDB::ASC )
    ->get();
print_r( $users );
Enter fullscreen mode Exit fullscreen mode

Updating Row

You can also update same JSON file with these methods

<?php
$json_db->update( [ 'name' => 'Oji', 'age' => 10 ] )
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->trigger();

Enter fullscreen mode Exit fullscreen mode

Without the **where()* method, it will update all rows*

Deleting Row

<?php
$json_db->delete()
    ->from( 'users.json' )
    ->where( [ 'name' => 'Thomas' ] )
    ->trigger();

Enter fullscreen mode Exit fullscreen mode

Without the **where()* method, it will deletes all rows*

Exporting to MySQL

You can export the JSON back to SQL file by using this method and providing an output

<?php
$json_db->to_mysql( 'users.json', 'users.sql' );
Enter fullscreen mode Exit fullscreen mode

Disable CREATE TABLE

<?php
$json_db->to_mysql( 'users.json', 'users.sql', false );
Enter fullscreen mode Exit fullscreen mode

Exporting to XML

Tarun Shanker also provided a feature to export data to an XML file


php
<?php
if( $json_db->to_xml( 'users.json', 'users.xml' ) ) {
    echo 'Saved!';
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)