Code Monkey home page Code Monkey logo

turso-http's Introduction

TursoHTTP

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.

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)

Features

Installation

You can install the TursoHTTP library using Composer:

composer require darkterminal/turso-http

Setting Up Timezone

Set the database display timezome in your env variable. See the list of timezones here

DB_TIMEZOME=Asia/Jakarta

Usage Example

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

LibSQL Schema Builder

<?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();
}

Raw Query

<?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'
]);

Query Builder

<?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);

Turso Platform API - PHP

Manage databases, replicas, and teams with the Turso Platform API using PHP.

License

This library is licensed under the MIT License - see the LICENSE file for details.

turso-http's People

Contributors

darkterminal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

turso-http's Issues

Support Platform API and Sync

Feature Roadmaps:

  • Wrap Platform API using PHP ๐Ÿ v1.2.0
  • Trying to implement sync in TursoHTPP ๐Ÿ turso-syncd

Even though the community's experience has not yet found the right method, why not try to create a sync method that follows my experience?

And I gave up on creating a native extension using C for PHP. When I tried using a lot of guided methods and others, I couldn't learn quickly and already had a lot of activities.

Class "Darkterminal\TursoHTTP" not found

I am using Codeigniter 4 and using composer I have installed it. Now when i open the page it shows "Class "Darkterminal\TursoHTTP" not found"

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

class AdminController extends BaseController
{
      public function test(){
        $databaseName       = "dbtest";
        $organizationName   = "org";
        $token              = "12345";

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

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.