CollectionManager

用于管理 Collection,其被 DataSource 管理。

实例方法

addCollections(collections)

添加数据表。

  • 类型
1class CollectionManager {
2  addCollections(collections: CollectionOptions[]): void
3}
  • 示例
1const userCollectionOptions = {
2  "name": "users",
3  "title": "Users",
4  fields: [
5    // ...
6  ],
7};
8
9collectionManager.addCollections([userCollectionOptions]);

setCollections(collections)

重置数据表,会先移除所有数据表,然后再调用 addCollections() 添加数据表。

  • 类型
1class CollectionManager {
2  setCollections(collections: CollectionOptions[]): void
3}

reAddCollections(collections)

由于 CollectionTemplate 或者 CollectionMixins 的添加会影响 Collection 的实例化,所以提供了重新添加数据表的方法。

  • 类型
1class CollectionManager {
2  reAddCollections(collectionInstances: Collection[]): void
3}
  • 示例
1const userCollectionInstance = collectionManager.getCollection('users');
2collectionManager.reAddCollections([userCollectionInstance]);

getCollections(predicate?)

获取数据表数组。

  • 类型
1class CollectionManager {
2  getCollections(predicate?: (collection: Collection) => boolean)
3}
  • 示例
1collectionManager.getCollections(); // [ userCollection ]
2
3collectionManager.getCollections(collection => collection.name === 'posts'); // [ postCollection ]

getCollection(path)

获取数据表。

  • 类型
1class CollectionManager {
2  getCollection<Mixins = {}>(path: string): (Mixins & Collection) | undefined
3}
  • 详细解释

    • path 参数可以是数据表名称,也可以是关系字段路径。
      • path: 'users': 获取 users 数据表
      • path: 'users.posts': 获取 users 数据表的 posts 关联字段对应的数据表,即 postCollection
  • 示例

1collectionManager.getCollection('users'); // userCollection
2
3collectionManager.getCollection('users.posts'); // postCollection
4collectionManager.getCollection('users.profileId'); // profileCollection

结合 Mixin 使用:

1const collection = collectionManager.getCollection<TestMixin>('users');
2const collection = collectionManager.getCollection<TestMixin & TestMixin2>('users');

getCollectionFields(collectionName)

获取数据表字段列表。

  • 类型
1class CollectionManager {
2  getCollectionFields(collectionName: string): CollectionFieldOptions[];
3}
  • 示例
1collectionManager.getCollectionFields('users'); // [ { name: 'username', type: 'string', title: 'Username', .. }, { name: 'password', type: 'password', title: 'Password', .. } ]

getCollectionName(path)

获取数据表名称。

  • 类型
1class CollectionManager {
2  getCollectionName(path: string: GetCollectionOptions): string | undefined;
3}
  • 示例
1collectionManager.getCollectionName('users'); // 'users'
2
3collectionManager.getCollectionName('users.profiles'); // 'profiles'

getCollectionField(path)

获取数据表字段。

  • 类型
1class CollectionManager {
2  getCollectionField(path: string: GetCollectionOptions): CollectionFieldOptions | undefined;
3}
  • 示例
1collectionManager.getCollectionField('users.username'); // { name: 'username', type: 'string', title: 'Username', .. }
2
3collectionManager.getCollectionField('users.roles.name'); // 获取 roles 关联字段对应的 roles 表中的 name 字段