DEV Community

Cover image for The PHP library for Turso HTTP
Imam Ali Mustofa
Imam Ali Mustofa

Posted on

The PHP library for Turso HTTP

I was one of those waiting for the Turso PHP SDK, but many said just use the HTTP SDK. OK that's very sad, in the end, I made this stupid decision.


When Turso was just born and started to be talked about on the Internet. I started to pay attention a little, especially with Turso often pronounced by ThePrimeangen (sorry, I wrote this article using Google Translate).

Then I searched about Turso Database and found something very interesting!

SQLite is the most widely used database engine in the world because it is the easiest and works best. Turso takes him everywhere.

Starting from this open card, as an SFE (Software Freestyle Engineer) who was curious about how to get started, I opened the documentation and looked at the available SDKs.

Sadly, I don't see PHP. Yes, I can use Typescript/JS, I'm stupid for Rust, childlike when it comes to Go, and playful when it comes to Python. Since I'm a Lambo Programmer living in a Warehouse, I want PHP to be there!


Turso over HTTP

When I saw the words Turso Over HTTP on the card in the SDK documentation, I was a little offended because there was a paragraph that said; "Using something else? Learn how to use Turso over HTTP."

Wait a minute, why should I be offended?!

Maybe they thought PHP was a difficult language and it wasn't easy to make a compatible SDK, so they gave up on making it HTTP only.

Wait a minute, why am I so stupid?!

HTTP/PHP/HTTP/Hypertext preprocessor yeah! Thank you my stupid brain! They don't give up on PHP, but they give them the freedom to be creative and innovate. So my decision was very correct! (yes I am! Because I'm too handsome to do this)


I created the TursoHTTP Library for PHP which is equipped with a SadQuery Generator to make it easier to create SQL queries for lazy people who will ask; What is the accompanying ORM? This is Madapunker!

The TursoHTTP library is a PHP wrapper for Turso HTTP Database API. 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, retrieve query results, and access various response details.

<?php

use Darkterminal\DataType;
use Darkterminal\SadQuery;
use Darkterminal\TursoHTTP;

require_once . 'vendor/autoload.php';

$databaseName       = "database-name";
$organizationName   = "organization-name";
$token              = "your-turso-database-token";

$tursoAPI = new TursoHTTP($databaseName, $organizationName, $token);
$query = new SadQuery();

$createTableUsers = $query->createTable('users')
    ->addColumn('userId', DataType::INTEGER, ['PRIMARY KEY', 'AUTOINCREMENT'])
    ->addColumn('name', DataType::TEXT)
    ->addColumn('email', DataType::TEXT)
    ->endCreateTable();

$createNewUser = $query->insert('users', ['name' => 'darkterminal', 'email' => 'darkterminal@duck.com'])->getQuery();

$tableCreated = $tursoAPI
    ->addRequest("execute", $createTableUsers)
    ->addRequest("close")
    ->queryDatabase()
    ->toJSON();
echo $tableCreated . PHP_EOL;

$userCreated = $tursoAPI
    ->addRequest("execute", $createNewUser)
    ->addRequest("close")
    ->queryDatabase()
    ->toJSON();
echo $userCreated . PHP_EOL;
Enter fullscreen mode Exit fullscreen mode

Isn't this interesting!? Very easy and true to reality!

If you are curious about the complete documentation, please visit the GitHub repository.

GitHub logo darkterminal / turso-http

The PHP library for Turso HTTP Database API

TursoHTTP

The TursoHTTP library is a PHP wrapper for Turso HTTP Database API. 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, retrieve query results, and access various response details.

Requirements

  • 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)

Installation

You can install the TursoHTTP library using Composer:

composer require darkterminal/turso-http
Enter fullscreen mode Exit fullscreen mode

Usage Example

use Darkterminal\TursoHTTP
require_once 'vendor/autoload.php';

$databaseName       = "your-database-name";
$organizationName   = "your-organization-name";
$token              = "your-turso-database-token";

$tursoAPI = new TursoHTTP($databaseName, $organizationName, $token);
$query = new SadQuery();

$createTableUsers = $query->
Enter fullscreen mode Exit fullscreen mode

And who dares to integrate it with fck-htmx with turso-http? Let me now in the comment section!

Top comments (1)

Collapse
 
sonyarianto profile image
Sony AK

this is nice :)