Since all of our events will be Rails models, go ahead and create a new /events directory inside app/models. At the top of the chain, we will define Events::BaseEvent where a lot of the event functionality will live. Our events will be built through inheritance. Since we might end up having a lot of User-related events, we’re also including the event_type field on our User events so we can store them all in one user_events table-and easily add more later! These events will be saved to our database, and will be immutable to serve as a permanent log of changes. When our Rails app creates or destroys a User, this will also trigger creating a new Event. event_type: a String to show that this user_event is the ”Destroyed” type.user_id: the target User’s id, used in its belongs_to relationship.payload: a hash containing the id for the User to be flagged as deleted.Events::User::Destroyed - this will record:. event_type: a String to show that this user_event is the ”Created” type.user_id: the created User’s id, used in its belongs_to relationship.payload: a hash containing the name, email, and password params to create the User.Events::User::Created - this will record:.In our event sourcing system, each Event will be a Rails model that stores information about changes to our data. Now, we’re ready to build our event pattern! Great, we can see our route working as expected! In your console, you will see byebug session: # app/controllers/users_controller.rb def create byebug endįire up rails s, and send a POST request to localhost:3000/users/create in Insomnia. You can test out the routes by adding a byebug to the controller action: Testing the create action with byebug and Insomnia Yay, now Insomnia’s ready to go! We’ll just need to fill out the body of our request with a hash once we have our Events created. We’ll make it a POST request, which we’ll user for a create User route:Īnd lastly, we’ll set the target URL to localhost:3000/users/create for testing later: We’ll be using Insomnia Core.Ĭreate a folder for our project, event-sourcing-user-app: Likewise, if you’re not familiar with Insomnia, it’s a tool for sending HTTP requests to test RESTful APIs. Go ahead and hit Connect to in the localhost using its default settings:įrom here, click the localhost button at the top to go to a list of available databases:Īnd now, we should be able to select our development database:Īnd, hurray-there’s our User model, with its four fields: If you’re not familiar with Postico, it’s a a database management tool and viewer for PostgreSQL with a great free trial.ĭownload and install from their website, and open it up. Set up Postico to view our PostgreSQL database Setting up our environment to test our Events We’ll set the database to PostgreSQL with -database=postgresql and skip tests with -skip-test, as we will be adding RSpec manually later. Let’s go ahead and create our new Rails app
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |