CollectionRecord

数据表中的一条记录。

类型

1interface CollectionRecordOptions<DataType = {}, ParentDataType = {}> {
2  isNew?: boolean;
3  data?: DataType;
4  parentRecord?: CollectionRecord<ParentDataType>;
5  /**
6   * 当前记录所属的 collection name
7   */
8  collectionName?: string;
9}
10
11class CollectionRecord<DataType = {}, ParentDataType = {}> {
12  public isNew?: boolean;
13  public data?: DataType;
14  public parentRecord?: CollectionRecord<ParentDataType>;
15  public collectionName?: string;
16  constructor(options: CollectionRecordOptions<DataType, ParentDataType>) {}
17
18  setData(data: DataType) {
19    this.data = data;
20  }
21
22  setParentRecord(parentRecord: CollectionRecord<ParentDataType>) {
23    this.parentRecord = parentRecord;
24  }
25}

详解

CollectionRecord 基本概念

CollectionRecord 类用于提供数据记录,通常情况下对应着后端数据表中的一条记录。以用户表为例,其一条数据对应的 CollectionRecord 类如下:

1const useCollectionRecord = new CollectionRecord({
2  data: {
3    "id": 1,
4    "roleId": 10,
5    "appLang": null,
6    "createdById": null,
7    "email": "[email protected]",
8    "nickname": "Admin",
9    "phone": null,
10    "systemSettings": {},
11    "updatedById": null,
12    "username": "tachybase",
13    "createdAt": "2023-12-04T09:42:52.953Z",
14    "updatedAt": "2023-12-04T09:42:52.953Z",
15  }
16});

CollectionRecord 和 Collection 的关系

CollectionRecord 是指的数据,而 Collection 则是表结构。对于上面的用户表,其对应的 Collection 如下:

1const usersCollection = new Collection({
2  name: 'users',
3  fields: [
4    {
5      type: 'bigInt',
6      name: 'id',
7    },
8    {
9      type: 'string',
10      name: 'username',
11    },
12    {
13      type: 'integer',
14      name: 'age',
15    },
16    {
17      "name": "email",
18      "type": "string",
19    },
20    // ....
21  ],
22});

父子关系和关系字段

对于关系字段,例如用户和角色的关系,在用户表中会有一个 roleId 字段,其值为角色表中的 id,当我们通过 users.roleId 字段查询用户的角色时:

1GET /api/users/1/roles:get/10

其中 1 为用户的 id10 为角色的 id,我们可以得到用户的角色数据:

1const roleRecord = new CollectionRecord({
2  data: {
3    "id": 10,
4    "name": "member",
5    "title": "test role",
6    "strategy": {
7      "actions": [
8        "view",
9        "update:own",
10        "destroy:own",
11        "create"
12      ]
13    },
14    "createdAt": "2023-03-30T07:53:10.924Z",
15    "updatedAt": "2023-12-15T02:51:43.577Z",
16  }
17})

其中 users id 为 1 的记录我们称之为父记录:

1roleRecord.setParentRecord(userRecord);

新记录

对于新表单,我们可以通过 isNew 属性来标识:

1const record = new CollectionRecord({
2  isNew: true,
3});

示例

基本使用

1import { CollectionRecord } from '@tachybase/client';
2
3const record = new CollectionRecord({
4  data: {
5    name: 'foo',
6  }
7});

创建空记录

1import { CollectionRecord } from '@tachybase/client';
2
3const record = new CollectionRecord({
4  isNew: true,
5});

设置 parentRecord

方式1: 通过构造函数设置

1const parentRecord = new CollectionRecord({
2  data: {
3    foo: 'foo',
4  }
5});
6
7const record = new CollectionRecord({
8  data: {
9    name: 'bar',
10  },
11  parentRecord,
12});

方式2: 通过 setParentRecord 方法设置

1const parentRecord = new CollectionRecord({
2  data: {
3    foo: 'foo',
4  }
5});
6
7const record = new CollectionRecord({
8  data: {
9    name: 'bar',
10  }
11});
12
13record.setParentRecord(parentRecord);