The TursoHTTP
library is a PHP wrapper for Turso HTTP Database API (Only). It simplifies interaction with Turso databases using the Hrana over HTTP protocol. This library provides an object-oriented approach to build and execute SQL queries with same API Interface like PHP Native Extension Turso Client PHP.
- Intention and Courage
- Instinct as a Software Freestyle Engineer
- Strong determination!
- Turso Account
- Don't forget to install PHP on your machine
- A cup of coffee and the music you hate the most
- Dancing (optional: if you are willing)
- libSQL Native Extension like API Interface
- Schema Builder
- Query Builder
- Turso Platform API
- Timezone Support
You can install the TursoHTTP library using Composer:
composer require darkterminal/turso-http
Set the database display timezome in your env
variable. See the list of timezones here
DB_TIMEZOME=Asia/Jakarta
use Darkterminal\TursoHttp\LibSQL;
require_once 'vendor/autoload.php';
$dbname = getenv('DB_URL');
$authToken = getenv('DB_TOKEN');
$db = new LibSQL("dbname=$dbname&authToken=$authToken");
echo $db->version() . PHP_EOL;
$create_table = <<<SQL
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT
)
SQL;
$db->execute($create_table);
<?php
/**
* THIS IS POOR TESTING, IF YOU HAVE KNOWLEDGE ABOUT UNIT TESTING
* PULL REQUEST ARE VERY WELCOME!
*/
use Darkterminal\TursoHttp\core\Enums\DataType;
use Darkterminal\TursoHttp\LibSQL;
use Darkterminal\TursoHttp\sadness\LibSQLBlueprint;
use Darkterminal\TursoHttp\sadness\LibSQLSchemaBuilder;
require_once 'vendor/autoload.php';
try {
$dbname = getenv('DB_URL');
$authToken = getenv('DB_TOKEN');
$db = new LibSQL("dbname=$dbname&authToken=$authToken");
$schemaBuilder = new LibSQLSchemaBuilder($db);
// Creating table
$schemaBuilder->create('contacts', function(LibSQLBlueprint $table) {
$table->increments('id');
$table->string('name');
$table->unique('email');
$table->string('phone');
$table->timestamps();
})->execute();
echo "Table created successfully.\n";
// Add new column in the table
$schemaBuilder->table('contacts', function(LibSQLBlueprint $table) {
$table->addColumn(DataType::TEXT, 'address');
})->execute();
echo "Column added successfully.\n";
// Drop the table
$schemaBuilder->drop('contacts')->execute();
echo "Table contacts successfully dropped!.\n";
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
}
<?php
/**
* THIS IS POOR TESTING, IF YOU HAVE KNOWLEDGE ABOUT UNIT TESTING
* PULL REQUEST ARE VERY WELCOME!
*/
use Darkterminal\TursoHttp\LibSQL;
require_once getcwd() . '/vendor/autoload.php';
$dbname = getenv('DB_URL');
$authToken = getenv('DB_TOKEN');
$db = new LibSQL("dbname=$dbname;authToken=$authToken");
$query = <<<SQL
INSERT INTO contacts (name, email, phone, address) VALUES (?, ?, ?, ?)
SQL;
$db->execute($query, [
'Imam Ali Mustofa',
'[email protected]',
'08123456789',
'Punk Univers'
]);
<?php
/**
* THIS IS POOR TESTING, IF YOU HAVE KNOWLEDGE ABOUT UNIT TESTING
* PULL REQUEST ARE VERY WELCOME!
*/
use Darkterminal\TursoHttp\LibSQL;
use Darkterminal\TursoHttp\sadness\LibSQLQueryBuilder;
require_once getcwd() . '/vendor/autoload.php';
$dbname = getenv('DB_URL');
$authToken = getenv('DB_TOKEN');
$db = new LibSQL("dbname=$dbname;authToken=$authToken");
$sql = new LibSQLQueryBuilder($db);
$contacts = $sql->table('contacts')
->where('address', '=', 'Punk Universe')
->get();
var_dump($contacts);
Manage databases, replicas, and teams with the Turso Platform API using PHP.
This library is licensed under the MIT License - see the LICENSE file for details.