Warning: we are using typeorm 0.3 and postgres for this tutorial
This tutorial continues where we left in
And let’s fill in the rest of generated boilerplate by nest-cli.
The Read of CRUD
Find queries are pretty straightforward:
The Update of CRUD
Define update DTO first, note that it can leverage PartialType
of the create DTO. For detailed tutorial of PartialType
and OmitType
check official doc at https://docs.nestjs.com/openapi/mapped-types
For update mutation, for a minimal version we can just return the count of affected rows:
Alternatively, we may want to return the updated User from the update mutation, the code may not look elegant but it works:
Since we are using snake case naming strategy for postgres to keep it consistent with postgres standard we need to map the object names back to camel case.
Here returning('*')
enforces the user entity to be returned.
The Delete of CRUD
Similarly if we just want to return affected Rows count:
To return the deleted users entities + affected rows count together we will have another in-depth tutorial on that in the future.