-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(repository): implement belongsTo relation #1794
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
Conversation
|
@raymondfeng @b-admike I am not able to make the following test pass right now: Source code: const todo = givenTodo({todoListId: undefined});
const response = await client
.post(`/todo-lists/${persistedTodoList.id}/todos`)
.send(todo)
.expect(200);The situation is as follows:
I'd like to ask you for your opinion on how to address this problem. I see two options:
Thoughts? |
|
@raymondfeng @b-admike I just realized another problem. Notice that in some tests, we are applying but in other places we apply the decorator on the target model getter property, e.g. The second approach have two problems:
I think we should unify the code in this pull request to use only one of this approaches; and I am leaning towards decorating the foreign key property. Thoughts? |
Thanks for explaining the issue in detail @bajtos. My vote is for not marking the foreign key as a required property and to let the database check its presence. But I guess that may not be true for all the databases we support (not sure if referential integrity is enforced in NoSQL dbs). I am okay with the other approach if there is strong reason to do so, but I see this as a temporary issue since it'll be resolved in #1179. |
I was not aware of the places where we use the |
The ref integrity has to be enforced at the client level, the document based DB has no rules for this at the DB Engine level. |
I filled a TypeScript issue for this. IMO, the compiler should report compile-time error if it cannot emit code that works. See microsoft/TypeScript#27519 |
|
Thank you for the comments. I am going to make the following changes:
When you say "client level", do you mean our ORM layer ( Ideally, I'd like LoopBack to enforce referential integrity inside our ORM layer ( When a model is created via a relational API, e.g. Are you ok with proceeding with the currently proposed solution and add LoopBack-level checks of foreign key values later? |
d407ea3 to
3d3bd9e
Compare
|
@raymondfeng @b-admike @jannyHou @marioestradarosa the pull request is done and ready for the final review. PTAL. I am leaving documentation out of scope of this pull request in order to get it lander sooner. I'll open a new PR to document this new relation type. |
raymondfeng
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
b-admike
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work and thank you for adding TSDocs / fixing up existing test suites 🎉
| required: true, | ||
| // TODO(bajtos) Make the foreign key required once our REST API layer | ||
| // allows controller methods to exclude required properties | ||
| // required: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the required: false line meant to be commented (I guess it doesn't make a difference since AFAIK it's false by default)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I'll keep the commented line to make it clear what is TODO, but change it to required: true.
| expect(jugglerMeta).to.eql({ | ||
| addressBookId: { | ||
| type: Number, | ||
| required: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is required: true set by juggler because this is the only property in the model def?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, required: true used to be set by @belongsTo decorator before I changed this behavior based on our discussion.
|
|
||
| before(givenCrudRepositories); | ||
| before(givenAccessor); | ||
| afterEach(async function resetRepositories() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont we want to clean up the repos in the before() handler so that we can inspect the records after if tests fail and to start with clean repository instances?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! I was copying this code from elsewhere, will fix it.
| import {expect} from '@loopback/testlab'; | ||
| import {Application} from '@loopback/core'; | ||
|
|
||
| describe('HasMany relation', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HasMany relation -> belongsTo relation
Improve the API documentation for `TypeResolver` type to leverage tsdoc's `@template` directive instead of using Markdown formatting.
3d3bd9e to
d87a8d7
Compare
d87a8d7 to
98af096
Compare
Implement missing pieces for belongsTo relation:
This pull request is another step on my journey to land #1618 in several smaller chunks.
This pull request is related to #1361.
Checklist
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated