PHP code example of meita / jsonbolt

1. Go to this page and download the library: Download meita/jsonbolt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meita / jsonbolt example snippets




use Meita\JsonBolt\Cache\FileCache;
use Meita\JsonBolt\Database;

$db = new Database(__DIR__ . '/data', [
    'cache' => new FileCache(__DIR__ . '/cache'),
    'cache_ttl' => 300,
]);

$users = $db->collection('users');

$user = $users->insert([
    'name' => 'Sara',
    'email' => '[email protected]',
]);

$found = $users->find($user['id']);

$active = $users
    ->where('active', true)
    ->orderBy('name')
    ->limit(10)
    ->get();

$db = new Database(__DIR__ . '/data', [
    'id_key' => 'id',           // default
    'id_strategy' => 'increment', // increment or random
    'cache' => $cache,          // any PSR-16 cache
    'cache_ttl' => 300,         // seconds or DateInterval
    'cache_enabled' => true,
    'cache_prefix' => 'dbe',
    'relations' => [/* ... */],
]);

$users = $db->collection('users');

$user = $users->insert(['name' => 'Mona']);
$users->insertMany([
    ['name' => 'Ali'],
    ['name' => 'Noor'],
]);

$user = $users->find(1);
$many = $users->findMany([1, 2, 3]);

$updated = $users->update(1, ['name' => 'Mona A.']);
$deleted = $users->delete(1);

$results = $users
    ->where('age', '>=', 18)
    ->orWhere('role', 'admin')
    ->orderBy('name', 'asc')
    ->offset(10)
    ->limit(20)
    ->select('id', 'name', 'email')
    ->get();

$first = $users->where('email', 'like', '%@example.com')->first();
$count = $users->where('active', true)->count();

$updated = $users->where('active', false)->update(['flagged' => true]);
$deleted = $users->where('last_login', '<', '2022-01-01')->delete();

$db = new Database(__DIR__ . '/data', [
    'relations' => [
        'users' => [
            'posts' => [
                'type' => 'hasMany',
                'collection' => 'posts',
                'foreignKey' => 'user_id',
                'localKey' => 'id',
            ],
            'profile' => [
                'type' => 'hasOne',
                'collection' => 'profiles',
                'foreignKey' => 'user_id',
                'localKey' => 'id',
            ],
        ],
        'posts' => [
            'user' => [
                'type' => 'belongsTo',
                'collection' => 'users',
                'foreignKey' => 'user_id',
                'ownerKey' => 'id',
            ],
            'tags' => [
                'type' => 'belongsToMany',
                'collection' => 'tags',
                'pivot' => 'post_tag',
                'foreignPivotKey' => 'post_id',
                'relatedPivotKey' => 'tag_id',
                'localKey' => 'id',
                'relatedKey' => 'id',
            ],
        ],
    ],
]);

$users = $db->collection('users')->with(['posts', 'profile'])->get();
$posts = $db->collection('posts')->with('tags')->get();

$users = $db->collection('users')->with('posts.tags')->get();

use Meita\JsonBolt\Cache\FileCache;

$db = new Database(__DIR__ . '/data', [
    'cache' => new FileCache(__DIR__ . '/cache'),
    'cache_ttl' => 300,
]);



eita\JsonBolt\Cache\FileCache;
use Meita\JsonBolt\Database;

$db = new Database(__DIR__ . '/data', [
    'cache' => new FileCache(__DIR__ . '/cache'),
    'cache_ttl' => 600,
    'relations' => [
        'users' => [
            'posts' => [
                'type' => 'hasMany',
                'collection' => 'posts',
                'foreignKey' => 'user_id',
                'localKey' => 'id',
            ],
        ],
        'posts' => [
            'user' => [
                'type' => 'belongsTo',
                'collection' => 'users',
                'foreignKey' => 'user_id',
                'ownerKey' => 'id',
            ],
            'tags' => [
                'type' => 'belongsToMany',
                'collection' => 'tags',
                'pivot' => 'post_tag',
                'foreignPivotKey' => 'post_id',
                'relatedPivotKey' => 'tag_id',
                'localKey' => 'id',
                'relatedKey' => 'id',
            ],
        ],
    ],
]);

$users = $db->collection('users');
$posts = $db->collection('posts');
$tags = $db->collection('tags');
$postTag = $db->collection('post_tag');

$alice = $users->insert(['name' => 'Alice', 'email' => '[email protected]']);
$bob = $users->insert(['name' => 'Bob', 'email' => '[email protected]']);

$post1 = $posts->insert(['user_id' => $alice['id'], 'title' => 'Hello JSONBolt']);
$post2 = $posts->insert(['user_id' => $alice['id'], 'title' => 'File DB Tips']);
$post3 = $posts->insert(['user_id' => $bob['id'], 'title' => 'Caching Fast']);

$tagFast = $tags->insert(['name' => 'fast']);
$tagJson = $tags->insert(['name' => 'json']);

$postTag->insertMany([
    ['post_id' => $post1['id'], 'tag_id' => $tagFast['id']],
    ['post_id' => $post1['id'], 'tag_id' => $tagJson['id']],
    ['post_id' => $post2['id'], 'tag_id' => $tagJson['id']],
]);

$recent = $posts
    ->where('title', 'like', '%JSON%')
    ->orderBy('title', 'asc')
    ->with(['user', 'tags'])
    ->get();

print_r($recent);

$activeUsers = $users
    ->where('email', 'contains', '@example.com')
    ->with('posts')
    ->get();

print_r($activeUsers);

// config/app.php
'providers' => [
    Meita\JsonBolt\Laravel\DBEServiceProvider::class,
],
'aliases' => [
    'DBE' => Meita\JsonBolt\Laravel\Facades\DBE::class,
],

use Meita\JsonBolt\Database;

$db = app(Database::class);
$users = $db->collection('users')->where('active', true)->get();
bash
php artisan vendor:publish --tag=dbe-config