TypeScript SDK
연관관계
모델 연관관계 리소스를 다루는 방법을 알아봅니다.
연관관계를 정의하는 방법은 모델 속성을 정의하는 방법과 유사하지만, 몇 가지 차이점이 있습니다. 아래 예제를 살펴보겠습니다.
정의
// post.ts
import {Model} from "@tailflow/laravel-orion/lib/model";
import {BelongsTo} from "@tailflow/laravel-orion/lib/drivers/default/relations/belongsTo";
import {HasMany} from "@tailflow/laravel-orion/lib/drivers/default/relations/hasMany";
import {User} from "./user";
import {Comment} from "./comment";
export class Post extends Model<{
title: string,
body: string
}, {
thumbnail_url: string
}, {
user: User,
comments: Array<Comment>
}> {
public user(): BelongsTo<User> {
return new BelongsTo(User, this);
}
public comments(): HasMany<Comment> {
return new HasMany(Comment, this);
}
}
- 모델 정의에 세 번째 제네릭 파라미터로 연관관계와 해당 모델 타입의 목록을 제공합니다.
- 각 프로퍼티(연관관계)에 대한 메서드를 만들고, 연관관계 유형과 모델에 맞는 연관관계 쿼리 빌더의 새 인스턴스를 반환합니다.
TypeScript SDK는 모든 연관관계 유형을 지원합니다 ✨
다대다
다대다 연관관계를 정의할 때, 피벗 테이블에 추가 필드가 있다면 선택적으로 피벗 모델 타입을 제공할 수 있습니다.
// post.ts
import {Model} from "@tailflow/laravel-orion/lib/model";
import {BelongsToMany} from "@tailflow/laravel-orion/lib/drivers/default/relations/belongsToMany";
import {Tag} from "./tag";
export class Post extends Model<{
title: string,
body: string
}, {
thumbnail_url: string
}, {
tags: Array<Tag>,
}> {
public tags(): BelongsToMany<Tag, {
pivot_field: string
}> {
return new BelongsToMany(Tag, this);
}
}
CRUD 및 검색
연관관계 리소스에 대한 CRUD 및 검색 작업은 기본적으로 모델 리소스에 대한 작업과 동일합니다.
연관관계 리소스 전용 쿼리 빌더 인스턴스를 얻으려면, 모델 인스턴스에서 연관관계 메서드를 호출하기만 하면 됩니다.
const post = Post.$query().find(5);
const comments = post.comments().get(); // ID가 5인 게시글의 댓글 목록을 조회합니다
const newComment = post.comments().store({body: 'Test comment'});
// 기타 등등
일대다
리소스 연관 설정
const post = Post.$query().find(5);
post.comments().associate(7);
리소스 연관 해제
const post = Post.$query().find(5);
post.comments().dissociate(7);
다대다
리소스 연결
const post = Post.$query().find(5);
post.tags().attach([2, 5, 7]);
// 또는
post.tags().attachWithFields({
2: {pivot_field: 'test value'},
5: {pivot_field: 'another value'},
7: {}
});
중복을 허용하는 연결
기본적으로 duplicates 쿼리 파라미터는 false로 설정되지만, attach 또는 attachWithFields 메서드에 두 번째 인수를 제공하여 SDK가 이를 true로 설정하도록 지시할 수 있습니다.
const post = Post.$query().find(5);
post.tags().attach([2, 5, 7], true);
리소스 연결 해제
const post = Post.$query().find(5);
post.tags().detach([2, 5, 7]);
// 또는
post.tags().detachWithFields({
2: {pivot_field: 'test value'},
5: {pivot_field: 'another value'},
7: {}
});
리소스 동기화
const post = Post.$query().find(5);
post.tags().sync([2, 5, 7]);
// 또는
post.tags().syncWithFields({
2: {pivot_field: 'test value'},
5: {pivot_field: 'another value'},
7: {}
});
연결 해제 없는 동기화
기본적으로 detaching 쿼리 파라미터는 true로 설정되지만, sync 또는 syncWithFields 메서드에 두 번째 인수를 제공하여 SDK가 이를 false로 설정하도록 지시할 수 있습니다.
const post = Post.$query().find(5);
post.tags().sync([2, 5, 7], false);
리소스 토글
const post = Post.$query().find(5);
post.tags().toggle([2, 5, 7]);
// 또는
post.tags().toggleWithFields({
2: {pivot_field: 'test value'},
5: {pivot_field: 'another value'},
7: {}
});
피벗 업데이트
const post = Post.$query().find(5);
post.tags().updatePivot(2, {pivot_field: 'test value'});
post.tags().updatePivot(5, {pivot_field: 'test value'});
post.tags().updatePivot(7, {pivot_field: 'test value'});