surrealdb/surrealdb.php

官方SurrealDB PHP驱动程序

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

This package is auto-updated.

Last update: 2024-09-20 21:13:08 UTC


README


 

官方SurrealDB SDK for PHP。


     

     

surrealdb.php

官方SurrealDB SDK for PHP。

文档

查看SDK文档 这里

如何安装

您可以通过 Composer 安装SurrealDB SDK。如果您尚未安装Composer,您可以 从这里 下载。

composer require surrealdb/surrealdb.php

入门指南

要开始,您需要创建SurrealDB HTTP或WebSocket类的实例。

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

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

基本查询

在PHP SDK中,我们有一个简单的API,允许您与SurrealDB交互。以下示例展示了如何与数据库交互。

以下示例需要SurrealDB已 安装 并在8000端口运行。

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

$db->connect("https://: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();

贡献

要求

  • PHP 8.1或更高版本
  • Composer
  • SurrealDB 1.4.0或更高版本

运行测试

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

目录结构

  • src - 库的源代码
  • tests - 库的单元测试