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);

永続化された属性

通常の属性に加えて、いわゆる「永続化された属性」も定義できます。これらの属性はモデルの作成時点では利用できず、データベースに保存され、処理され、APIから返された後に初めて利用可能になります。

デフォルトの永続化された属性はidcreated_atupdated_atdeleted_atですが、モデル定義の2番目のジェネリックパラメータをオーバーライドすることで、別の属性を指定できます。

// 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/posts配下にAPIリソースがある場合、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の場合や、より明示的にしたい場合は、モデル定義の4番目のジェネリックパラメータをオーバーライドします。

// 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();

リソースの作成(store)

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();

完全削除(force delete)

リソースを完全削除するには、deleteメソッドの第2引数を指定します。

const deletedPost = await Post.$query().delete(5, true);
// または
await post.$destroy(true);