typeorm EntityMetadata

cornpip·2023년 6월 11일
0

nestjs

목록 보기
1/1
post-thumbnail

typeorm EntityMetadata

import { EntityMetadata } from 'typeorm';

const repo: EntityMetadata = this.markdownrepo.metadata;
const columnNames = repo.columns.map(column => column.propertyName);
console.log(columnNames);

(1)
entity의 컬럼명이 들어있는 array를 얻을 수 있다.

UpdatePostDto에서 tags는 별도 처리가 필요한데 if i=="tags" 같이 직접 string으로 해두는건 수정에 좋지않으니까 일단 column명을 찾아봤는데 잘 모르겠다."tags"는 상수 파일로 두는게 최선같기도?

적용하면 어떻게 적용했는지 올리겠다.


올림

  async update(id: number, updatePostDto: UpdatePostDto) {
    //이미지 수정은 나중에
    const mdpost = await this.markdownrepo.findOne({
      relations: {
        tags: true,
      },
      where: { id: id }
    })

    if (updatePostDto.tags) {
      const arr_tag_repo = await this.createTags(updatePostDto.tags);
      mdpost.tags = arr_tag_repo;
    }

    if (updatePostDto.feature_title) mdpost.featureTitle = updatePostDto.feature_title;
    if (updatePostDto.sub_title) mdpost.subTitle = updatePostDto.sub_title;
    if (updatePostDto.sub_title) mdpost.content = updatePostDto.content;

    this.markdownrepo.save(mdpost);
    return `This action updates a #${id} post`;
  }

this.markdownrepo.update(id, update_object) 이 save와 다르게 update query만 날린다해서 update를 쓰려고 했고 그래서 update_object을 만들면서 위의(1)과 같은 고민을 했는데 many-many delete를 save로 하는 듯해서 그냥 save를 사용했다.

many-many는 매번 통계 query로 tag 쓰이는지 확인하고 지워야하는데 매번 하는건 별로인 것 같다. 이런류는 나중에 일정 시간마다 쿼리도는 로직에 있는게 좋겠다.

profile
https://cornpip.tistory.com 티스토리로 이전했습니다!

0개의 댓글

Powered by GraphCDN, the GraphQL CDN