Doclite document store

DocLite is a very new NoSQL document store for PHP built on top of SQLite, something you can put together during some lockdown evenings and weekends. The library uses the PHP PDO SQLite library to access a SQLite database and automatically manage documents organized into named collections, which are stored as JSON.

DocLite takes advantage of the SQLite JSON1 extension to store, parse, index and query JSON documents – giving you the power and flexibility of a fully transactional and ACID compliant NoSQL solution, yet contained within the local file system.

No need for more complex systems like Mongo, CouchDB or Elasticsearch when your requirements are slim. No need for any external dependencies, just PHP with PDO SQLite enabled.

Doclite is NoSQL document store
NoSQL Databases, Doclite is a NoSQL Document store.

Key features :

  • Agile development and rapid prototyping while your requirements are evolving.
  • Powerful, self-contained NoSQL database for small to medium websites or applications, such as blogs, business website, CMS, CRM or forums.
  • A fast and reliable cache for data retrieved from remote databases, APIs or servers. Process your data in to documents, save and easily query and filter your data as needed.
  • Robust, performant, ACID compliant replacement for weaker, slower, flat-file data stores utilizing JSON, XML or YAML.
  • Application database for web apps installed and run on a local environment.
  • Database for microservices and middleware.
  • Fast in-memory database for data processing or machine learning algorithms.

Getting started with Doclite

The solution require PHP 7.4 or above with PDO SQLite enabled, built against libsqlite ≥ 3.18.0 with JSON1 extension. You can install it with composer using :

composer require dwgebler/doclite

The solution provides both a FileDatabase and MemoryDatabase implementation. To create or open an existing database, simply create a Database object, specifying the file path if using a FileDatabase. So to create a database, if the database name is not specified it will be named data.db by default :

use Gebler\DocLite\{FileDatabase, MemoryDatabase};

// To create or open an existing file database.
$db = new FileDatabase('/path/to/db');

// To open an existing file database in read-only mode.
$db = new FileDatabase('/path/to/existing/db', true);

// To create a new in-memory database.
$db = new MemoryDatabase();

Once you have opened a database, you can obtain a document Collection which will be automatically created if it does not exist. The Collection object can then be used to retrieve, create and manipulate documents. In the example below $user is an instance of a DocLite Document, but you can also hydrate objects of your own custom classes from a collection.

$users = $db->collection("user"); 
// Create a new User in the collection
$user = $users->get();

// Get the automatically generated document ID
$id = $user->getId();

// Set properties by magic set* methods
$user->setUsername("dwgebler");
$user->setRole("admin");
$user->setPassword(password_hash("admin", \PASSWORD_DEFAULT));
$user->setCreated(new \DateTimeImmutable);

// Update the user in the collection
$user->save();

// Retrieve this user later on by ID
$user = $users->get($id);

// Or search for a user by any field
$user = $users->findOneBy(["username" => "dwgebler"]);

Integration with Symfony framework

You can inject the solution as a service into any Symfony application. Simply install the library via Composer as an app dependency, then modify your services.yaml as per the following example :

    app.filedatabase:
        class: Gebler\Doclite\FileDatabase
        arguments:
            $path: "../var/data/app.db"
            $readOnly: false
    app.memorydatabase:
        class: Gebler\Doclite\MemoryDatabase

    Gebler\Doclite\DatabaseInterface: '@app.filedatabase'
    Gebler\Doclite\DatabaseInterface $memoryDb: '@app.memorydatabase'

You can now typehint a DatabaseInterface like any other service, using the alias $memoryDb as the parameter name if you’d like a MemoryDatabase.

DocLite provides a simple, intuitive, flexible and powerful PHP library that you can learn, install and start using in minutes. We don’t have yet any benchmark to compare the performance of the solution, however the project is very well documented for a something written during lockdown evenings. It’s really huge !

Doclite is an open source PHP NoSQL database and document store released under GPL v3 license. More information at https://github.com/dwgebler/doclite

LEAVE A REPLY

Please enter your comment!
Please enter your name here