I don't really find anything in the issues/wiki regarding how to validate and create relations on POSTing a resource.
In my example I have an entity Product which has a One-To-Many relation to a Company (one company has multiple products). When I create a product (POST /products) I want to add a field like company or companyId to the body which contains the Id of the company that it should be linked to. My example below is my current implementation which is not type-safe but validates that the company exists and passes the company id down to TypeORM which then linkes them both together.
@Crud({
model: {
type: Product,
},
routes: {
only: [
"getOneBase",
"replaceOneBase",
"deleteOneBase",
"getManyBase",
"createOneBase",
],
},
params: {
...defaultParamsOptions,
},
dto: {
create: Product,
},
query: {
join: {
// include company in response
company: {
eager: true,
required: true,
},
},
},
})
@ApiTags("Product")
@Controller("products")
export class ProductController implements CrudController<Product> {
constructor(
public service: ProductService,
public companyService: CompanyService,
) {}
get base(): CrudController<Product> {
return this;
}
@Override()
async createOne(
@ParsedRequest() req: CrudRequest,
@ParsedBody() dto: Product,
): Promise<Product> {
// dto.company contains the company id supplied in the request
// it does NOT contain the company object from the DB
// so we check if it exists and if not throw an error
// TypeORM will handle it correctly
const company = await this.companyService.findOne(dto.company);
if (!company) {
throw new NotFoundException("Company not found");
}
return this.base.createOneBase(req, dto);
}
}
The problem is now that in ProductController.createOne() the second parameter is a DeepPartial<Product> and therefor the company property would be of type Company but in my example request in the body the company field would be the Id of the company. Is there any better way to create a relation and verify that the to-be-relation resource is existing?
I don't really find anything in the issues/wiki regarding how to validate and create relations on POSTing a resource.
In my example I have an entity
Product
which has aOne-To-Many
relation to aCompany
(one company has multiple products). When I create a product (POST /products
) I want to add a field likecompany
orcompanyId
to the body which contains the Id of the company that it should be linked to. My example below is my current implementation which is not type-safe but validates that the company exists and passes the company id down to TypeORM which then linkes them both together.product.entity.ts
:company.entity.ts
:product.controller.ts
:Example request:
Response:
The problem is now that in
ProductController.createOne()
the second parameter is aDeepPartial<Product>
and therefor thecompany
property would be of typeCompany
but in my example request in the body thecompany
field would be the Id of the company. Is there any better way to create a relation and verify that the to-be-relation resource is existing?