storing aggregates as json in postgresql all in one table
I am trying to decide what the best way of storing my data would be. I want to build a cqrs application where I also apply DDD principles like aggregates in the command side. I started with with storing the aggregates in postgresql the 'normal' (relational) way using spring data jpa and hibernate. I have a domain objects that are pure and dont have any annotations from the framework (spring, jpa, hibernate etc.). And in the infrastructure layer I have *Entity Objects for the aggregates to store the data in postgresql. But I was thinking if it would make sense to just have one table called 'Aggregate' that has an ID, type and data (jsonb) field. ID would be the aggregates Id, type would be an identifier for the aggregate class (e.g. BankAccount, Transaction etc.) and data would be the aggregates data in jsonb form. This way I wouldnt have to create *Entity objects in infrastructure layer with all the annotations and also would simplify the liquibase migrations, since I wouldnt have to create new migration files for every new aggregate implemented. Another alternative would be to create the same tables for each Aggregate, so for BankAccount I would create a table with the fields Id, data (jsonb) and for Transaction a table with Id, data and so on. Which solution would be preferred? The first one is simpler in terms of data migrations, because adding new attributes in aggregates wouldnt change the table because everything is in the data column in jsonb. Also no changes are needed for new aggregates implemented into the system. The second solution needs database migration every time I add a new aggregate, because the table needs to be created. But the the data is more separated each in his own table which could mean better performance in the future when there are thousands of rows in each table instead of millions of rows in a single table?
I am trying to decide what the best way of storing my data would be. I want to build a cqrs application where I also apply DDD principles like aggregates in the command side. I started with with storing the aggregates in postgresql the 'normal' (relational) way using spring data jpa and hibernate.
I have a domain objects that are pure and dont have any annotations from the framework (spring, jpa, hibernate etc.). And in the infrastructure layer I have *Entity Objects for the aggregates to store the data in postgresql. But I was thinking if it would make sense to just have one table called 'Aggregate' that has an ID, type and data (jsonb) field. ID would be the aggregates Id, type would be an identifier for the aggregate class (e.g. BankAccount, Transaction etc.) and data would be the aggregates data in jsonb form. This way I wouldnt have to create *Entity objects in infrastructure layer with all the annotations and also would simplify the liquibase migrations, since I wouldnt have to create new migration files for every new aggregate implemented.
Another alternative would be to create the same tables for each Aggregate, so for BankAccount I would create a table with the fields Id, data (jsonb) and for Transaction a table with Id, data and so on.
Which solution would be preferred? The first one is simpler in terms of data migrations, because adding new attributes in aggregates wouldnt change the table because everything is in the data column in jsonb. Also no changes are needed for new aggregates implemented into the system. The second solution needs database migration every time I add a new aggregate, because the table needs to be created. But the the data is more separated each in his own table which could mean better performance in the future when there are thousands of rows in each table instead of millions of rows in a single table?