Skip to content

A LibSQL Driver for Doctrine DBAL with Native libSQL

Notifications You must be signed in to change notification settings

tursodatabase/turso-doctrine-dbal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Turso + TypeScript

Turso + Doctrine DBAL

SQLite for Production. Powered by libSQL and libSQL Extension for PHP.

Turso · Quickstart · Examples · Docs · Discord · Blog & Tutorials


A LibSQL Driver for Doctrine DBAL

Shows a black logo in light color mode and a white one in dark color mode.

LibSQL is a fork of SQLite and this package is #1 LibSQL Driver that run natively using LibSQL Native Extension/Driver/Whatever and support Doctrine DBAL.

Requirement

Before using this package, you need to install and configure LibSQL Native Extension for PHP. You can download from LibSQL Extension - Release

  1. 📦 Extract the archive
  2. 🗃 Locate somewhere in your machine
  3. 💽 Copy a relative path that address that extension/driver
  4. 📂 Open php.ini search ;extension if you using nano (ctrl+w) then searching for it
  5. 📝 add in the next-line extension=liblibsql_php.so (in Linux) without ; at the begining

Package Installation

composer require tursodatabase/turso-doctrine-dbal

Configuration

LibSQL has 4 types of connections to interact with the database: In-Memory Connection, Local Connection, Remote Connection, and Remote Replica Connection (Embedded Replica).

In-Memory Connection

$params = [
    "url"               => ":memory:",
    'driverClass'       => \Turso\Doctrine\DBAL\Driver::class,
];

Local Connection

$params = [
    "url"               => "database.db",
    'driverClass'       => \Turso\Doctrine\DBAL\Driver::class,
];

If you want to create Remote Connection or Remote Replica (Embedded Replica) Connection, you will need an existing database to continue. If you don’t have one, create one.

Get the database URL:

turso db show --url <database-name>

Get the database authentication token:

turso db tokens create <database-name>

If you use .env file, assign credentials to the environment variables inside .env.

TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=

Remote Connection

$params = [
    "auth_token"        => "<your-database-auth-token-from-turso>",
    "sync_url"          => "<your-database-url-from-turso>",
    'driverClass'       => \Turso\Doctrine\DBAL\Driver::class,
];

Remote Replica (Embedded Replica) Connection

$params = [
    "url"               => "database.db",
    "auth_token"        => "<your-database-auth-token-from-turso>",
    "sync_url"          => "<your-database-url-from-turso>",
    "sync_interval"     => 5,    // Optional, default is: 5 in seconds
    "read_your_writes"  => true, // Optional, default is: true
    "encryption_key"    => "",   // Optional, default is: empty
    'driverClass'       => \Turso\Doctrine\DBAL\Driver::class,
];

Usage

use Doctrine\DBAL\DriverManager;

require_once __DIR__ . '/vendor/autoload.php';

$params = [
    "url"               => ":memory:",
    'driverClass'       => \Turso\Doctrine\DBAL\Driver::class,
];

$db = DriverManager::getConnection($params);

$createTable = "CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    age INTEGER
)";
$db->executeStatement($createTable);

$insertUsers = <<<SQL
INSERT INTO users (name, age) VALUES ('Budi Dalton', 49);
INSERT INTO users (name, age) VALUES ('Sujiwo Tedjo', 50);
SQL;

$db->getNativeConnection()->executeBatch($insertUsers);

$result = $db->executeQuery("SELECT * FROM users")->fetchAllAssociative();
var_dump($result);
$db->close();

Documentation

  1. Turso Quickstart — Learn how create and connect your first database.
  2. SDK Quickstart — Learn how to install and execute queries using the libSQL client.
  3. SDK Reference — Dive deeper with the libSQL SDK reference and examples.

What is Turso?

Turso is a SQLite-compatible database built on libSQL, the Open Contribution fork of SQLite. It enables scaling to hundreds of thousands of databases per organization and supports replication to any location, including your own servers, for microsecond-latency access.

Learn more about what you can do with Turso:

About

A LibSQL Driver for Doctrine DBAL with Native libSQL

Resources

Stars

Watchers

Forks

Sponsor this project

 

Languages