moleculerjs / moleculer-db

:battery: Database access service mixins for Moleculer
https://moleculer.services/
MIT License
152 stars 123 forks source link

Relations in moleculer-sequelize? #71

Open paulsherwinang opened 5 years ago

paulsherwinang commented 5 years ago

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?

boehlerlukas commented 5 years ago

Any updates on this? :)

AndreMaz commented 5 years ago

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.

luckyguido commented 4 years ago

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:

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.

icebob commented 4 years ago

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.

luckyguido commented 4 years ago

I'm a little worried about all this fragmentation but I will make some test for sure. Thanks a lot for your opinion

intech commented 4 years ago

Alternative with one pool connection in middleware https://gist.github.com/intech/0618869763b4bf176d53b70b26633408

intech commented 4 years ago

@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.