Using an RDBMS as event sourcing storage

前端 未结 6 1685
灰色年华
灰色年华 2020-12-07 07:02

If I were using an RDBMS (e.g. SQL Server) to store event sourcing data, what might the schema look like?

I\'ve seen a few variations talked about in an abstract sens

6条回答
  •  天涯浪人
    2020-12-07 07:47

    The event store should not need to know about the specific fields or properties of events. Otherwise every modification of your model would result in having to migrate your database (just as in good old-fashioned state-based persistence). Therefore I wouldn't recommend option 1 and 2 at all.

    Below is the schema as used in Ncqrs. As you can see, the table "Events" stores the related data as a CLOB (i.e. JSON or XML). This corresponds to your option 3 (Only that there is no "ProductEvents" table because you only need one generic "Events" table. In Ncqrs the mapping to your Aggregate Roots happens through the "EventSources" table, where each EventSource corresponds to an actual Aggregate Root.)

    Table Events:
        Id [uniqueidentifier] NOT NULL,
        TimeStamp [datetime] NOT NULL,
    
        Name [varchar](max) NOT NULL,
        Version [varchar](max) NOT NULL,
    
        EventSourceId [uniqueidentifier] NOT NULL,
        Sequence [bigint], 
    
        Data [nvarchar](max) NOT NULL
    
    Table EventSources:
        Id [uniqueidentifier] NOT NULL, 
        Type [nvarchar](255) NOT NULL, 
        Version [int] NOT NULL
    

    The SQL persistence mechanism of Jonathan Oliver's Event Store implementation consists basically of one table called "Commits" with a BLOB field "Payload". This is pretty much the same as in Ncqrs, only that it serializes the event's properties in binary format (which, for instance, adds encryption support).

    Greg Young recommends a similar approach, as extensively documented on Greg's website.

    The schema of his prototypical "Events" table reads:

    Table Events
        AggregateId [Guid],
        Data [Blob],
        SequenceNumber [Long],
        Version [Int]
    

提交回复
热议问题