waad/truffle
Composer 安装命令:
composer require waad/truffle
包简介
Laravel package for in-memory driver database Eloquent model connections
README 文档
README
Waad/Truffle
Eloquent models backed by in-memory SQLite. Perfect for static data, reference tables, and config that doesn't belong in your database.
Zero config • Full Eloquent API • CSV / JSON / XML support • Per-model caching • Optional file-based SQLite
Installation
composer require waad/truffle
Quick Start
use Illuminate\Database\Eloquent\Model; use Waad\Truffle\Truffle; class Product extends Model { use Truffle; protected $fillable = ['name', 'price', 'category']; protected $casts = ['price' => 'float']; protected $records = [ ['id' => 1, 'name' => 'Laptop', 'price' => 999.99, 'category' => 'Electronics'], ['id' => 2, 'name' => 'Coffee Mug', 'price' => 12.50, 'category' => 'Kitchen'], ]; } Product::all(); Product::where('category', 'Electronics')->first(); Product::avg('price');
Schema
Define column types explicitly with DataType. If omitted, types are inferred from your data.
use Waad\Truffle\Enums\DataType; protected $schema = [ 'id' => DataType::Id, 'name' => DataType::String, 'price' => DataType::Decimal, 'active'=> DataType::Boolean, ];
All DataType values
| DataType | DB Type |
|---|---|
Id |
INTEGER (PK, auto-increment) |
String |
VARCHAR(255) |
Text |
TEXT |
Integer |
INTEGER |
BigInteger |
BIGINT |
UnsignedBigInteger |
UNSIGNED BIGINT |
Float |
FLOAT |
Double |
DOUBLE |
Decimal |
DECIMAL |
Boolean |
BOOLEAN |
Json / Jsonb |
TEXT |
Date |
DATE |
DateTime |
DATETIME |
Time |
TIME |
Timestamp |
TIMESTAMP |
Uuid |
CHAR(36) |
Ulid |
CHAR(26) |
Dynamic Records
Override getRecords() to generate data at runtime:
public function getRecords(): array { return collect(range(1, 100))->map(fn ($i) => [ 'id' => $i, 'name' => "User {$i}", ])->toArray(); }
File-Based Records
Load records from CSV, JSON, or XML files. Format is auto-detected from the extension.
// Via property (auto-detected) protected $truffleFile = __DIR__ . '/../data/countries.csv'; // Or via getRecords() public function getRecords(): array { return $this->fromCsvFile(__DIR__ . '/../data/countries.csv'); // return $this->fromJsonFile(__DIR__ . '/../data/products.json'); // return $this->fromXmlFile(__DIR__ . '/../data/categories.xml', 'category'); }
CSV custom delimiters are supported via $truffleFileDelimiter, $truffleFileEnclosure, and $truffleFileEscape properties.
See full examples:
CsvModel.php,JsonModel.php,XmlModel.php
Caching
Enable per-model caching to avoid rebuilding the SQLite table on every request:
protected $truffleCache = true; protected $truffleCacheTtl = 3600; // seconds (null = forever) // protected $truffleCacheDriver = 'redis'; // protected $truffleCachePrefix = 'app_';
Model::clearTruffleCache(); // clear cached records Model::refreshTruffleCache(); // clear + rebuild
See full example:
CachedModel.php
SQLite File Storage
Persist to a SQLite file instead of in-memory. Ideal for large datasets:
protected static $truffleSqliteFile = '/path/to/database.sqlite';
Model::deleteTruffleSqliteFile(); // delete the file Model::refreshTruffleSqliteFile(); // delete + rebuild
See full example:
SqliteFileModel.php
Performance Tuning
protected $insertChunkRecords = 500; // batch insert size protected $foreignKeyConstraints = true; // enable FK constraints protected function thenMigration(Blueprint $table) { $table->index('name'); }
Validation
Works with Laravel's exists rule:
'category_id' => ['required', Rule::exists(Category::class, 'id')],
Examples
See the examples/ directory for complete, runnable models:
| Example | Description |
|---|---|
BasicModel.php |
Inline records with scopes |
CountryModel.php |
Non-incrementing string primary key |
DynamicRecordsModel.php |
Generated records via getRecords() |
CachedModel.php |
Caching with TTL and custom driver |
SqliteFileModel.php |
File-based SQLite persistence |
CsvModel.php |
Load data from CSV |
JsonModel.php |
Load data from JSON |
XmlModel.php |
Load data from XML |
TruffleExample.php |
Full-featured model with all options |
Testing
composer test
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Add tests and make your changes
- Run tests:
composer test - Submit a Pull Request
Roadmap
- Eloquent integration
- SQLite in-memory support
- SQLite file support
- Caching support
- Support for CSV/JSON/XML files
- Multi-tenancy support
Credits
Built with love for the Laravel community by Waad Mawlood
waad/truffle 适用场景与选型建议
waad/truffle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 286 次下载、GitHub Stars 达 13, 最近一次更新时间为 2025 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 waad/truffle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 waad/truffle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 286
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-05
