foundry-co/laravel-snowflake
Composer 安装命令:
composer require foundry-co/laravel-snowflake
包简介
Snowflake database driver for Laravel using REST API - no PDO extension required
README 文档
README
A Laravel database driver for Snowflake using the REST SQL API. No PHP extensions or ODBC drivers required.
Features
- Pure PHP implementation using Snowflake's REST API
- Full Eloquent support with models and relationships
- Laravel Query Builder with Snowflake-specific SQL
- Migrations with Snowflake-specific column types
- ULID primary keys optimized for Snowflake clustering
- Native support for VARIANT, OBJECT, and ARRAY types
- Large result set streaming via partitions
Requirements
- PHP 8.2+
- Laravel 12.0+
- Snowflake account with REST API access
Installation
composer require foundry-co/laravel-snowflake
The package will auto-register its service provider.
Configuration
1. Snowflake Account Setup
Set up key-pair authentication in Snowflake:
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out snowflake_key.p8 -nocrypt
openssl rsa -in snowflake_key.p8 -pubout -out snowflake_key.pub
Assign the public key to your Snowflake user:
ALTER USER your_user SET RSA_PUBLIC_KEY='MIIBIjANBgkqh...';
2. Environment Variables
SNOWFLAKE_ACCOUNT=your-account-identifier SNOWFLAKE_WAREHOUSE=COMPUTE_WH SNOWFLAKE_DATABASE=MY_DATABASE SNOWFLAKE_SCHEMA=PUBLIC SNOWFLAKE_USER=your_username SNOWFLAKE_PRIVATE_KEY_PATH=/path/to/snowflake_key.p8 SNOWFLAKE_ROLE=SYSADMIN
3. Database Configuration
Add the Snowflake connection to config/database.php:
'connections' => [ 'snowflake' => [ 'driver' => 'snowflake', 'account' => env('SNOWFLAKE_ACCOUNT'), 'warehouse' => env('SNOWFLAKE_WAREHOUSE'), 'database' => env('SNOWFLAKE_DATABASE'), 'schema' => env('SNOWFLAKE_SCHEMA', 'PUBLIC'), 'role' => env('SNOWFLAKE_ROLE'), 'auth' => [ 'jwt' => [ 'user' => env('SNOWFLAKE_USER'), 'private_key_path' => env('SNOWFLAKE_PRIVATE_KEY_PATH'), 'private_key_passphrase' => env('SNOWFLAKE_PRIVATE_KEY_PASSPHRASE'), ], ], ], ],
You can also provide the private key content directly instead of a file path:
'auth' => [ 'jwt' => [ 'user' => env('SNOWFLAKE_USER'), 'private_key' => env('SNOWFLAKE_PRIVATE_KEY'), ], ],
Usage
Eloquent Models
Add the UsesSnowflake trait to any model that connects to Snowflake:
use Illuminate\Database\Eloquent\Model; use FoundryCo\Snowflake\Eloquent\Concerns\UsesSnowflake; class User extends Model { use UsesSnowflake; protected $connection = 'snowflake'; protected $table = 'users'; }
The trait automatically generates ULID primary keys and handles Snowflake timestamp formats.
Query Builder
$users = DB::connection('snowflake')->table('users')->get(); DB::connection('snowflake')->table('users')->insert([ 'id' => Str::ulid()->toLower(), 'name' => 'John Doe', 'email' => 'john@example.com', ]); DB::connection('snowflake') ->table('events') ->where('payload->type', 'purchase') ->get();
Migrations
use Illuminate\Database\Migrations\Migration; use FoundryCo\Snowflake\Schema\SnowflakeBlueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { protected $connection = 'snowflake'; public function up(): void { Schema::connection('snowflake')->create('users', function (SnowflakeBlueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->variant('preferences'); $table->timestamps(); $table->clusterBy(['created_at', 'id']); }); } public function down(): void { Schema::connection('snowflake')->dropIfExists('users'); } };
Snowflake Column Types
| Method | Snowflake Type |
|---|---|
id() |
CHAR(26) |
variant() |
VARIANT |
object() |
OBJECT |
array() |
ARRAY |
geography() |
GEOGRAPHY |
geometry() |
GEOMETRY |
timestampNtz() |
TIMESTAMP_NTZ |
timestampLtz() |
TIMESTAMP_LTZ |
timestampTz() |
TIMESTAMP_TZ |
number() |
NUMBER(p,s) |
identity() |
INTEGER IDENTITY |
Custom Casts
use FoundryCo\Snowflake\Casts\VariantCast; use FoundryCo\Snowflake\Casts\SnowflakeTimestamp; class Event extends Model { use UsesSnowflake; protected $connection = 'snowflake'; protected $casts = [ 'payload' => VariantCast::class, 'occurred_at' => SnowflakeTimestamp::class, ]; }
Warehouse & Role Switching
$connection = DB::connection('snowflake'); $connection->useWarehouse('ANALYTICS_WH'); $connection->useRole('ANALYST'); $connection->useSchema('STAGING');
Transactions
DB::connection('snowflake')->transaction(function ($db) { $db->table('accounts')->where('id', 1)->decrement('balance', 100); $db->table('accounts')->where('id', 2)->increment('balance', 100); });
Cursors
foreach (DB::connection('snowflake')->table('events')->cursor() as $event) { // Process one row at a time }
Testing
composer test
Limitations
- No savepoints (Snowflake limitation)
- No row locking (Snowflake is append-only)
- No traditional indexes (use clustering keys instead)
License
MIT License. See LICENSE for details.
foundry-co/laravel-snowflake 适用场景与选型建议
foundry-co/laravel-snowflake 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 400 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「driver」 「laravel」 「eloquent」 「snowflake」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 foundry-co/laravel-snowflake 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 foundry-co/laravel-snowflake 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 foundry-co/laravel-snowflake 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Galera cluster driver for Doctrine
A PHP client driver for the RethinkDB query language (ReQL)
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 400
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-18