Order
Let's begin by creating a resource for Order
.
We'll create the basic strucutre for Order
, beginning with the enum OrderStatus
. Let's do this by creating the file orders/enums/order-status.enum with following content:
Now, in the order.entity file, define the attributes.
Then, execute the necessary steps to finish assembling the new resource:
In the controller, use the
IdDto
whereverid
is being usedRemove the number conversion operator (
+
) below each occurrenceUse
PaginationDto
in thefindAll()
method (and also in the service)Default page size - 5
Add to the entity the ORM decorators (
@Entity()
,@Column()
, etc)Register the entity inside the
TypeOrmModule
in the resource's own module
However, the enum is not automatically mapped to the database just with the @Column()
decorator. We need to explicitly define the enum in its options, like this:
We'll now deal with the matter of relationships. One user may have many orders, and one order belongs to one user. We can see that this is a one-to-many relationship. So, let's begin on the user's side. In the user.entity file, we'll define an attribute for his orders
and use the @OneToMany()
decorator to map this relationship.
Now, let's deal with the order's side, defining the attribute for its customer
and using @ManyToOne()
.
Let's now generate and run the migration create-order through the process already known to us.
As a last step, let's make sure that, when a user is searched by id, his orders are also fetched. To do this, go back to the UsersService
and, in the findOne()
method, alter it like the following:
Commit - Creating order entity, using enum and handling one to many relation
Last updated