biensure/part

An instant database alternative, storing JSON, written in PHP

v1.0.1 2025-01-30 17:39 UTC

This package is not auto-updated.

Last update: 2025-05-09 17:53:23 UTC


README

Run

Make sure you have PHP 7.4 and composer installed, see https://getcomposer.org

Create a composer project

Run this command:

composer require biensure/part

Usage

Create a file called index.php like this and run it in your browser

<?php

// Part examples

// include classes
require __DIR__ . '/vendor/autoload.php';

// use Part
use Biensure\Part;

// define your schema
$schema = <<<'JSON'
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "http://localhost/.pitstop/hire.json",
    "type": "object",
    "properties": {
        "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "pattern": "^[a-zA-Z0-9\\-]+(\\s[a-zA-Z0-9\\-]+)*$"
        },
        "age": {
            "type": "integer",
            "minimum": 18,
            "maximum": 100
        },
        "email": {
            "type": "string",
            "maxLength": 128,
            "format": "email"
        },
        "website": {
            "type": ["string", "null"],
            "maxLength": 120,
            "format": "hostname"
        },
        "location": {
            "type": "object",
            "properties": {
                 "country": {
                     "enum": ["US", "CA", "GB"]
                 },
                 "address": {
                     "type": "string",
                     "maxLength": 128
                 }
            },
            "required": ["country", "address"],
            "additionalProperties": false
        },
        "available_for_hire": {
            "type": "boolean"
        },
        "interests": {
            "type": "array",
            "minItems": 3,
            "maxItems": 100,
            "uniqueItems": true,
            "items": {
                "type": "string",
                "maxLength": 64
            }
        },
        "skills": {
            "type": "array",
            "maxItems": 100,
            "uniqueItems": true,
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "minLenght": 1,
                        "maxLength": 64
                    },
                    "value": {
                        "type": "number",
                        "minimum": 0,
                        "maximum": 100,
                        "multipleOf": 0.25
                    },"test": {
                        "type": "string",
                        "minLength": 0,
                        "maxLength": 120,
                        "default": "University"
                    }
                },
                "required": ["name", "value", "test"],
                "additionalProperties": false
            }
        }
    },
    "required": ["name", "age", "email", "location", "available_for_hire", "interests", "skills"],
    "additionalProperties": false
}
JSON;

// create a Part (a collection store) named: "test" with the defined schema
$testPart = new Part("test", $schema);


// insert a record
$dataInsert = <<<'JSON'
{
    "name": "John Doe",
    "age": 31,
    "email": "john@example.com",
    "website": "www.example.com",
    "location": {
        "country": "US",
        "address": "Sesame Street, no. 5"
    },
    "available_for_hire": true,
    "interests": ["php", "html", "css", "javascript", "programming", "web design"],
    "skills": [
        {
            "name": "HTML",
            "value": 100
        },
        {
            "name": "PHP",
            "value": 55
        },
        {
            "name": "CSS",
            "value": 99.5
        },
        {
            "name": "JavaScript",
            "value": 75
        }
    ]
}
JSON;

$dataInsertArray = json_decode($dataInsert, true);
$resultInsert = $testPart->insert($dataInsertArray);

echo "\nINSERT:<pre> ";
var_dump( $resultInsert );
echo "</pre>";


// update the record
$dataUpdate = <<<'JSON'
{
        "name": "John Doe",
        "age": 31,
        "email": "john@example.com",
        "website": null,
        "location": {
            "country": "US",
            "address": "Sesame Street, no. 5"
        },
        "available_for_hire": true,
        "interests": [
            "php",
            "html",
            "css",
            "javascript",
            "programming",
            "web design"
        ],
        "skills": [
            {
                "name": "HTML",
                "value": 60
            },
            {
                "name": "PHP",
                "value": 55
            },
            {
                "name": "CSS",
                "value": 199.5
            },
            {
                "name": "JavaScript",
                "value": 75
            }
        ],
        "_id": 4
    }
JSON;

$dataUpdateArray = json_decode($dataUpdate, true);
$resultUpdate = $testPart->update($dataUpdateArray);

echo "\nUPDATE:<pre> ";
var_dump( $resultUpdate );
echo "</pre>";


// show all records (1)
$allTest = $testPart->findAll();

echo "\nFINDALL:<pre> ";
echo json_encode($allTest, JSON_PRETTY_PRINT);
echo "</pre>";


// drop the Part "test"
echo "\nDROP:<pre> ";
echo "\npart directory before drop>";
var_dump( scandir($_SERVER["DOCUMENT_ROOT"]."/.pitstop/test/") );
$testPart->deletePart();
echo "\npart directory after drop>";
var_dump( scandir($_SERVER["DOCUMENT_ROOT"]."/.pitstop/test/") );
echo "</pre>";
?>
  1. \Pitstopdb\Part is an extension of \sleekdb\store, so for doing queries and joins please see the functions of sleekdb.github.io

Issues

License

FREE

Project status

In development, but used in a project 'Pitbox' an API with WEB-Interface for this database system: