moleculer-db
moleculer-db copied to clipboard
Relations in moleculer-sequelize?
I am just wondering how moleculer-sequelize adapter implements the relations for Sequelize?
As I understand the moleculer-db module, it only takes care of the CRUD operations to the DB, and everything else is taken care of in the service level (relationships). Is this true? if not, is there a way that this is handled by the adapter?
Any updates on this? :)
Hey guys.
At the moment, moleculer-db
only supports one model per service. This design works well if you are using a NoSQL database, especially Document database, because you can easily nest all child entities. However, for SQL databases things get tricky because you can have multiple and complex relations between the entities/tables. Due to this, its difficult (with the current workforce) to create a solution that will work for everyone. Therefore, for scenarios with multiple entities and relationships you will have to write your own adapter.
Do you think that i possible to provide an example as guideline? Otherways any realworld implementation with SQL database is very difficult.
In example I'm thinking about a simple user service it should handle at least:
- Users
- Roles
- Authorizations
I don't have big experienced with microservices paradigm but i think all this models and controllers should belongs to a single service with entities correctly structured with foreign keys relations.
In a microservices architecture application, you should define the boundaries. So I think the Users & Roles are two separated entity with relation. But you should solve the relation in the application side and not in DB (in case of microservices application)
If you need a user profile, you should ask it from the users
service.
If you need the roles of a user, you should ask it from the Roles
services like roles.getRolesByUserId
For authorization you pass the userID to the Authorizations
service which gets the roles of the given user from the Roles
service...etc.
I'm a little worried about all this fragmentation but I will make some test for sure. Thanks a lot for your opinion
Alternative with one pool connection in middleware https://gist.github.com/intech/0618869763b4bf176d53b70b26633408
@icebob @AndreMaz I strongly disagree, I believe that this is an imposed architecture pattern and it is not applicable for modern databases that are designed for distributed services, since they provide distributed transactions on their side, it does not make sense on the moleculer side.
There is consensus in the databases and there is an algorithm for reaching consensus between the consistency of the nodes of the binary transaction log and the centralized time source.
Moleculer has to fulfill its task - distributed execution of business logic with simple scaling. It should not be responsible for the consensus of data in the database.