Nodes 2.0.0 · Library for PHP
A database in folders, without a server
Nodes stores any PHP value, such as an array or an object, as a file in a folder: its schema. Query the values by their fields, tie schemas with references that delete in cascade, run triggers before and after every change, and change a value through a queue that keeps processes from overwriting each other.
Free software (GPL 3) · PHP 5.4 or newer · one class, no dependencies
- addNode()a file: contacts/ada
- setNode()new keys added, the rest kept
- getNodes(){age} > 30: ada, grace
Examples
What sets it apart
Each example is a complete script, run with Nodes 2.0.0. The output is what it printed.
Values are files
A schema is a folder and each value one file in it. setNode adds keys and keeps the others; getNodes filters and sorts by the fields.
<?php // Nodes keeps each value in a file of a folder (a schema), with no database server. define('DIV_NODES_ROOT', __DIR__ . '/../data/'); require __DIR__ . '/../vendor/autoload.php'; use divengine\nodes; $db = new nodes('shop/contacts'); $db->delSchema('shop/contacts'); // start empty $db->addSchema('shop/contacts'); $db->addNode(['name' => 'Ada', 'age' => 36], 'ada'); $db->addNode(['name' => 'Linus', 'age' => 29], 'linus'); $db->addNode(['name' => 'Grace', 'age' => 45], 'grace'); // setNode completes the node: new keys are added, the others kept $db->setNode('ada', ['email' => 'ada@example.com']); print_r($db->getNode('ada')); // a condition over the fields, sorted by one of them, at most 10 $older = $db->getNodes(['where' => '{age} > 30', 'order' => 'age', 'limit' => 10]); echo 'Older than 30: ', implode(', ', array_keys($older)), "\n"; echo 'Count: ', $db->getCount(), "\n"; echo "Files in data/shop/contacts:\n"; foreach (scandir(DIV_NODES_ROOT . 'shop/contacts') as $file) { if ($file[0] !== '.') echo " $file\n"; }
Array
(
[name] => Ada
[age] => 36
[email] => ada@example.com
)
Older than 30: ada, grace
Count: 3
Files in data/shop/contacts:
ada
grace
linusReferences that cascade
A property of one schema holds ids of another. Deleting a customer deletes the orders that point to it.
<?php // A reference ties a property of one schema to the ids of another. // Deleting a customer deletes the orders that point to it. define('DIV_NODES_ROOT', __DIR__ . '/../data/'); require __DIR__ . '/../vendor/autoload.php'; use divengine\nodes; $db = new nodes('shop'); $db->delSchema('shop'); // start empty $db->addSchema('shop/customers'); $db->addSchema('shop/orders'); $db->addReference([ 'schema' => 'shop/orders', // orders.customer ... 'property' => 'customer', 'foreign_schema' => 'shop/customers', // ... holds a customer id 'delete_cascade' => true, ]); $db->addNode(['name' => 'Ada'], 'ada', 'shop/customers'); $db->addNode(['name' => 'Linus'], 'linus', 'shop/customers'); $db->addNode(['customer' => 'ada', 'total' => 120], 'order-1', 'shop/orders'); $db->addNode(['customer' => 'linus', 'total' => 80], 'order-2', 'shop/orders'); $db->addNode(['customer' => 'ada', 'total' => 45], 'order-3', 'shop/orders'); echo 'Orders: ', implode(', ', $db->getNodesID('shop/orders')), "\n"; $db->delNode('ada', 'shop/customers'); echo 'After deleting ada: ', implode(', ', $db->getNodesID('shop/orders')), "\n";
Orders: order-1, order-2, order-3 After deleting ada: order-2
Triggers
A function runs before or after a value is added, changed or deleted. It can change the value, or refuse it.
<?php // Triggers run before and after a node is added, changed or deleted. // One can change the node, or refuse it. define('DIV_NODES_ROOT', __DIR__ . '/../data/'); require __DIR__ . '/../vendor/autoload.php'; use divengine\nodes; $db = new nodes('shop/products'); $db->delSchema('shop/products'); // start empty $db->addSchema('shop/products'); $db->addTrigger(DIV_NODES_TRIGGER_BEFORE_ADD, function ($node, $id) { if ($node['price'] <= 0) { echo "refused $id: the price must be positive\n"; return DIV_NODES_ROLLBACK_TRANSACTION; } $node['code'] = strtoupper($id); // what the trigger returns is saved return $node; }); $db->addNode(['name' => 'Apple', 'price' => 10], 'apple'); $db->addNode(['name' => 'Ghost', 'price' => 0], 'ghost'); echo 'Saved: ', implode(', ', $db->getNodesID()), "\n"; print_r($db->getNode('apple'));
refused ghost: the price must be positive
Saved: apple
Array
(
[name] => Apple
[price] => 10
[code] => APPLE
)Changes in turn
setNode can take a function that gets the value as stored and returns the change, while other processes wait their turn.
<?php // setNode() can take a function: it receives the node as stored and returns // the change. Nodes waits for its turn in a queue on disk, so two processes // changing the same node do not overwrite each other. define('DIV_NODES_ROOT', __DIR__ . '/../data/'); require __DIR__ . '/../vendor/autoload.php'; use divengine\nodes; $db = new nodes('shop/stock'); $db->delSchema('shop/stock'); // start empty $db->addSchema('shop/stock'); $db->addNode(['product' => 'apple', 'units' => 10], 'apple'); foreach ([3, 4, 5] as $sold) { $db->setNode('apple', function ($node) use ($sold) { if ($node['units'] < $sold) { echo "cannot sell $sold, only {$node['units']} left\n"; return []; // no change } echo "sold $sold\n"; return ['units' => $node['units'] - $sold]; }); } echo 'Units left: ', $db->getNode('apple')['units'], "\n";
sold 3 sold 4 cannot sell 5, only 3 left Units left: 3
Install
Add it to a project
1. Require it
With Composer.
composer require divengine/nodes:2.0.0
2. Run an example
Save one of the scripts above in an examples folder of the project, then:
php examples/nodes.php
The examples write to a data folder next to them, set with the DIV_NODES_ROOT constant. The examples, with the commands to run them again with Nodes 2.0.0, are in the site's repository.