The PHP library for Turso HTTP
Tags: php, turso, database, webdev
Published: 2024-01-22 14:30:00

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](https://turso.tech/)** was just born and started to be talked about on the Internet. I started to pay attention a little, especially with **[Turso](https://turso.tech/)** often pronounced by [ThePrimeangen](twitch.tv/ThePrimeagen) (sorry, I wrote this article using Google Translate).

Then I searched about **[Turso](https://turso.tech/)** 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](https://turso.tech/)** 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](https://turso.tech/)** 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
<?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;
```

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

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

{% github https://github.com/darkterminal/turso-http %}

And who dares to integrate it with [fck-htmx](https://github.com/darkterminal/fck-htmx) with [turso-http](https://github.com/darkterminal/turso-http)? Let me now in the comment section!