Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I found your boilerplate to be very useful. But, I'm not able to properly add a new entity, with reference to user.
Example, I want to add a new entity (id, name, userId).
Note: userId is foreign reference to user table.
I added new field through your commands given. I got generated files. Now, I want to add user reference.

  1. I updated it in entity like this:
    @manytoone(() => UserEntity, {
    eager: true,
    })
    user?: UserEntity;

  2. Added this in domain as:
    @ApiProperty({
    type: () => User,
    })
    user: User;

  3. Added this in mapper.

  4. But, Im having difficulty in DTOs.

My question is how to do changes in all three DTOs: create-qna.dtp.ts, find-all-qnas.dto.ts, update-qna.dto.ts

Thanks in advance.

You must be logged in to vote

Replies: 2 comments · 3 replies

Comment options

Update: In create-qna.dto.ts, I added
@ApiProperty()
@IsNumber()
@type(() => CreateUserDto)
user: CreateUserDto;

but then error:

src/qnas/qnas.service.ts:13:38 - error TS2345: Argument of type 'CreateqnaDto' is not assignable to parameter of type 'Omit<qna, "id" | "createdAt" | "updatedAt">'.
Types of property 'user' are incompatible.
Type 'CreateUserDto' is missing the following properties from type 'User': id, createdAt, updatedAt, deletedAt

13 return this.qnaRepository.create(createqnaDto);
~~~~~~~~~~~~

[8:07:08 PM] Found 1 error. Watching for file changes.

You must be logged in to vote
1 reply
@goravsingal
Comment options

Any example with reference to user field would be helpful.

Comment options

Your coding conventions are so hard to understand.
I fixed it by following:

My dto
export class CreateqnaDto {
@ApiProperty()
@IsString()
body: string;

@ApiProperty()
@IsString()
title: string;

@ApiProperty({
description: 'The ID of the user who created the QnA',
example: 1,
})
@isnotempty()
@IsNumber()
userId: number;
}

And in service
async create(createQnaDto: CreateqnaDto) {
// Resolve the user from the userId
const user = await this.userRepository.findById(createQnaDto.userId);
if (!user) {
throw new Error('User not found');
}

const clonedObject = {
  ...createQnaDto,
  user: user,
};
return this.qnaRepository.create(clonedObject);

}

You must be logged in to vote
2 replies
@goravsingal
Comment options

A full example, with a new entity and referencing user entity would be good to have.
And, I see there is no property HTTP code, in case GET/{ID} not found.

@Shchepotin
Comment options

You are on the right way.

Also, we are working on a CLI tool to help you create a property as a reference to other entities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.