Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/surrealdb/surrealdb.php

SurrealDB SDK for PHP
https://github.com/surrealdb/surrealdb.php

Last synced: 3 months ago
JSON representation

SurrealDB SDK for PHP

Awesome Lists containing this project

README

        




 

The official SurrealDB SDK for PHP.




 

 

 



 

 

 

# surrealdb.php

The official SurrealDB SDK for PHP.

## Documentation

View the SDK documentation [here](https://surrealdb.com/docs/integration/libraries/php).

## How to install

You install the SurrealDB SDK via [Composer](https://getcomposer.org/). If you don't have Composer installed, you can download it [here](https://getcomposer.org/download/).

```sh
composer require surrealdb/surrealdb.php
```

## Getting started

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

```php
// 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](https://surrealdb.com/install) and running on port 8000.

```php
// 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
```bash
./vendor/bin/phpunit -c phpunit.xml
```

### Directory Structure

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