Examples

Server client - PHP

View source
agdb example: managing users and databases and running queries on agdb_server from PHP

What it shows

The PHP version of the server client example using the agnesoft/agdb_api package. It checks the server status, logs in as the admin, creates a user and a memory mapped database and runs all queries in a single batch, linking the inserted nodes with references to the results of earlier queries in the same batch (:0, :1).

Run it

From the repository root:

pnpm run examples --filter agdb_api_php

The script installs the Composer dependencies, builds the agdb_server and runs the example against a freshly started server. The port 3000 must be free (stop any agdb_server you started yourself).

To use the client in your own project, add it with Composer:

composer require agnesoft/agdb_api:dev-main

See the PHP client page for details.

Users and databases

require "vendor/autoload.php";

use Agnesoft\AgdbApi\AgdbApi;
use Agnesoft\AgdbApi\CurlClient;
use Agnesoft\AgdbApi\DbKind;
use Agnesoft\AgdbApi\QueryBuilder;

// Create the client using the native cURL transport.
$client = AgdbApi::new(new CurlClient(), "localhost:3000");

// Runs the status query against the database
// and throws if the server is not accessible.
$client->status();

// Login as server admin.
$client->user_login("admin", "admin");

// Create user "php_user1".
$client->admin_user_add("php_user1", "php_user1");

// Login as "php_user1".
$client->user_login("php_user1", "php_user1");

// Creates memory mapped database "db1" for user "php_user1".
$client->db_add("php_user1", "db1", DbKind::Mapped);

One batch with result references

Within a batch a query can refer to the result of an earlier query by its index prefixed with a colon. The edges are inserted from the result of query :0 (the users root) to the result of query :1 (the user nodes):

$queries = [
    // :0: Inserts a root node aliased "users".
    QueryBuilder::insert()
        ->nodes()
        ->aliases(["users"])
        ->query(),

    // :1: Inserts more nodes with some data.
    QueryBuilder::insert()
        ->nodes()
        ->values([
            [
                "username" => "user1",
                "password" => "password123",
            ],
            [
                "username" => "user2",
                "password" => "password456",
            ],
        ])
        ->query(),

    // :2: Connect the root to the inserted nodes with edges referencing both from previous queries.
    QueryBuilder::insert()->edges()->from(":0")->to(":1")->query(),

    // :3: Find a node starting at the "users" node with specific username.
    QueryBuilder::select()
        ->search()
        ->from("users")
        ->where()
        ->key("username")
        ->value("user1")
        ->query(),
];

// Execute the queries.
$result = $client->db_exec_mut("php_user1", "db1", $queries);

// Print the result of the last query.
print_r($result[1][3]);

The client returns the HTTP status and the list of results, one per query, so $result[1][3] is the result of the search.

Learn more