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,但你可以通过覆盖模型定义中的第二个泛型参数来指定不同的属性。

// 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,或者你希望更明确地声明类型,只需覆盖模型定义中的第四个泛型参数。

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