定制 maslauskas/event-store 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Build Status Latest Stable Version Total Downloads License

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. for user_created event, 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., for email_sent event, you can pass user_id as $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. for user_updated event, you may pass $user->toArray() to record attributes that were changed and their values before the change. Note: the add() method automatically filters out only those keys that exist in $payload parameter 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 maslauskas/event-store 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 905
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-02