Skip to content

Laravel SDK for Litebase, an open source distributed database built on SQLite and backed by distributed file systems and object storage

License

Notifications You must be signed in to change notification settings

litebase/litebase-laravel

Litebase Laravel SDK (Alpha)

tests GitHub License

A Laravel database driver for Litebase, an open source distributed database built on SQLite, distributed file systems, and object storage.

Installation

You can install the package via composer:

composer require litebase/litebase-laravel

The service provider will be automatically registered.

Configuration

Add a Litebase connection to your config/database.php:

'connections' => [
    // ... other connections
    
    'litebase' => [
        'driver' => 'litebase',
        'database' => env('LITEBASE_DATABASE', 'your_database/main'),
        'host' => env('LITEBASE_HOST', 'localhost'),
        'port' => env('LITEBASE_PORT', '8888'),
        'access_key_id' => env('LITEBASE_ACCESS_KEY_ID'),
        'access_key_secret' => env('LITEBASE_ACCESS_KEY_SECRET'),
    ],
],

Add the corresponding environment variables to your .env:

LITEBASE_HOST=localhost
LITEBASE_PORT=8888
LITEBASE_ACCESS_KEY_ID=lbakid_**********
LITEBASE_ACCESS_KEY_SECRET=lbaks_**********
LITEBASE_DATABASE=your_database/main

Usage

Once configured, you can use Litebase like any other Laravel database connection:

Query Builder

use Illuminate\Support\Facades\DB;

// Select
$users = DB::connection('litebase')
    ->table('users')
    ->where('active', true)
    ->get();

// Insert
DB::connection('litebase')
    ->table('users')
    ->insert([
        'name' => 'John Doe',
        'email' => 'john@example.com',
    ]);

// Update
DB::connection('litebase')
    ->table('users')
    ->where('id', 1)
    ->update(['name' => 'Jane Doe']);

// Delete
DB::connection('litebase')
    ->table('users')
    ->where('id', 1)
    ->delete();

Eloquent Models

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $connection = 'litebase';
    protected $table = 'users';
}

// Use the model
$user = User::create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
]);

$users = User::where('active', true)->get();

Migrations

Use Laravel's migration system as usual:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    protected $connection = 'litebase';
    
    public function up()
    {
        Schema::connection('litebase')->create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->boolean('active')->default(true);
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::connection('litebase')->dropIfExists('users');
    }
};

Run migrations:

php artisan migrate --database=litebase

Schema Operations

use Illuminate\Support\Facades\Schema;

// Check if table exists
if (Schema::connection('litebase')->hasTable('users')) {
    // ...
}

// Get all tables
$tables = Schema::connection('litebase')->getTables();

// Get table columns
$columns = Schema::connection('litebase')->getColumns('users');

Transactions

use Illuminate\Support\Facades\DB;

DB::connection('litebase')->transaction(function () {
    DB::connection('litebase')
        ->table('users')
        ->insert(['name' => 'John Doe', 'email' => 'john@example.com']);
    
    DB::connection('litebase')
        ->table('logs')
        ->insert(['action' => 'user_created']);
});

Interactive Database Shell

The package includes an interactive database shell command:

php artisan litebase:db [connection?]

This provides an interactive SQL prompt where you can execute queries directly against your Litebase database.

Contributing

Please see CONTRIBUTING for details.

Testing

Run unit tests:

composer test

Run integration tests (requires Docker):

composer test-integration

Integration tests require a running Litebase Server. When running integration tests, a server will be automatically started using Docker.

Run static analysis:

composer phpstan

Run code style checks:

composer pint

Code of Conduct

Please see Code of Conduct for details.

Security

All security related issues should be reported directly to security@litebase.com.

License

Litebase is open-sourced software licensed under the MIT License.

About

Laravel SDK for Litebase, an open source distributed database built on SQLite and backed by distributed file systems and object storage

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •  

Languages