surrealdb/surrealdb.php

Official SurrealDB PHP Driver

1.0.0-beta.1 2024-05-13 19:48 UTC

This package is auto-updated.

Last update: 2024-05-14 07:42:20 UTC


README


surreal.svg   php.svg

The official SurrealDB SDK for PHP.


68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617475732d626574612d6666303062622e7376673f7374796c653d666c61742d737175617265   68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63732d766965772d3434636331312e7376673f7374796c653d666c61742d737175617265   68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7375727265616c64622f7375727265616c64622e7068703f7374796c653d666c61742d737175617265   68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f7375727265616c64622f7375727265616c64622e7068703f7374796c653d666c61742d737175617265

68747470733a2f2f696d672e736869656c64732e696f2f646973636f72642f3930323536383132343335303539393233393f6c6162656c3d646973636f7264267374796c653d666c61742d73717561726526636f6c6f723d356136366636   68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769747465722d666f6c6c6f775f75732d3164396266302e7376673f7374796c653d666c61742d737175617265   68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b6564696e2d636f6e6e6563745f776974685f75732d3061363663322e7376673f7374796c653d666c61742d737175617265   68747470733a2f2f696d672e736869656c64732e696f2f62616467652f796f75747562652d7375627363726962652d6663316331632e7376673f7374796c653d666c61742d737175617265

surrealdb.php

The official SurrealDB SDK for PHP.

Documentation

View the SDK documentation here.

How to install

You install the SurrealDB SDK via Composer. If you don't have Composer installed, you can download it here.

composer require surrealdb/surrealdb.php

Getting started

To get started, you need to create a new instance of the SurrealDB HTTP or WebSocket Class.

// Make a new instance of the SurrealDB class. Use the ws or wss protocol for having WebSocket functionality.
$db = new Surreal\Surreal();

$db->connect("http://localhost:8000", [
    "namespace" => "test",
    "database" => "test"
]);

Basic Querying

In the PHP SDK, We have a simple API that allows you to interact with SurrealDB. The following example shows how to interact with the database.

The example below requires SurrealDB to be installed and running on port 8000.

// Connect set the specified namespace and database.
$db = new Surreal\Surreal();

$db->connect("http://localhost:8000", [
    "namespace" => "test",
    "database" => "test"
]);

// We want to authenticate as a root user.
$token = $db->signin([
    "user" => "root",
    "pass" => "root"
]);

// Create a new person in the database with a custom id.
$person = $db->create("person", [
    "title" => "Founder & CEO",
    "name" => [
        "first" => "Tobie",
        "last" => "Morgan Hitchcock" 
    ],
    "marketing" => true
]); 

// Get the person with the name "John Doe".
$record = Surreal\Cbor\Types\RecordId::create("person", "john");
$person = $db->select($record);

// Update a person record with a specific id
$record = Surreal\Cbor\Types\RecordId::create("person", "john");
$person = $db->merge($record, ["age" => 31]);

// Select all people records.
$people = $db->select("person");  

// Perform a custom advanced query.
$groups = $db->query('SELECT marketing, count() FROM $tb GROUP BY marketing', [
    "tb" => Surreal\Cbor\Types\Table::create("person")
]);

// Close the connection between the application and the database.
$db->disconnect();

Contributing

Requirements

  • PHP 8.1 or higher
  • Composer
  • SurrealDB 1.4.0 or higher

Run tests

./vendor/bin/phpunit -c phpunit.xml

Directory Structure

  • src - The source code of the library
  • tests - The unit tests of the library