PHP code example of mesahub / client

1. Go to this page and download the library: Download mesahub/client 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/ */

    

mesahub / client example snippets


use Mesahub\MesahubClient;

$client = new MesahubClient(
    apiKey: 'shs_your_api_key',        // from mesahub.app → Settings → API Keys
    apiUrl: 'https://api.mesahub.app', // or your dedicated / self-hosted instance URL
);

$db    = $client->db('my-app-db');    // your database slug from the dashboard
$users = $db->table('users');

// High-level table API
$all    = $users->find(where: ['active' => 1], limit: 20);
$alice  = $users->findOne(where: ['email' => '[email protected]']);
$count  = $users->count(where: ['active' => 1]);
$newRow = $users->insert(['name' => 'Bob', 'email' => '[email protected]', 'active' => 1]);

$users->update(where: ['id' => $newRow['id']], set: ['name' => 'Robert']);
$users->delete(where: ['id' => $newRow['id']]);

// Raw SQL
$result = $db->query('SELECT * FROM users WHERE active = ?', [1]);
print_r($result->rows);

$db->exec('CREATE TABLE IF NOT EXISTS logs (msg TEXT, created_at TEXT)');

$info   = MesahubClient::parseMesahubUrl('mh://[email protected]/mydb');
$client = new MesahubClient(
    apiKey:      $info['api_key'],
    apiUrl:      $info['api_url'],
    routePrefix: $info['route_prefix'],
);
$db = $client->db($info['db_name']);

// Shorthand equality
$users->find(where: ['active' => 1]);

// Comparison operators
$users->find(where: ['age' => ['gte' => 18], 'score' => ['lt' => 100]]);

// LIKE
$users->find(where: ['name' => ['like' => '%alice%']]);

// IN / NOT IN
$users->find(where: ['role' => ['in' => ['admin', 'editor']]]);

// NULL checks
$users->find(where: ['deleted_at' => ['is_null' => true]]);
$users->find(where: ['verified_at' => ['is_not_null' => true]]);

$users->find(
    where:   ['active' => 1],
    select:  ['id', 'name', 'email'],
    orderBy: [['column' => 'created_at', 'direction' => 'desc']],
    limit:   10,
    offset:  20,
);

$db->table('logs')->insertMany(
    rows:       [['msg' => 'started'], ['msg' => 'done']],
    onConflict: 'ignore', // 'ignore' | 'replace' | ''
);

$files = $db->files;

// List
$listing = $files->list(limit: 10, folderPrefix: 'reports/');

// Upload
$record = $files->upload(file_get_contents('report.pdf'), 'report.pdf', 'application/pdf');
echo $record->url;

// Download
$data = $files->download($record->id);

// Delete
$files->delete($record->id);

use Mesahub\Errors\MesahubException;
use Mesahub\Errors\AuthenticationException;
use Mesahub\Errors\NotFoundException;

try {
    $db->query('SELECT * FROM users');
} catch (AuthenticationException $e) {
    echo 'Invalid API key';
} catch (MesahubException $e) {
    echo "[{$e->errorCode}] {$e->statusCode}: {$e->getMessage()}";
}

$client = new MesahubClient(
    apiKey:      'your-admin-token',
    apiUrl:      'http://localhost:4004',
    routePrefix: 'api',
);