Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware and a note on clearing unit of work outside commandservice #396

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions docs/docs/rails-sequent.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ To make use of this feature, best is to put your domain classes under an `app` s
In this case Rails expects your domain class to be called `BankAccount::BankAccountAggregate`.
See the [Rails autoloading and reloading guide](https://guides.rubyonrails.org/autoloading_and_reloading_constants.html) for more details.

1. Add to your `Gemfile`
1) Add to your `Gemfile`

```
gem 'sequent', git: 'https://github.com/zilverline/sequent'
```

2. Run `bundle install`
2) Run `bundle install`

3. Copy the `sequent_schema.rb` file from [https://raw.githubusercontent.com/zilverline/sequent/master/db/sequent_schema.rb](https://raw.githubusercontent.com/zilverline/sequent/master/db/sequent_schema.rb) and put it in your `./db` directory.
3) Copy the `sequent_schema.rb` file from [https://raw.githubusercontent.com/zilverline/sequent/master/db/sequent_schema.rb](https://raw.githubusercontent.com/zilverline/sequent/master/db/sequent_schema.rb) and put it in your `./db` directory.

4. Create `./db/sequent_migrations.rb`. This will contain your `view_schema` migrations.
4) Create `./db/sequent_migrations.rb`. This will contain your `view_schema` migrations.

```ruby
VIEW_SCHEMA_VERSION = 1
Expand All @@ -52,7 +52,7 @@ See the [Rails autoloading and reloading guide](https://guides.rubyonrails.org/a
For a complete overview on how Migrations work in Sequent, check out the [Migrations Guide](/docs/concepts/migrations.html)


5. Add the following snippet to your `Rakefile`
5) Add the following snippet to your `Rakefile`

```ruby
# Sequent requires a `SEQUENT_ENV` environment to be set
Expand Down Expand Up @@ -97,22 +97,22 @@ See the [Rails autoloading and reloading guide](https://guides.rubyonrails.org/a
so running it without `SEQUENT_MIGRATION_SCHEMAS` set will fail.
{: .notice--warning}

6. Ensure your `database.yml` contains the schema_search_path:
6) Ensure your `database.yml` contains the schema_search_path:

```yaml
default:
schema_search_path: <%= ENV['SEQUENT_MIGRATION_SCHEMAS'] || 'public, sequent_schema, view_schema' %>
```

7. Enable eager loading on all environments
7) Enable eager loading on all environments

Sequent internally relies on registries of classes of certain types. For instance it keeps track of all
`AggregateRoot` classes by adding them to a registry when `Sequent::Core::AggregateRoot` is extended.
For this to work properly, all classes must be eager loaded otherwise code depending on this fact might
produce unpredictable results. Set the `config.eager_load` to `true` for all environments
(in production the Rails default is already `true`).

8. Add `./config/initializers/sequent.rb` containing at least:
8) Add `./config/initializers/sequent.rb` containing at least:

```ruby
require_relative '../../db/sequent_migrations'
Expand All @@ -134,7 +134,7 @@ See the [Rails autoloading and reloading guide](https://guides.rubyonrails.org/a
**You must** wrap the sequent initializer code in `Rails.application.reloader.to_prepare` because during
initialization, the autoloading hasn't run yet.

9. Run the following commands to create the `sequent_schema` and `view_schema`
9) Run the following commands to create the `sequent_schema` and `view_schema`

```bash
bundle exec rake sequent:db:create_event_store
Expand All @@ -145,7 +145,22 @@ See the [Rails autoloading and reloading guide](https://guides.rubyonrails.org/a
bundle exec rake sequent:migrate:offline
```

10. Run `rails s`
10) Add the following to application.rb

This step is actually only necessary if you load Aggregates outside the scope
of the Unit Of Work which is automatically started and committed via the `execute_commands` call.
If you for instance load Aggregates inside Controllers or or ActiveJob you have to clear Sequent's Unit Of Work (stored in the Thread.current) yourself.
For the web you can add the following Rack middleware:

```ruby
config.middleware.use Sequent::Util::Web::ClearCache
```

Otherwise you will need to add a call to `Sequent.aggregate_repository.clear!` somewhere yourself.

See using the [AggregateRepository outside the Unit Of Work](concepts/aggregate-repository.html#advanced-usage-outside-the-commandservice-transaction) for more details.

11) Run `rails s`


### Where to put your domain classes
Expand Down
2 changes: 2 additions & 0 deletions integration-specs/rails-app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0

config.middleware.use Sequent::Util::Web::ClearCache

# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
Expand Down
1 change: 1 addition & 0 deletions lib/sequent/util/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require_relative 'timer'
require_relative 'printer'
require_relative 'dry_run'
require_relative 'web/clear_cache'
19 changes: 19 additions & 0 deletions lib/sequent/util/web/clear_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Sequent
module Util
module Web
class ClearCache
def initialize(app)
@app = app
end

def call(env)
@app.call(env)
ensure
Sequent.aggregate_repository.clear!
end
end
end
end
end
Loading