2017 © Pedro Peláez
 

library doru-db

A simple file-based database storing data in JSON.

image

punarinta/doru-db

A simple file-based database storing data in JSON.

  • Sunday, July 22, 2018
  • by punarinta
  • Repository
  • 2 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

DoruDB

A simple file-based database. Stores data in JSON format. Supports basic CRUD operations and concurrency. The name comes from a Proto-Indo-European word 'doru' which meant 'a tree'. This database is one tool in a bigger collection with this name., (*1)

Usage

Connecting to the database

use \DoruDB\Database;

// if no arguments are passed to constructor the database is stored in 'db' folder
$db = new Database('path/to/db');

Inserting a record

$user = $db->create('user', ['name' => 'That Guy']);

// this will insert a document with a unique id and nothing else
$user = $db->create('user');

// this will also insert a record, as update() works as 'upsert' if ID is given
$user = $db->update('user', ['id' => 1337, 'name' => 'This Guy']);

Finding a record

// this is the fastest way to fetch a document, but you need to have an ID
$user = $db->findById('user', 1337);

// 'find' returns one record
$users = $db->find('user', ['filter' => ['name' => 'This Guy']]);

// 'findAll' returns all. NB: filter can use user-defined functions
$users = $db->find('user', ['filter' => ['name' => function ($x)
{ 
    return strpos($x, 'Guy') !== false;
} ]]);

Limit/offset

$users = $db->findAll('user', ['offset' => 1, 'limit' => 1]);

Using limit/offset with filters on the fields that do not have indices is slower than just limit/offset or filters with indices, as the database has to read all documents first to apply limit and offset., (*2)

Sorting

Only sorting by ID is supported for now. Use 'invert' option to use descending sorting., (*3)

$users = $db->findAll('user', ['invert' => true]);

Removing a record

// deletes one document
$db->delete('user', 1337);

// deletes all documents
$db->truncate('user');

Working with indices

Index support is still experimental. Use with care. Indices are used to speed up filter, limit and offset operations., (*4)

// add an index or update it automatically
$db->rebuildIndex('user', 'lastLogin');

// update index manually with one or more documents
$db->updateIndex('user', 'lastLogin', $someDocument);

// remove index completely from the database
$db->removeIndex('user', 'lastLogin');

ToDo

  • make ID immutable
  • unique indices
  • improve index r/w performance
  • sync index on document update
  • counting with non-indexed filter

License

This project is licensed under the terms of MIT license., (*5)

The Versions

22/07 2018

dev-master

9999999-dev

A simple file-based database storing data in JSON.

  Sources   Download

MIT

The Requires

  • php >=7.2.0

 

by Vladimir Osipov

database json nosql

31/01 2017

v0.1.1

0.1.1.0

A simple file-based database storing data in JSON.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

by Vladimir Osipov

database json nosql

29/01 2017

v0.1

0.1.0.0

A simple file-based database storing data in JSON.

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

by Vladimir Osipov

database json nosql