PHP Classes

Simple PHP Memory Database: Manipulate databases stored in JSON format files

Recommend this page to a friend!
  Info   View files Example   View files View files (42)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 163 All time: 8,910 This week: 200Up
Version License PHP version Categories
simple-memory-db 1.0.1MIT/X Consortium ...5PHP 5, Databases, Files and Folders
Description 

Author

This class can manipulate databases stored in JSON format files.

It provides an abstract table class that can insert, update, remove, find all records or records by ID.

The database table data is loaded and saved in files in the JSON format.

Picture of Joseluis Laso
  Performance   Level  
Name: Joseluis Laso <contact>
Classes: 16 packages by
Country: Spain Spain
Age: 56
All time rank: 92519 in Spain Spain
Week rank: 91 Up4 in Spain Spain Up
Innovation award
Innovation award
Nominee: 6x

Winner: 2x

Example

<?php

namespace JLaso\SimpleMemoryDb\Example;

require_once
__DIR__.'/../../vendor/autoload.php';

use
JLaso\SimpleMemoryDb\RepositoryInterface;

/** @var RepositoryInterface[] $databases */
$databases = [];

$databases['customers'] = new CustomerTable(__DIR__.'/customers.json');
$databases['taxes'] = new TaxTable(__DIR__.'/taxes.json');

foreach (
$databases['customers']->findAll() as $customer) {
   
$tax = $databases['taxes']->find($customer['tax_type_id']);

   
printf(
       
"Customer (%d) %s applies %s [%f]\r\n",
       
$customer['id'],
       
$customer['name'],
       
$tax['name'],
       
$tax['percent']
    );
}

printf("There are %d customers\r\n", $databases['customers']->count());


Details

jlaso/simple-memory-db

Build Status

A simple db in memory, that has to be populated from JSON data.

It lives entirely in memory, quick to access data and manipulate in order to process data at very high speed.

Installation

You need only to require this package in your project


### Look at the Examples folders to see how to use it

#### Example

You have a very simple example with two tables: customers and taxes, each customer has a tax_type associated.

In order to implement your data you have to create a class extending AbstractTable and declare the property $indexMap

by default ```id``` is automatically indexed. So, this field is mandatory in every table, must come in the json.

namespace JLaso\SimpleMemoryDb\Example;

use JLaso\SimpleMemoryDb\AbstractTable;

class CustomerTable extends AbstractTable {

protected $indexMap = [
    "tax_type_id",
];

}


#### BigExample

You can just squeeze it in order to know how big is the limit of this database is in your system.

In order to populate the memory tables with real data, the json files can be generated parametrically:

And to see the results


### ProcessRecord

If you need to add some extra fields or process the records somehow when they are loaded in memory you can implement the method ```processRecord``` in your table

class TaxTable extends AbstractTable {

protected $subtypes = [
    0 => 'mandatory',
    1 => 'optional',
    2 => 'regulated',
    3 => 'unknown',
];

protected function processRecord(&$record)
{
    $module = count($this->subtypes);
    $record['subtype'] = $this->subtypes[intval($record['value'] * 100) % $module];
    
    return true;  // or return false if you want to filter this record
}

}


# Modifier methods

Although is not a real database you can insert new elements or remove the existent ones:

## Insert

- ```insert($data)```

This insert the new record in memory and updates 
the indices to make it accessible.
`$data` can be an array or an object that implements the `JLaso\SimpleMemoryDb\ToArrayInterface` 

## Update

This is an alias for insert, currently insert just replaces the existent copy of the record inserted if any.

## Remove

- ```remove($id)```

This removes the record pointed by ```$id``` and 
updates the indices to make it not accessible.


# Storing method

And, why not ... dump it into a json file again.

- ```saveToJsonFile($fileName)```

# Find methods

## find($id)

Just fetches the current record pointed by `$id` or null if does not exist.

## findAll($field, $value, $filterCallback)

if `$field` and `$value` are null returns all the records.

Always passes the record to `$filterCallback` being to added in order to know if 
passes the filter.  Obviously if `$filterCallback` is null no filter applied.
 
You can check the examples on the Tests folder.
 
## Exceptions
 
Since version 1.5 added the property `$notFoundThrowsException` in order to throw or not 
an Excepcion in the case the record is not found.



  Files folder image Files  
File Role Description
Files folder imagebin (9 files)
Files folder imagesrc (3 files, 2 directories)
Files folder imageTests (5 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGES.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file docker-compose.yml Data Auxiliary data
Accessible without login Plain text file Dockerfile Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file php-git-hooks.yml Data Auxiliary data
Accessible without login Plain text file phpci.yml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file PmdRules.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  bin  
File Role Description
  Accessible without login Plain text file jsonlint Data Auxiliary data
  Accessible without login Plain text file pdepend Data Auxiliary data
  Accessible without login Plain text file php-cs-fixer Data Auxiliary data
  Accessible without login Plain text file phpcbf Data Auxiliary data
  Accessible without login Plain text file phpcs Data Auxiliary data
  Accessible without login Plain text file phploc Data Auxiliary data
  Accessible without login Plain text file phpmd Data Auxiliary data
  Accessible without login Plain text file phpunit Data Auxiliary data
  Accessible without login Plain text file phpunit-randomizer Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageBigExample (8 files)
Files folder imageExample (6 files)
  Accessible without login Plain text file AbstractTable.php Class Class source
  Accessible without login Plain text file RepositoryInterface.php Class Class source
  Accessible without login Plain text file ToArrayInterface.php Class Class source

  Files folder image Files  /  src  /  BigExample  
File Role Description
  Accessible without login Plain text file countries.json Data Auxiliary data
  Accessible without login Plain text file CountryTable.php Class Class source
  Accessible without login Plain text file customers.json Data Auxiliary data
  Accessible without login Plain text file CustomerTable.php Class Class source
  Accessible without login Plain text file demo.php Example Example script
  Accessible without login Plain text file generate.php Example Example script
  Accessible without login Plain text file taxes.json Data Auxiliary data
  Accessible without login Plain text file TaxTable.php Class Class source

  Files folder image Files  /  src  /  Example  
File Role Description
  Accessible without login Plain text file Customer.php Class Class source
  Accessible without login Plain text file customers.json Data Auxiliary data
  Accessible without login Plain text file CustomerTable.php Class Class source
  Accessible without login Plain text file demo.php Example Example script
  Accessible without login Plain text file taxes.json Data Auxiliary data
  Accessible without login Plain text file TaxTable.php Class Class source

  Files folder image Files  /  Tests  
File Role Description
  Accessible without login Plain text file AbstractTestCase.php Class Class source
  Accessible without login Plain text file DbTest.php Class Class source
  Accessible without login Plain text file FilterTest.php Class Class source
  Accessible without login Plain text file ToArrayTest.php Class Class source
  Accessible without login Plain text file UpdateTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:163
This week:0
All time:8,910
This week:200Up