-
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): rework *ById methods to throw if id not found #1723
Conversation
9bffc20
to
ef8b9f1
Compare
ef8b9f1
to
08a8553
Compare
Rework Repository interface and implementations to throw EntityNotFound error from `updateById`, `replaceById` and `deleteById` when there is no entity with the provided id. Update related controller methods to return 204 No Content on success, because there is no boolean flag to indicate success anymore. Clients should check the response status code instead (204 vs 404). This also reverts commit f68a45c.
08a8553
to
c5888ee
Compare
const idProp = this.modelClass.definition.idName(); | ||
const where = {} as Where; | ||
where[idProp] = id; | ||
return this.updateAll(data, where, options).then(count => count > 0); | ||
const count = await this.updateAll(data, where, options); |
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 return Oops never mind. I just realize we returned a boolean instead of the count
anymore?count
itself.
@@ -216,11 +221,11 @@ export class DefaultCrudRepository<T extends Entity, ID> | |||
return this.toEntity(model); | |||
} | |||
|
|||
update(entity: T, options?: Options): Promise<boolean> { | |||
update(entity: T, options?: Options): Promise<void> { |
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 it should call
return await this.updateById(entity.getId(), entity, options)
same for the next function delete
.
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.
It's not necessary.
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.
return await
is actually slightly less performant than return
because of promise re-wrapping.
See http://exploringjs.com/es2016-es2017/ch_async-functions.html#_returned-promises-are-not-wrapped
return this.updateById(entity.getId(), entity, options); | ||
} | ||
|
||
delete(entity: T, options?: Options): Promise<boolean> { | ||
delete(entity: T, options?: Options): Promise<void> { |
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.
return await this.deleteById(entity.getId(), options);
This is a follow-up for #1658 related to #1469.
Rework Repository interface and implementations to throw EntityNotFound error from
updateById
,replaceById
anddeleteById
when there is no entity with the provided id.Update related controller methods to return 204 No Content on success, because there is no boolean flag to indicate success anymore. Clients should check the response status code instead (204 vs 404).
Improve our REST layer to set the response status code to 204 when body is undefined.
This patch is reverting the implementation details of #1621 while keeping the observed REST API behavior.
See the discussion in #1658 and #1658 (comment) in particular to better understand the reasoning for this change. Also note that soon we will add a new Repository implementation that will provide
*ById
variant using a a special return value to indicate "not found" case - see #1735.Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated