libSQL Extension for PHP Officially Landed at Turso!
Tags: php, programming, laravel, database
Published: 2024-06-11 13:31:26

Hello Punk! Yes, I am again.

I've very busy dancing day and messing up something until I got confused (_what's going on me?!_) I don't know!

Writing bunch of paper and try to implement one by one in disorder direction (_yes, it's a tech-ni-que!_) to create abstract art that make me overwhelming-confused. BTW, I just tired!

tub, I need to show you some _**art**_istic piece of punk by me (off course!), cz I am too handsome to do anything while dancing.

---

## Turso Database

![Turso Database](https://mintlify.s3-us-west-1.amazonaws.com/turso/images/turso-banner.png)

### What the punk is turso?!

Nice question! Thank you very much... 

_[**Turso**](https://docs.turso.tech/introduction) is a SQLite-compatible database built on [**libSQL**](https://docs.turso.tech/libsql), the <mark>Open Contribution fork of SQLite</mark>. It enables scaling to hundreds of thousands of <mark>databases per organization</mark> and supports <mark>replication to any location</mark>, including <mark>your own servers</mark>, for <mark>microsecond-latency</mark> access. - [turso.tech](https://docs.turso.tech/introduction)_

Have I just promoted Turso on my previous 3 or 4 blogs?! Hmm... damn it!

tub, never-mind! I just proud by myself found amazing playground and be the first person to build Native Extension/Driver/Whatever for PHP that support all features of Turso.

## SQLite for Production 

What even mean by that? It's stupid!

Na na na na... hold on, let me tell you.

_A database is often referred to as a “**logical database**” because it consists of multiple libSQL databases._

_<mark>Each database</mark> is equipped with its <mark>own schema and data</mark>, situated in a primary location along <mark>with possibly several replica locations</mark> within its designated group. This setup ensures that the schema and data are <mark>seamlessly copied from the primary location to all replica</mark> sites in the **[group](https://docs.turso.tech/concepts#groups)**._

_Databases are <mark>identified by a distinct libSQL URL</mark>. When accessed through the libSQL client SDKs, this URL <mark>directs the client to the database</mark> instance that <mark>offers the lowest latency</mark>, thereby reducing the overall <mark>duration of read operations.<mark>_

The point I just highlight for you, this is what I called _it's is amazing place to create chaos as I can!_

And you can [read by your-self](https://docs.turso.tech/concepts) to proof **_deznutt_**.

## Client SDKs

Lorsque je regarde le flux VimMoustache et parle de la base de données Turso, je consulte le site, je lis l'introduction et tous les concepts et fonctionnalités qu'ils ont et proposent, c'est.....

My first Impression, ThePunk! Where is PHP SDK!? Aaaaaaahhh... I am mad! tub, still handsome.

Then I search through Internet, call Vivaldi to play [**Campursai Music**](https://en.wikipedia.org/wiki/Campursari), then make a "quack" sound from the 🦆 that said: "Turso for PHP", "libSQL for PHP" it's basic search.


![Turso for PHP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ok3ladqxfmdbyuwj2k7.png)

![libSQL for PHP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/059eb7rmixggizxokvnd.png)

See.. that's me! I search the queries using [browserling](https://www.browserling.com/) for fairness, btw.

Now, I am polute the Internet.

tub, before I messing up internet, they come up with nice and amazing marterpiece that represent the power of Turso. Almost all of them is implement HTTP SDK to interact with database and natively talk using SQLite3, sync using cronjob.

Then... my freestyler soul starting to stir and want to start something extraordinary with my chaos dance as a Software Freestyle Engineer (BTW).

### #1 Song to Dance

{% embed https://www.youtube.com/embed/_K62GKx-qvQ?si=mRcbA3_YnMxIaF8n %}

I started by gathering the energy to dance by reading the documentation, comments on the source code, and exploring the [libSQL](https://github.com/tursodatabase/libsql) repository on GitHub.

**I am good as an artist**

So, I will impersonate `libsql-client-ts` in PHP land. _Yes, call me impostor for now!_

You know, explore new things for me is like mountaineering, enter the forest that has not been touched much and find a way to be able to reach the top or at least be able to return home to meet the family.

I create [Turso-HTTP](https://github.com/darkterminal/turso-http) using [HTTP SDK](https://docs.turso.tech/sdk/http/quickstart). My first thought; this is really amazing, SQLite as Rest API?! WhatThePunk!

_I am still dance with this **#1 Song** in 8 hours building in a loop!_

After create [Turso-HTTP](https://github.com/darkterminal/turso-http), I think something missing here...

Ah! The Punk, yes! The `sync` method, tub wait... _how can I implement this method in PHP when I didn't have libSQL binary?_

That question in my handsome brain, is kept coming up.

How about using `Loop::addPeriodicTimer` from `ReactPHP` and manage sync between local and remote with queue manager in JSON file with timestamp?!

Yes, I did something like this when I sell Instagram Followers, Likes, and Comments 9 years ago using [mgp25/instagram-api](https://github.com/mgp25/Instagram-API), how wonderful that day!.

Nevermind, go back to the topic fish!

Yes, I did it! I create [Turso-Syncd](https://github.com/darkterminal/turso-syncd) to leverage Turso Background Sync!

The final touch is, I support Turso-HTTP with [Platform API](https://docs.turso.tech/api-reference/introduction)

---

### #2 Song to Dance

{% embed https://www.youtube.com/embed/ekHJTF20FA8?si=dhuVX1Bxsce5pMoK %}

**Handsome Brain Still Thinking**

I need implement all features and I need to embed the libSQL Rust Binary into PHP, tub how?

Learn Rust or C damn it!

Yes! I need to learn Rust with some motivation that come from [Aaron Francis](https://aaronfrancis.com/podcast/php-doesnt-suck-anymore-yfnsj0) podcast and YouTube Video [PHP doesn't suck (anymore)](https://www.youtube.com/watch?v=ZRV3pBuPxEQ)

Why not!

_Learning Rust is the same as I will become **rust** that can stick to sturdy scrap metal._

I learn Rust! Cz libSQL build using Rust, so I can embed libSQL into my PHP Extension using crate/framework [ext-php-rs](https://github.com/davidcole1340/ext-php-rs) made by [
David Cole](https://github.com/davidcole1340) this is cool crate/framework that can make people create their own PHP Extension using Rust!

---

Tub, I am struggle with this crate/framework in the first-time. So I decide to create using extension with On-Demand Compilation tech-ni-que "this sound stupid" using Rust Cbindgen, so I force developer to build this extension from Source via Composer it's called [libsql-php-ext](https://github.com/darkterminal/libsql-php-ext)!

So, when you run this command:
```bash
composer require darkterminal/libsql-php-ext
```

They will install:

- C/C++ Compiler
- jq
- Rust Installed
- PHP Installed
- FFI Extension is Enabled (Why? I read the C heder definition from wrapper)

Then create PHP Abstraction to wrap the API in PHP using PHP FFI. It's too complicated like Tech's nowaday 😁, I named [libSQL Client PHP](https://github.com/darkterminal/libsql-client-php) and this is the #1 libSQL PHP binary that talk natively with libSQL driver!

And I think this is **_too forced and less effective_** for working in a PHP environment because developers have to build it from source with various tool preparations. _Why not make it a binary file that can be downloaded and used immediately like a PHP extension in general?!_

---

### #3 Song to Dance

{% embed https://www.youtube.com/embed/yvuj09xmwRo?si=kLmisT4GaSgYX8Di %}

**Handsome Dance Move**

Now I'm starting to get familiar with the `ext-php-rs` crate/framework to start creating libSQL extensions for PHP after a few tries.

_Like penetrating a wild jungle in the mountains without a compass and without experience, yes, this was indeed my first journey dancing in a new language sung and accompanied by rusty old iron music._

After <1 Month! I create **libSQL Native Extension for PHP** that support all Turso features! And distribute the extension via GitHub Action!

There is no other song, tub if you want to hear more about Campursari Song Playlist while creating the libSQL Native Extension for PHP [Turso for PHP - Building Playlists](https://www.youtube.com/playlist?list=PLZaIaMXfdrYw5NE13ek2G2tR4T4JswCvr)

## Native BTW

![libSQL Native Extension for PHP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7p2jam8lensqytgyomp6.png)

One-time installation for your PHP Environment and use it anywhere in your PHP project!

I build and make it feel like using SQLite3 in PHP but powered by more type of database connections!

- In-Memory Connection
- Local Connection
- Remote Connection ✅
- Embedded Replica ✅

This this the core features of Turso/libSQL and it's now available for PHP!

The Extension Repository:

- [Turso Client PHP](https://github.com/tursodatabase/turso-client-php) - Turso - libSQL client libraries and utilities for PHP _formerly [libSQL Client PHP](https://github.com/darkterminal/libsql-client-php)_

The Other PHP Driver Repostories:

- [Turso Driver Laravel](https://github.com/tursodatabase/turso-driver-laravel) - Turso driver for Laravel
- [Turso Doctrine DBAL](https://github.com/tursodatabase/turso-doctrine-dbal) - A LibSQL Driver for Doctrine DBAL

## Officially Landed

![The landing page for PHP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jyrcrzo96t0qd5ov0bf.png)

The landing page for PHP at Turso: [https://turso.tech/php](https://turso.tech/php)

## Made in Java

This is amazing joke from CEO of Turso Database! Don't get angry because of this, THIS IS A JOKE!

![Made in Java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3laz0nkxn0jbx4kwkefz.png)

Source: [Tweet from Glauber Costa - Founder/CEO at Turso Database](https://x.com/glcst/status/1791128033677463890)

---

You can support my open-source activity at GitHub and support me to keep build something that make me proud of myself!

{% cta https://github.com/sponsors/darkterminal %} Become a Sponsor {% endcta %}