envor/laravel-datastore
Composer 安装命令:
composer require envor/laravel-datastore
包简介
On Prem Laravel
README 文档
README
A simple strategy for handling dynamic databases of varying types
Upgrade Guide
Upgrading from 1.x to 2.x.
2x does not push middleware by default, or configure context by default. To continue using as before add the following to your project's .env file:
DATASTORE_PUSH_CONTEXT_MIDDLEWARE=true AUTOCONFIGURE_DEFAULT_CONTEXT=true
2x does not require envor/platform by default, Next you will need to manually require envor/platform
composer require envor/platform
Installation
You can install the package via composer:
composer require envor/laravel-datastore
You can publish and run the migrations with:
php artisan vendor:publish --tag="datastore-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --tag="datastore-config"
This is the contents of the published config file:
return [ 'model' => \Envor\Datastore\Models\Datastore::class, 'create_databases' => env('DATASTORE_CREATE_DATABASES', true), 'push_middleware' => env('DATASTORE_PUSH_CONTEXT_MIDDLEWARE', false), 'autoconfigure_default_context' => env('AUTOCONFIGURE_DEFAULT_CONTEXT', false), ];
Usage
You actually don't need to use the model or even run migrations. You can use the factory directly like so:
use Envor\Datastore\DatabaseFactory; $sqlite = DatabaseFactory::newDatabase('mydb', 'sqlite'); // Envor\Datastore\Databases\SQLite {#2841 ... $sqlite->create(); // true $sqlite->name; // ...storage/app/datastore/mydb.sqlite $sqlite->connection; // mydb $sqlite->migrate(); // INFO Preparing database. // Creating migration table ................ 9.55ms DONE $sqlite->configure(); config('database.default'); // "mydb" config('database.connections.mydb'); // [ // "driver" => "sqlite", // "url" => null, // "database" => "...storage/app/datastore/mydb.sqlite", // "prefix" => "", // "foreign_key_constraints" => true, // "name" => "mydb", // ]
/** * Create a newly registered user. * * @param array<string, string> $input */ public function create(array $input): User { $create = function () use ($input) { Validator::make($input, [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => $this->passwordRules(), 'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '', ])->validate(); return DB::transaction(function () use ($input) { return tap(User::create([ 'name' => $input['name'], 'email' => $input['email'], 'password' => Hash::make($input['password']), ]), function (User $user) { $this->createTeam($user); }); }); }; SQLite::make(database_path('my_backup.sqlite')) ->create() ->migratePath('database/migrations/platform') ->migrate() ->run($create) ->disconnect(); MariaDB::make('backup') ->create() ->migratePath('database/migrations/platform') ->migrate() ->run($create) ->disconnect(); return MariaDB::make('datastore') ->create() ->migratePath('database/migrations/platform') ->migrate() ->run($create) ->return(); }
Middleware
You can use the 'datastore.context' middleware to get the app to behave in the context of the current datastore;
Route::get('/contexed', fn() => 'OK')->middleware('datastore.context'); // or Route::get('/contexed', fn() => 'OK')->middleware(\Envor\Datastore\DatastoreContextMiddleware::class); // will use the authenticated user to configure a database
Your user must implement the HasDatastoreContext interface in order for this to work.
... use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable implements \Envor\Datastore\Contracts\HasDatastoreContex { use \Envor\Datastore\Concerns\BelongsToDatastore public function datastoreContext(): \Envor\Datastore\Contracts\ConfiguresDatastore; { return $this->datastore; } }
Here are the relevant interfaces:
interface HasDatastoreContext { public function datastoreContext(): ?\Envor\Datastore\Contracts\ConfiguresDatastore; } interface ConfiguresDatastore { public function configure(); public function use(); public function database(): ?\Envor\Datastore\Datastore; }
Testing
composer test
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.
envor/laravel-datastore 适用场景与选型建议
envor/laravel-datastore 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.84k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「envor」 「laravel-datastore」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 envor/laravel-datastore 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 envor/laravel-datastore 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 envor/laravel-datastore 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Some helpful (database level) macros for laravel's schema builder. Requires Laravel 11.
This is my package platform
Database per team starter kit for Laravel based on Laravel Jetstream and Livewire
Alfabank REST API integration
This is my package libstream
A small package for managing multiple databases and their connections at runtime using laravel tools.
统计信息
- 总下载量: 5.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-15