-
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
feat(repository): delete operation in hasOne relation and example hasOne #2347
Conversation
implemeted delete new feature on hasone relation. created an example of one to one application into examples folder "fix loopbackio#2233"
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.
Thank you @RaphaelDrai for the pull request.
First of all, please add tests to cover your new delete
method in HasOne repository.
I have mixed feelings about your new one-2-one
example. IIRC, our intention was to add another model to examples/todo-list
, for example "TodoList has one {some new model}". What are your arguments for adding an entirely new example? We must be cautious about maintenance costs - the more example apps we have, the more expensive it is to make changes related to project layout.
@b-admike you were working on HasOne relation in the past, please take a look at this pull request too.
examples/one-2-one/package.json
Outdated
{ | ||
"name": "@loopback/example-one-2-one", | ||
"version": "1.3.1", | ||
"description": "Continuation of the hasOne example using relations in LoopBack 4.", |
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.
The description says "Continuation of the hasOne example" - which example do you mean by "hasOne example"?
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.
Hello @bajtos , thanks you for your comments. I will add shortly the test of the delete operation. Just for information I already completed the PATCH operation as well, and I will commit it on next week. About the one-2-one example: the example implementation is fitting the documentation in LB4 for hasOne (https://loopback.io/doc/en/lb4/hasOne-relation.html) that describes Supplier and Account (by the way same as in LB3). "Todo-List has one" could make confusion with the existing "Todo-List" that implement hasMany relation. In my opinion and from my own experience as loopback new learner, better to have each relation with its own example to ease the learning and fit the documentation.
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.
Makes sense 👍
Let's find a new better example name please. How about @loopback/example-hasOne-relation
or maybe just @loopback/example-hasOne
?
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.
Or maybe @loopback/example-supplier-account
?
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.
Let's use a clearer description. How about "An example showing hasOne relation in LoopBack 4"?
@@ -39,6 +40,14 @@ export interface HasOneRepository<Target extends Entity> { | |||
filter?: Pick<Filter<Target>, Exclude<keyof Filter<Target>, 'where'>>, | |||
options?: Options, | |||
): Promise<Target>; | |||
|
|||
/** | |||
* Delete multiple target model 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.
This does not make sense to me. In HasOne relation, there is always either no target model or one target model. I don't see why would the caller want to delete multiple instances or provide a filter?
IMO, the API should look like this:
delete(options?: Options): Promise<Count>;
I am also not sure whether to return Count
(0
= not found; 1
= deleted), or whether to use Boolean
(false
= not found; true
= deleted). I guess it's good to stay consistent with other delete
methods and return Count
.
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.
I agree hasOne is always either no target model or one target model. The problem is that actually there is no any check to avoid multiple targets as we had in the past. Adding multiple targets check was removed from the initial version that I tested a coupe of months ago. This is the reason why I kept the Count. Let me know if you want me to add the check that avoid to add multiple targets then I will return true or false as per your recommendation.
Do you agree with a separated example one-2-one application as I did?
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.
I agree hasOne is always either no target model or one target model. The problem is that actually there is no any check to avoid multiple targets as we had in the past. Adding multiple targets check was removed from the initial version that I tested a coupe of months ago. This is the reason why I kept the Count.
I think we need to decouple the delete API (what parameters is the method accepting) from the value it returns.
Do we both agree that it does not make sense to provide a filter
or where
argument where deleting the other side of hasOne
relation?
As for the return value, let's keep returning Count
for consistency with other delete
methods.
- @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected] - @loopback/[email protected]
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.
Hi @RaphaelDrai, I think this pull request is a great start 👍
I see three major areas to improve:
- Remove
where
parameter from the repositorydelete
API. - Add tests to cover the new feature.
- Improve the example, add tests.
It may be easier to split these changes into two pull requests: one PR to add delete
method, another PR to add the new example app. If you agree, then please keep this PR for the example app and open a new PR for delete
implementation. That way most of my comments below stay preserved and will be easier to verify.
examples/one-2-one/.gitignore
Outdated
@@ -0,0 +1,64 @@ | |||
# Logs |
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.
We don't keep package-level .gitignore
files and use a top-level .gitignore
file in loopback-next monorepo instead. Please remove this file.
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.
ok
examples/one-2-one/.yo-rc.json
Outdated
@@ -0,0 +1 @@ | |||
{} |
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.
We don't have .yo-rc.json
in other example packages, can you please remove this file too?
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.
ok
examples/one-2-one/package.json
Outdated
{ | ||
"name": "@loopback/example-one-2-one", | ||
"version": "1.3.1", | ||
"description": "Continuation of the hasOne example using relations in LoopBack 4.", |
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.
Let's use a clearer description. How about "An example showing hasOne relation in LoopBack 4"?
examples/one-2-one/README.md
Outdated
@@ -0,0 +1,3 @@ | |||
# one2one |
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.
The README needs to be improved before this patch is landed. See our README guidelines.
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.
I will remove my hasOne example as per your notification below.
examples/one-2-one/package.json
Outdated
"@loopback/repository": "^1.1.1", | ||
"@loopback/rest": "^1.5.1", | ||
"@loopback/rest-explorer": "^1.1.4", | ||
"@loopback/service-proxy": "^1.0.5", |
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.
I think this dependency is not needed by this example app, can you remove it please?
}, | ||
}, | ||
}) | ||
async find( |
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.
Let's call this method get
for consistency with the method name used by the repository.
}) | ||
async delete( | ||
@param.path.string('id') id: string, | ||
@param.query.object('where', getWhereSchemaFor(Account)) where?: Where, |
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.
As commented earlier, I think HasOne's delete
should not accept any where
condition. Let's simplify the REST API too.
export interface AppWithClient { | ||
app: One2oneApplication; | ||
client: Client; | ||
} |
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.
Please add acceptance-level tests for each controller you are adding to this example application:
SupplierController
SupplierAccountController
AccountController
(if you decide to keep it)
examples/one-2-one/tsconfig.json
Outdated
"include": [ | ||
"src", | ||
"test", | ||
"index.ts" |
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.
We have recently reworked our project layout so that test files are kept in src/__tests__
. Please rebase your pull request on top of the latest master and update your new example app to follow the new convention. See the following pull request for the changes you need to make: https://github.com/strongloop/loopback-next/pull/2316/files.
It basically boils down to:
- move
test
tosrc/__tests__
- update
package.json
scripts & files - update
tsconfig.build.json
- fix paths in
import
statements
I apologize for inconvenience I caused here.
@@ -87,4 +96,12 @@ export class DefaultHasOneRepository< | |||
} | |||
return found[0]; | |||
} | |||
|
|||
async delete(where?: Where<TargetEntity>, options?: Options): Promise<Count> { |
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.
Please add tests to cover this new repository method, see packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts
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.
ok
@RaphaelDrai I just found that our Given that we already have an example for HasOne, I am taking back my earlier comments where I said it's ok to create a new example for HasOne-only. Let's keep building on top of the existing example please. |
implement hasone relation operations delete, patch and put "fix loopbackio#2233"
implemeted delete new feature on hasone relation. created an example of one to one application into examples folder "fix loopbackio#2233"
implement hasone relation operations delete, patch and put "fix loopbackio#2233"
…k-next into hasOneDelete
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.
@RaphaelDrai Thank you for the contribution! Your branch contains several commits not related to this PR, it's a little bit hard to find the real code change, could you rebase and fix them?
@RaphaelDrai I see a lot of unrelated commits from our Since most of the review comments were related to the new example app that's no longer part of this pull request, it may be easier for you to start fresh, create a new feature branch and open a new pull request. You can use |
This PR was replaced with : #2414 |
Closing in favor of #2414 |
implemented delete new feature on hasone relation. created an example of one to one application into
examples folder
"fix #2233"
Implemented the DELETE operation in hasOne relation. which is part of the issue #2233.
I have added an example application for the hasOne relation under the folder:
examples/one-2-one
The next step, I will implement the PATCH then the PUT operations new features in the hasOne relation.
Fixes #2233
Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated