PHP Classes
elePHPant
Icontem

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 (40)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2016-11-10 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 116 All time: 8,702 This week: 662Up
Version License PHP version Categories
simple-memory-db 1.0.0MIT/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.

  Performance   Level  
Name: Joseluis Laso <contact>
Classes: 16 packages by
Country: Spain Spain
Age: 49
All time rank: 104820 in Spain Spain
Week rank: 177 Up6 in Spain Spain Up
Innovation award
Innovation award
Nominee: 6x

Winner: 2x

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 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.dist 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)
  Plain text file AbstractTable.php Class Class source
  Plain text file RepositoryInterface.php Class Class source
  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
  Plain text file CountryTable.php Class Class source
  Accessible without login Plain text file customers.json Data Auxiliary data
  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
  Plain text file TaxTable.php Class Class source

  Files folder image Files  /  src  /  Example  
File Role Description
  Plain text file Customer.php Class Class source
  Accessible without login Plain text file customers.json Data Auxiliary data
  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
  Plain text file TaxTable.php Class Class source

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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:116
This week:0
All time:8,702
This week:662Up