Skip to content

DuckDB CLI wrapper to interact with duckdb databases through laravel query builder.

License

Notifications You must be signed in to change notification settings

harish81/laravel-duckdb

Repository files navigation

DuckDB CLI wrapper to interact with duckdb databases through laravel query builder.

Latest Version on Packagist Total Downloads

https://github.com/duckdb/duckdb

Support us

Installation

You can install the package via composer:

composer require harish81/laravel-duckdb

Usage

  • Connect
'connections' => [
    'my_duckdb' => [
        'driver' => 'duckdb',
        'cli_path' => env('DUCKDB_CLI_PATH', base_path('vendor/bin/duckdb')),
        //'dbfile' => env('DUCKDB_DB_FILE', '/tmp/duck_main.db'),
    ],
...
  • Examples
# Using DB facade
DB::connection('my_duckdb')
    ->table(base_path('genderdata.csv'))
    ->where('Gender', '=', 'M')
    ->limit(10)
    ->get();
# Using Raw queries
DB::connection('my_duckdb')
    ->select("select * from '".base_path('genderdata.csv')."' limit 5")
# Using Eloquent Model
class GenderDataModel extends \Harish\LaravelDuckdb\LaravelDuckdbModel
{
    protected $connection = 'my_duckdb';
    public function __construct()
    {
        $this->table = base_path('genderdata.csv');
    }
}
...
GenderDataModel::where('Gender','M')->first()

Advanced Usage

You can install duckdb extensions too.

Query data from s3 files directly.

  • in database.php
'connections' => [
    'my_duckdb' => [
        'driver' => 'duckdb',
        'cli_path' => env('DUCKDB_CLI_PATH', base_path('vendor/bin/duckdb')),
        'cli_timeout' => 0, //0 to disable timeout, default to 1 Minute (60s)
        'dbfile' => env('DUCKDB_DB_FILE', storage_path('app/duckdb/duck_main.db')),
        'pre_queries' => [
            "SET s3_region='".env('AWS_DEFAULT_REGION')."'",
            "SET s3_access_key_id='".env('AWS_ACCESS_KEY_ID')."'",
            "SET s3_secret_access_key='".env('AWS_SECRET_ACCESS_KEY')."'",
        ],
        'extensions' => ['httpfs'],
    ],
    ...
  • Query data
DB::connection('my_duckdb')
  ->select("SELECT * FROM read_csv_auto('s3://my-bucket/test-datasets/example1/us-gender-data-2022.csv') LIMIT 10")

Writing a migration

return new class extends Migration {
    protected $connection = 'my_duckdb';
    public function up(): void
    {
        DB::connection('my_duckdb')->statement('CREATE SEQUENCE people_sequence');
        Schema::create('people', function (Blueprint $table) {
            $table->id()->default(new \Illuminate\Database\Query\Expression("nextval('people_sequence')"));
            $table->string('name');
            $table->integer('age');
            $table->integer('rank');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('people');
        DB::connection('my_duckdb')->statement('DROP SEQUENCE people_sequence');
    }
};

Readonly Connection - A solution to concurrent query.

  • in database.php
    'connections' => [
        'my_duckdb' => [
            'driver' => 'duckdb',
            'cli_path' => env('DUCKDB_CLI_PATH', base_path('vendor/bin/duckdb')),
            'cli_timeout' => 0,
            'dbfile' => env('DUCKDB_DB_FILE', storage_path('app/duckdb/duck_main.db')),
            'schema' => 'main',
            'read_only' => true,
            'pre_queries' => [
                "SET s3_region='".env('AWS_DEFAULT_REGION')."'",
                "SET s3_access_key_id='".env('AWS_ACCESS_KEY_ID')."'",
                "SET s3_secret_access_key='".env('AWS_SECRET_ACCESS_KEY')."'",
            ],
            'extensions' => ['httpfs', 'postgres_scanner'],
        ],
        ...

Testing

  • Generate test data
# Syntax: ./data-generator.sh <lines> <file-to-save.csv>
./data-generator.sh 100 _test-data/test.csv
./data-generator.sh 90000000 _test-data/test_big_file.csv
  • Run Test case
composer test

Limitations & FAQ

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

DuckDB CLI wrapper to interact with duckdb databases through laravel query builder.

Resources

License

Stars

Watchers

Forks

Packages

No packages published