用于创建数据表的模板。
1interface AvailableFieldInterfacesInclude {
2 include?: any[];
3}
4
5interface AvailableFieldInterfacesExclude {
6 exclude?: any[];
7}
8
9interface CollectionTemplateDefaultOptions {
10 /**
11 * Auto-generate id
12 * @default true
13 * */
14 autoGenId?: boolean;
15 /** Created by */
16 createdBy?: boolean;
17 /** Updated by */
18 updatedBy?: boolean;
19 /** Created at */
20 createdAt?: boolean;
21 /** Updated at */
22 updatedAt?: boolean;
23 /** Sortable */
24 sortable?: boolean;
25 /* Tree structure */
26 tree?: string;
27 /* Logging */
28 logging?: boolean;
29 /** Inherits */
30 inherits?: string | string[];
31 /* Field list */
32 fields?: CollectionOptions['fields'];
33}
34
35class CollectionTemplate {
36 constructor(public collectionTemplateManager: CollectionTemplateManager) {}
37 name: string;
38 Collection?: typeof Collection;
39 title?: string;
40 color?: string;
41 /** Sorting */
42 order?: number;
43 /** Default configuration */
44 default?: CollectionTemplateDefaultOptions;
45 events?: any;
46 /** UI configurable CollectionOptions parameters (fields for adding or editing Collection forms) */
47 configurableProperties?: Record<string, ISchema>;
48 /** Available field types for the current template */
49 availableFieldInterfaces?: AvailableFieldInterfacesInclude | AvailableFieldInterfacesExclude;
50 /** Whether it is a divider */
51 divider?: boolean;
52 /** Template description */
53 description?: string;
54 /** Configure buttons in the configuration fields */
55 configureActions?: Record<string, ISchema>;
56 // Whether to prohibit deleting fields
57 forbidDeletion?: boolean;
58
59 supportDataSourceType?: string[];
60 notSupportDataSourceType?: string[];
61
62 transform?(collection: CollectionOptions, app: Application): CollectionOptions;
63}
其需要结合 CollectionTemplateManager 使用。
1import { Plugin, Collection, CollectionTemplate } from '@tachybase/client';
2
3class SqlCollection extends Collection {
4 otherMethods() {
5 // ...
6 }
7}
8
9class SqlCollectionTemplate extends CollectionTemplate {
10 name = 'sql';
11 Collection = SqlCollection; // 自定义的数据表类
12 title = '{{t("SQL collection")}}';
13 order = 4;
14 color = 'yellow';
15 default = {
16 fields: [],
17 };
18 configurableProperties = {
19 // ...
20 }
21}
22
23class MyPlugin extends Plugin {
24 async load() {
25 this.app.dataSourceManager.collectionTemplateManager.addCollectionTemplates([ SqlCollectionTemplate ]);
26
27 // or
28 this.app.dataSourceManager.addCollectionTemplates([ SqlCollectionTemplate ]);
29 }
30}
模板的唯一标识符。
模板对应的数据表类。
在创建数据表后,Collection 会有 template 字段,用于标识该数据表是由哪个模板创建的。
当通过 collectionManager.addCollections()
添加数据表对象时,会先读取 collection.template
字段,然后通过 collectionManager.getCollectionTemplate(collection.template)
获取到 collectionTemplate
。
读取 collectionTemplate.Collection
字段,并通过 new collectionTemplate.Collection(collection)
创建对应的实例。
如果不传递 Collection
,则会通过 new Collection(collection)
创建对应的实例。
1class SqlCollection extends Collection {
2 otherMethods() {
3 // ...
4 }
5}
6
7class SqlCollectionTemplate extends CollectionTemplate {
8 name = 'sql';
9 Collection = SqlCollection; // 自定义的数据表类
10 // ...
11}
12
13const userCollection = {
14 name: 'users',
15 template: 'sql',
16 // ...
17}
18
19// 内部会调用 new SqlCollection(userCollection)
模板的标题。
模板的颜色。
模板的排序。
beforeSubmit
:提交前触发表单配置项。
1class SqlCollectionTemplate extends CollectionTemplate {
2 name = 'sql',
3 // ...
4 configurableProperties = {
5 title: {
6 type: 'string',
7 title: '{{ t("Collection display name") }}',
8 required: true,
9 'x-decorator': 'FormItem',
10 'x-component': 'Input',
11 },
12 name: {
13 type: 'string',
14 title: '{{t("Collection name")}}',
15 required: true,
16 'x-disabled': '{{ !createOnly }}',
17 'x-decorator': 'FormItem',
18 'x-component': 'Input',
19 'x-validator': 'uid',
20 description:
21 "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}",
22 },
23 // ...
24 },
25}
表单默认值。
用于获取内置的配置项字段。
1export type DefaultConfigurableKeys =
2 | 'name'
3 | 'title'
4 | 'inherits'
5 | 'category'
6 | 'autoGenId'
7 | 'createdBy'
8 | 'updatedBy'
9 | 'createdAt'
10 | 'updatedAt'
11 | 'sortable'
12 | 'description'
13 | 'moreOptions';
14
15const getConfigurableProperties: (...keys: DefaultConfigurableKeys[]) => Record<DefaultConfigurableKeys, any>
1import { getConfigurableProperties } from '@tachybase/client';
2
3const sqlCollectionTemplate = new CollectionTemplate({
4 name: 'sql',
5 // ...
6 configurableProperties: {
7 ...getConfigurableProperties('name', 'title', 'description'),
8 // ...
9 },
10});