-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relations and referential integrity in NoSQL databases #2127
Comments
@bajtos thank you for starting the discussion around NoSQL backend support for relations; I get the gist of your proposal and since we're not using the LB3 relation engine, it's logical to introduce different flavours of relations for NoSQL DBs. |
I do like to add though that the bulk of the issues that arose with MongoDB and relations in LB4 have to do with the |
The
|
See my #1718 (comment). I think we need to look at the bigger picture first. |
Based on @raymondfeng's comment above and the discussion we had elsewhere, I am proposing the following:
I am going to convert this GH issue into an Epic. |
Converting back into issue in favor of a newly created epic #2331 |
Great summary. +1. |
Moved these steps into a new issue: Document hasMany/belongsTo/hasOne limitations for NoSQL databases #2340
See #2341
Created the following spike stories:
I feel the discussion is over and since we have a list of small actionable tasks to follow up, I am closing this GH issue as done. |
Our current implementation of model relations (has-many, has-one, belongs-to) is deeply rooted in SQL and based on the assumption that the database take care of referential integrity for us.
Example 1: "Customer has many Order instances" and "Order belongs to Customer". When creating a new Order instance, we expect the database to verify that
Order.customerId
is matching the id value of an existing Customer record. We don't have any reliable & atomic way to do this check at LoopBack side.Example 2: "Customer has one Credentials instance". When creating a new Credentials instance, we expect the database to verify that there are no other Credentials instances already created for the user. We don't have any reliable & atomic way to do this check at LoopBack side.
SQL databases provide FOREIGN KEY and UNIQUE constraints that work great for this flavor of relations.
The situation becomes more tricky when we try to map this approach to NoSQL databases. Many NoSQL databases do not provide FOREIGN KEY and UNIQUE constraints, this is often a constraint caused by CAP theorem.
For example, it's not possible to enforce UNIQUE constraint when the model data is stored in multiple physical machines and a network partition occurs (a node performing a write operation is not able to reach other nodes because of networking problems, and thus is cannot verify that the new value is not violating uniqueness constraint for records stored on those nodes).
I think we should rethink the way how we are modelling relations and offer different flavors optimized for different backends.
For example, instead of storing a foreign key in the target model, we can store id of related model(s) in the source model and use optimistic locking scheme to enforce the constraints
We can even store the related models as embedded documents, this should work great for Document databases.
Related issues & discussions:
@strongloop/loopback-next @strongloop/loopback-maintainers thoughts?
The text was updated successfully, but these errors were encountered: