TypeScript SDK
모델
모델 리소스를 다루는 방법을 알아봅니다.
속성
속성 정의
모델을 생성할 때는 모델 정의에 해당 모델의 속성을 지정해야 합니다.
// post.ts
import {Model} from "@tailflow/laravel-orion/lib/model";
export class Post extends Model<{
title: string,
body: string
}>
{
}
속성 접근
API에서 모델을 조회하면 모델 객체의 $attributes 프로퍼티를 통해 속성에 접근할 수 있습니다.
const post = await Post.$query().find(5);
console.log(post.$attributes.title);
console.log(post.$attributes.body);
저장된 속성
일반 속성과 함께 이른바 "저장된 속성(persisted attributes)"도 정의할 수 있습니다. 이 속성들은 모델 생성 시점에는 사용할 수 없고, 모델이 데이터베이스에 저장되고 처리되어 API가 반환한 후에만 사용할 수 있습니다.
기본 저장된 속성은 id, created_at, updated_at, deleted_at이지만, 모델 정의의 두 번째 제네릭 파라미터를 오버라이드하여 다른 속성을 지정할 수 있습니다.
// post.ts
import {Model} from "@tailflow/laravel-orion/lib/model";
export class Post extends Model<{
title: string,
body: string
}, {
id: number,
thumbnail_url: string
}>
{
}
소프트 삭제
모델이 소프트 삭제 가능하다면 DefaultSoftDeletablePersistedAttributes 타입을 사용할 수 있습니다. 이 타입은 다른 기본 저장된 속성과 함께 deleted_at 속성을 추가해 줍니다.
리소스 이름
API 리소스가 /api/posts 아래에 있다면, posts가 모델에 지정해야 하는 리소스 이름입니다.
// post.ts
import {Model} from "@tailflow/laravel-orion/lib/model";
export class Post extends Model<{
title: string,
body: string
}>
{
public $resource(): string {
return 'posts';
}
}
기본 키
기본 키 값 가져오기와 설정하기
const post = await Post.$query().find(5);
post.$setKey(4);
console.log(post.$getKey());
console.log(post.$attributes.id);
기본 키 커스터마이징
기본적으로 id 속성이 모델의 기본 키로 간주됩니다. 그러나 다른 속성을 기본 키로 사용하도록 지정할 수도 있습니다.
// post.ts
import {Model} from "@tailflow/laravel-orion/lib/model";
export class Post extends Model<{
slug: string,
title: string,
body: string,
}>
{
protected $keyName: string = 'slug';
}
기본 키 타입 커스터마이징
기본적으로 기본 키의 타입은 number | string입니다. 기본 키가 string이거나 타입을 더 명시적으로 지정하고 싶다면, 모델 정의의 네 번째 제네릭 파라미터를 오버라이드하세요.
// post.ts
import {Model} from "@tailflow/laravel-orion/lib/model";
export class Post extends Model<{
slug: string,
title: string,
body: string,
}, {}, {}, string>
{
protected $keyName: string = 'slug';
}
작업
리소스 목록 조회
const posts = await Post.$query().get();
리소스 검색
키워드 기반 검색
const posts = await Post.$query().lookFor('some value').search();
스코프
const posts = await Post.$query().scope('published', [Date.now()]).search();
필터
const posts = await Post.$query().filter('meta.source_id', FilterOperator.Equal, 'test-source').search();
정렬
const posts = await Post.$query().sortBy('published_at', SortDirection.Desc).search();
메서드를 체이닝하여 복잡한 검색 쿼리를 구성할 수 있습니다.
const posts = await Post.$query()
.lookFor('some value')
.scope('published', [Date.now()])
.sortBy('published_at', SortDirection.Desc)
.search();
리소스 생성
const newPost = await Post.$query().store({
title: 'New post'
});
리소스 조회
const post = await Post.$query().find(5);
리소스 수정
let post = await Post.$query().find(5);
post.$attributes.title = 'Updated post';
await post.$save();
// 또는
await post.$save({title: 'Updated post'});
// 또는
const updatedPost = await Post.$query().update(5, {
title: 'Updated title'
});
리소스 삭제
const deletedPost = await Post.$query().delete(5);
// 또는
await post.$destroy();
강제 삭제
리소스를 강제 삭제하려면 delete 메서드에 두 번째 인자를 전달하세요.
const deletedPost = await Post.$query().delete(5, true);
// 또는
await post.$destroy(true);