섬 사이의 연결 과 비용을 포함한 배열을 바탕으로 모든 섬을 연결하는 최소 소요 비용을 구하는 문제.
앞선 시도로 인해 다음 두가지 가능성에 대해 고민하게 되었다.
특정 노드에서 연결되는 다리의 수는 한개 이상의 가능성을 가진다.
특정 노드에서 최소 값으로 연결되는 섬이 늘 최소 건설 비용을 돌려주지 않는다.
이 가능에서 따라 코드를 수정 해야 했지만 아쉽게도 적절한 구조의 코드를 완성하지 못하였다. 좀더 고민하여 진행해봐야겠다.
사용할 데이터베이스 설정과 단순한 엔티티 하나 구성하여 적용. 실제 사용가능 한 수준으로 저굥ㅇ하는 것은 좀더 현재 버전의 typeorm 에 익숙해진뒤에 진행할 예정이다.
ormconfig.ts 파일에서 dataSource를 사용하는 방식으로 변경
entity 구성 방식 이전과 동일.
이전의 entity 사용방식에는 entity 구성 migration 이후 사용하였던 경험이 있으나 현재 고민되는 것은 migration 이후 진행한 것이 sequelize 를 사용하면서 나온 결과물일뿐 해당 과정 없이도 사용 가능했던 것은 아닌가 하는 점이다. 그게 맞다면 현재 엔티티 구성후 바로 사용가능한 점이 새로운 것이 아니고 스스로가 알지 못했기에 나온 문제이기때문이다.
여하튼 현재의 방식에서 간단하게 user, comment 관계를 구성하고 데미데이터를 만들어 검색하는 과정에 대한 예시를 작성하였다.
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
import { Comments } from "./Comments";
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
firstName: string;
@Column()
lastName: string;
@Column()
age: number;
@OneToMany((type) => Comments, (comments) => comments.user)
comments: Comments[];
}
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from "typeorm";
import { User } from "./User";
@Entity()
export class Comments {
@PrimaryGeneratedColumn()
id: number;
@ManyToOne((type) => User, (user) => user.comments)
user: User;
@Column()
comments: string;
}
이후 dataSource로 불러온 database를 초기화 하는 도중에 더미데이터를 추가하고
const user1 = user.create({
id: 1,
firstName: "Timber",
lastName: "Saw",
age: 27,
});
await user.save(user1).then(console.log).catch(console.log);
const user2 = user.create({
id: 2,
firstName: "Phantom",
lastName: "Assassin",
age: 24,
});
await user.save(user2).then(console.log).catch(console.log);
const commentsAll = comment.create([
{
id: 1,
comments: "abc",
user: user1,
},
{
id: 2,
comments: "ccccc",
user: user2,
},
{
id: 3,
comments: "abcss",
user: user1,
},
{
id: 4,
comments: "aaaaa",
user: user2,
},
{
id: 5,
comments: "sdfse",
user: user1,
},
]);
await comment.save(commentsAll).then(console.log).catch(console.log);
해당 데이터를 다음과 같은 방식으로 검색 해보았다.
let c1 = await comment.find({ where: { user: { id: 1 } } });
console.log(c1);
let c2 = await user.findOne({
relations: { comments: true },
where: { id: 1 },
});
console.log(c2);
첫번째는 유저 아이디가 1인 코멘트를 검색하는것 , 두번째는 유저 아이디 1번의 정보를 불러오되 코멘트 정보까지 불러오는 방식이다.
결과는 다음과 같다.
마이그레이션없이 구성된 엔티티를 적용하여 다대1관계 1대다 관계 구성및 사용이 잘되는것을 확인하였다.