castore-dev / castore

Making Event Sourcing easy 😎
MIT License
232 stars 19 forks source link

"Building your own storage adapter" info? #159

Open thearchduke opened 1 year ago

thearchduke commented 1 year ago

I've been working on a system that will be using postgres for our event database. However, the page for how to build your own adapter seems to have been deleted. Is the information in the linked commit outdated? It's still listed when you search the https://castore-dev.github.io/ docs, but then gives a 404. Not sure the right way to proceed here.

Thanks for the help!

dvxam commented 1 year ago

Hello there!

First and foremost, I want to extend my heartfelt congratulations to all the contributors behind this outstanding library. It truly stands as a remarkable tool.

Regarding the issue at hand, I've been working on crafting an EventStorage adapter for EventStoreDB, and I'm also considering creating event bus and queue adapters down the line.

To answer your question @thearchduke , up to this point, I've been shaping my adapter's behavior by drawing inspiration from existing implementations and unit tests for the DynamoDB and InMemory adapters. However, I suspect I might be overlooking some undocumented features, which has prompted a few questions, targeted to this project's main contributors (@ThomasAribart it seems given source history :) ):

I plan to open source the adapter, would you be open to include it in this codebase?

thearchduke commented 1 year ago

Regarding the issue at hand, I've been working on crafting an EventStorage adapter for EventStoreDB, and I'm also considering creating event bus and queue adapters down the line.

I wrote an (I'm sure hackish) implementation of a redis-backed command queue using bullmq that wasn't the hardest thing in the world. I also ended up building a whole edifice around using commands in a different way, which I'm not sure I'm able to open source, but would probably be a valuable snippet if nothing else. I'll check about that.

ThomasAribart commented 1 year ago

@thearchduke Indeed, good catch, the page has disappeared when we migrated the docs! Will re-publish it next week, thanks for the heads up 👍

Meanwhile, you can follow the implementation based on DynamoDB. The TypeScript interface does 90% of the job. The only small catch is that the adapter should throw an error that implements the EventAlreadyExists error when, well, attempting to write an event that already exists (without the force: true option). This allows for commands to be retried down the line in case of race condition: https://castore-dev.github.io/castore/docs/event-sourcing/pushing-events/

@dvxam thanks for the feedback! Always appreciated 🤗

Of course, I would be happy to include the EventStoreDB adapter into Castore!

Regarding the initialEventAfter, you are right. It is mainly there as a way to paginate the listAggregateIds query, which could otherwise return problematically large amounts of data. This method is exposed but not really used internally, except for the pourEventStoreCollectionEvents util in dam which is thought of as a long running task anyway. Potential solutions:

Regarding pushEventGroup, that's a bummer. Potential solutions:

Last thought: The immutability of EventStoreDB makes the force: true option of the pushEvent method unimplementable. I don't see any option there other than throwing a clear error if anyone uses it (but they will probably already be aware of that anyway).