Collection

Collection 数据表类,其被 CollectionManager 管理。

1. 类型

1interface CollectionOptions {
2  name: string;
3  title?: string;
4  fields?: FieldOptions[];
5  // ....
6}
7
8class Collection {
9  app: Application;
10  collectionManager: CollectionManager;
11
12  constructor(options: CollectionOptions) {}
13
14  name: string;
15  primaryKey: string;
16  titleField: string;
17
18  getOptions(): CollectionOptions;
19  setOptions(options: CollectionOptions): void;
20  getOption<K extends keyof CollectionOptions>(key: K): CollectionOptions[K];
21
22  getFields(predicate?: CollectionFieldOptions | ((collection: CollectionFieldOptions) => boolean) | keyof CollectionFieldOptions): any[]
23  getField(name: SchemaKey): CollectionFieldOptions
24  hasField(name: SchemaKey): boolean;
25}
1const usersCollection = new Collection({
2  name: 'users',
3  title: 'Users',
4  fields: [
5    {
6      type: 'string',
7      name: 'username',
8    },
9    {
10      type: 'integer',
11      name: 'age',
12    },
13  ],
14});

2. CollectionOptions

1export interface CollectionOptions {
2  name: string;
3  title?: string;
4  dataSource?: string;
5  duplicator?:
6    | dumpable
7    | {
8        dumpable: dumpable;
9        with?: string[] | string;
10        delayRestore?: any;
11      };
12
13  tableName?: string;
14  inherits?: string[] | string;
15  inherit?: string;
16  key?: string;
17  viewName?: string;
18  writableView?: boolean;
19
20  filterTargetKey?: string;
21  fields?: CollectionFieldOptions[];
22  model?: any;
23  repository?: any;
24  sortable?: CollectionSortable;
25  /**
26   * @default true
27   */
28  autoGenId?: boolean;
29  /**
30   * @default 'options'
31   */
32  magicAttribute?: string;
33
34  tree?: string;
35
36  template?: string;
37
38  isThrough?: boolean;
39  autoCreate?: boolean;
40  resource?: string;
41  collectionName?: string;
42  sourceKey?: string;
43  uiSchema?: any;
44  [key: string]: any;
45}
  • name: Collection 的标识,必须唯一。

  • title: Collection 的标题,用于显示。

  • fields: 字段列表,详细说明请查看 CollectionField

  • template: 模板标识,用于标识该 Collection 是由哪个模板创建的,详细说明请查看 CollectionTemplate

  • dataSource: 数据源标识,用于标识该 Collection 是由哪个数据源创建的,详细说明请查看 DataSource

  • duplicator

  • tableName

  • inherits

  • viewName

  • writableView

  • filterTargetKey

  • model

  • repository

3. 实例属性

collection.collectionManager

CollectionManager 的实例。

collection.titleFieldName

标题字段的 name 属性。

其他属性

其他属性同 CollectionOptions

4. 实例方法

collection.getOptions()

获取 collection 的所有配置项。

  • 类型
1class Collection {
2  getOptions(): CollectionOptions;
3}
  • 示例
1const usersCollection = new Collection({
2  name: 'users',
3  title: 'Users',
4  fields: [
5    // ...
6  ],
7});
8
9console.log(usersCollection.getOptions()); // { name: 'users', title: 'Users', fields: [ ] }

collection.setOptions(options)

设置 collection 的配置项,最终会和默认配置项进行合并。

  • 类型
1class Collection {
2  setOptions(options: CollectionOptions): void;
3}
  • 示例
1collection.setOptions({
2  name: 'users',
3  title: 'Users',
4  fields: [
5    // ...
6  ],
7});

collection.getOption(key)

获取 collection 的单个配置项。

  • 类型
1class Collection {
2  getOption<K extends keyof CollectionOptions>(key: K): CollectionOptions[K];
3}
  • 示例
1collection.getOption('name'); // 'users'
2collection.getOption('title'); // 'Users'

collection.getFields(predicate?)

获取 collection 的字段列表。

  • 类型
1class Collection {
2  getFields(predicate?: CollectionFieldOptions | ((collection: CollectionFieldOptions) => boolean) | keyof CollectionFieldOptions): any[]
3}
  • 详解
    • predicate
      • 类型
        • CollectionFieldOptions
        • (collection: CollectionFieldOptions) => boolean
        • keyof CollectionFieldOptions
      • 说明
        • 如果传递了 predicate,则返回符合条件的字段列表
        • 如果没有传递 predicate,则返回所有字段列表

predicate 的使用可看参考 lodash.filter

  • 示例
1collection.getFields(); // [{ name: 'username', type: 'string', primaryKey: true }, { name: 'age', type: 'integer' }]
2
3collection.getFields({ name: 'age' }); // [{ name: 'age', type: 'integer' }]
4
5collection.getFields('primaryKey'); // [{ name: 'username', type: 'string', primaryKey: true }]
6
7collection.getFields(field => field.type === 'string'); // [{ name: 'name', type: 'string' }]

collection.getField(name)

获取 collection 的单个字段。

  • 类型
1class Collection {
2  getField(name: SchemaKey): CollectionFieldOptions
3}
  • 示例
1collection.getField('username'); // { name: 'username', type: 'string', primaryKey: true }

collection.hasField(name)

判断 collection 是否存在某个字段。

  • 类型
1class Collection {
2  hasField(name: SchemaKey): boolean;
3}
  • 示例
1collection.hasField('username'); // true
2collection.hasField('name'); // false