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

Added message type to allow store not only events but also commands and unfied message processing #183

Merged
merged 1 commit into from
Feb 27, 2025

Conversation

oskardudycz
Copy link
Collaborator

@oskardudycz oskardudycz commented Feb 8, 2025

This is the first step in enabling message storage and workflows.

It adds a Message type that's either a command or an event. I added an additional property called kind, which will inform you what the message actually is (so if it's an event or command).

The next step is to adjust the stores to keep that information. I will also rename tables and columns to have event in their names instead of message (e.g., message ID, message type, etc.). MongoDB event store is already adjusted to some degree to that. Eventually, event stores will become message stores. Of course, I want to keep the abstractions as we have, but probably as aliases. See this in follow up PR: #184

Thanks to that, Emmett will be able to store and then handle async workflows as described in https://event-driven.io/en/how_to_have_fun_with_typescript_and_workflow/. Commands will be stored either in the stream together with events or in a dedicated "outgoing commands stream".

Thanks to having mentioned earlier kind when building the state, we'll be able to just read the events.

As an alternative to kind I was considering renaming type property in Command and Event types to commandType and eventType. Then we could benefit from duck typing, but I didn't like that fully, as in general both Commands and Events are just Messages, so that'd be a bit too expressive imho.

Of course, the kind will not be required to be provided in the regular code. It will probably only be used in workflows to distinguish if we're sending command or storing event.

It will always be available upon reading. As it'll be stored.

data: { mocked: true },
metadata: {
streamName: 'testStream',
eventId: `event-${position}`,
messageId: `event-${position}`,
streamPosition: position,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should "event" from value be changed to "message" too? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that too, indeed. 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did that ;)

@@ -45,7 +42,7 @@ export type CreateCommandType<
data: CommandData;
metadata: CommandMetaData;
}
>;
> & { readonly kind?: 'Command' };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to be undefined?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I don't want to force Emmett user to pass kind property if it's not needed and can be resolved by context. Typically, it'll be needed when a message can be either a command or an event, e.g. workflow or message handling). That will be typically done on the receiving end (so subscriptions). There, it will be automatically filled upon reading from the store. See: https://github.com/event-driven-io/emmett/pull/183/files#diff-ca381aa5cbc0929ecbb32c004c54bce2d0d6c1ceb3c91d524055888807396559R69

This is the first step to enable message storing and workflows
@oskardudycz oskardudycz added this to the 0.31.0 milestone Feb 27, 2025
@oskardudycz oskardudycz marked this pull request as ready for review February 27, 2025 19:04
@oskardudycz
Copy link
Collaborator Author

I'm not yet convinced whether this will stay like that or I'll go with eventType or commandType. However, this PR does not stop further distinctions but just unifies what we have right now. I'm pulling that in, adding an option to specify message kind explicitly. If it stays longer unmerged, then it may be harder to live with rebases, etc.

Happy to continue discussions on this design in the future.

@oskardudycz oskardudycz merged commit 402afc2 into main Feb 27, 2025
3 checks passed
@oskardudycz oskardudycz deleted the message_type branch February 27, 2025 19:05
@oskardudycz oskardudycz modified the milestones: 0.31.0, 0.32.0 Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants