maslauskas/event-store
Composer 安装命令:
composer require maslauskas/event-store
包简介
Simple event store for Laravel using MySQL
README 文档
README
Simple EventStore implementation package for Laravel using MySQL.
Installation
To start using this package, install it with composer:
composer require maslauskas/event-store
Publishing config and migrations:
php artisan vendor:publish --provider=Maslauskas\EventStore\EventStoreServiceProvider
This package uses Laravel's package auto-discovery feature, so there is no need to modify your config/app.php file.
Configuration
Event Store logs are saved to your main database by default, but it is recommended to use a dedicated MySQL database for it. Once you create the database, make sure to set Event Store to use it:
First, add a dedicated connection to your config/database.php file:
'connections' => [ /* ... */ 'eventstore' => [ 'driver' => 'mysql', 'host' => env('EVENT_STORE_HOST', 'localhost'), 'port' => env('EVENT_STORE_PORT', '3306'), 'database' => env('EVENT_STORE_DATABASE', 'event_store'), 'username' => env('EVENT_STORE_USERNAME', 'root'), 'password' => env('EVENT_STORE_PASSWORD', 'root'), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], /* ... */ ]
Next, add required environment variables to your .env file:
EVENT_STORE_CONNECTION="eventstore" EVENT_STORE_DATABASE="your_event_store_database_name" EVENT_STORE_TABLE="your_event_store_table_name" EVENT_STORE_USERNAME="your_event_store_user_name" EVENT_STORE_PASSWORD="your_event_store_user_password"
It is recommended to create a separate user for event store database, and remove UPDATE, DELETE, DROP permissions, to make sure your event store is append-only.
Next, run the migration to create default event store table:
php artisan migrate
Usage
To start logging your events, append this line to your code where you wish the event to be logged:
EventStore::add('event_name', $data);
Or using the eventstore helper function, which is just a wrapper for the facade:
eventstore()->add('event_name', $data);
the add() method accepts four arguments:
$event_type: name of your event, e.g.user_created,email_sent,order_shipped, etc.$payload: array of values to record. e.g. foruser_createdevent, you can pass the array of attributes that this user was created with.$target_id: (optional) ID of target model in your database. E.g., foremail_sentevent, you can passuser_idas$target_id. This helps in the future when you wish to fetch all events related to a particular user.$before: (optional) array of values that were changed. E.g. foruser_updatedevent, you may pass$user->toArray()to record attributes that were changed and their values before the change. Note: theadd()method automatically filters out only those keys that exist in$payloadparameter to avoid unnecessary overhead.
Sometimes, certain events occur much more frequently than others, e.g. user_created and user_logged_in. To help with query performance, you can separate certain events to their dedicated tables by changing the streams array in config/eventstore.php file:
'streams' => [ 'user_login_stream' => [ 'user_logged_in', ] ]
This will automatically create a dedicated user_login_stream table in your event store database when you try to add user_logged_in event. All events that are not defined in this array will be saved in the default event store table.
Extra methods
query()
Returns Illuminate\Database\Eloquent\Builder instance so you can perform any query on event store tables.
get()
Gets all events from the default event store table. Returns a collection.
get($event_name)
Gets all events of specific type from event store table. Automatically determines which table to search in. Returns a collection.
stream($stream_name)
Sets dedicated table and returns Illuminate\Database\Eloquent\Builder instance so you can perform any query on event store tables.
Exception handling
By default, EventStore suppresses any exceptions that occur during add() method call. You can disable this by changing throw_exceptions setting in config/eventstore.php:
'throw_exceptions' => true,
Testing
Run the tests with
vendor/bin/phpunit
maslauskas/event-store 适用场景与选型建议
maslauskas/event-store 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 905 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 maslauskas/event-store 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 maslauskas/event-store 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 905
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-02