Approaching EventStoreDB with JAVA: The com.eventstore.dbclient.EventData wrapper

Hi everyone,

After reading the docs and approaching to EventSourcing theory, I’m trying to implement a very simple PoC with EventStoreDB and Spring Boot.

My first trouble is using the com.eventstore.dbclient.EventData wrapper class.

Suppose having an Event with this structure:

{
    "id": "a22f823d-1a5s-1432-aed5-032805c8410d",
    "type": "ORDER-PLACED",
    "streamId": "1", // The order-id
    "streamPosition": 1, // The event position in the stream
    "timestamp": "2023-12-05T00:00:12.000Z",
    "body": {
            ....  // The Order Data
    },
    "metadata": 
    {
            .... // The Event Metadata
    }
}

Is it right to pass an event having the given structure (MyEvent in the example below) as eventData of the builderAsJson?

EventData eventData = EventData
            .builderAsJson(
                    THE-EVENT-UUID,
                    THE-EVENT-TYPE,
                    new MyEvent())
            .build();

Or, alternatively, is it better to reduce the Event structure only to the Body?

It seems to me that using the first approach leads to duplicating the event’s data (event-id, event-type, event-metadata) that is passed both directly to the builderAsJson builder and as MyEvent data)

What is the right approach?

Thank you

Reduce the structure to the Body.
Note there is also a Metadata byte array , so you can also move that one out

Question : StreamPosition how would you know the position of an event that has not been appended yet ?
( thinking about concurrent appends here…)

So while all the attribute you describe are necessary, the representation in the database may be different.

Don’t forget that in the case of ESDB , the timestamp is the time it was appended to the database, don’t use that for any business related purpose.
Any Date / time used for business purpose should be in the body & controlled by the system generating the event.

Also for metadata :
try to keep metadata as a simple Key/value list .

 {
            "key1": "Value1", 
            "key2":"value2"
    }

herre some thoughts that might help in what to put there :

It’s good that I came across this topic, recently encountered a similar problem but did not find a solution, thank you.

I’m not sure what you mean by problem, could you expand a little ?

Thank you Ives.

I understand what you mean about the positionStream field. I will deepen this aspect.