DataSourceManager
数据源管理器,用于管理 DataSource
实例。
1. 实例属性
collectionTemplateManager
用于管理 CollectionTemplate
实例。
1import { Plugin, CollectionTemplate } from '@tachybase/client'
2
3class MyCollectionTemplate extends CollectionTemplate {
4 name = 'my-collection-template'
5}
6
7class MyPlugin extends Plugin {
8 async load() {
9 this.app.dataSourceManager.collectionTemplateManager.addCollectionTemplates([
10 MyCollectionTemplate,
11 ])
12 }
13}
详细请参考:CollectionTemplateManager
collectionFieldInterfaceManager
用于管理 CollectionFieldInterface
实例。
1import { Plugin, CollectionFieldInterface } from '@tachybase/client'
2
3class MyCollectionFieldInterface extends CollectionFieldInterface {
4 name = 'my-collection-field-interface'
5}
6
7class MyPlugin extends Plugin {
8 async load() {
9 this.app.dataSourceManager.collectionFieldInterfaceManager.addFieldInterfaces([
10 MyCollectionFieldInterface,
11 ])
12 }
13}
详细请参考:CollectionFieldInterfaceManager
2. 实例方法
addCollectionTemplates()
是 CollectionTemplateManager
的快捷方法,用于添加 CollectionTemplate
。
1class DataSourceManager {
2 addCollectionTemplates(templates: CollectionTemplate[]): void
3}
1import { Plugin, CollectionTemplate } from '@tachybase/client'
2
3class MyCollectionTemplate extends CollectionTemplate {
4 name = 'my-collection-template'
5}
6
7class MyPlugin extends Plugin {
8 async load() {
9 this.app.dataSourceManager.collectionTemplateManager.addCollectionTemplates([
10 MyCollectionTemplate,
11 ])
12 }
13}
更多详细请参考:CollectionTemplateManager
addFieldInterfaces()
是 CollectionFieldInterfaceManager
的快捷方法,用于添加 CollectionFieldInterface
。
1class DataSourceManager {
2 addFieldInterfaces(fieldInterfaces: CollectionFieldInterface[]): void
3}
1import { Plugin, CollectionFieldInterface } from '@tachybase/client'
2
3class MyCollectionFieldInterface extends CollectionFieldInterface {
4 name = 'my-collection-field-interface'
5}
6
7class MyPlugin extends Plugin {
8 async load() {
9 this.app.dataSourceManager.collectionFieldInterfaceManager.addFieldInterfaces([
10 MyCollectionFieldInterface,
11 ])
12 }
13}
更多详细请参考:CollectionFieldInterfaceManager
addCollectionMixins()
用于添加 Collection
的 Mixins。
1class DataSourceManager {
2 addCollectionMixins(mixins: (typeof Collection)[]): void
3}
1import { Plugin, Collection } from '@tachybase/client'
2
3class MyCollectionMixin extends Collection {
4 otherMethod() {
5 console.log('otherMethod')
6 }
7}
8
9class MyPlugin extends Plugin {
10 async load() {
11 this.app.dataSourceManager.addCollectionMixins([MyCollectionMixin])
12 }
13}
14
15const MyComponent = () => {
16 const collection = useCollection<MyCollectionMixin>()
17 collection.otherMethod()
18}
更多详细请参考:CollectionMixins
addDataSource()
用于添加 DataSource
。
1class DataSourceManager {
2 addDataSource(DataSource: DataSource, options: DataSourceOptions): void
3}
1import { Plugin, DataSource, DataSourceOptions } from '@tachybase/client'
2
3class MyDataSource extends DataSource {
4 async getDataSource() {
5 return {
6 status: 'loaded',
7 collections: [{ name: 'users' }],
8 }
9 }
10}
11
12class MyPlugin extends Plugin {
13 async load() {
14 this.app.dataSourceManager.addDataSource(MyDataSource, {
15 key: 'my-data-source',
16 displayName: 'My Data Source',
17 })
18 }
19}
更多详细请参考:DataSource
removeDataSources()
移除 DataSource
。
1class DataSourceManager {
2 removeDataSources(keys: string[]): void
3}
1const MyComponent = () => {
2 const dataSourceManager = useDataSourceManager()
3 dataSourceManager.removeDataSources(['my-data-source'])
4}
getDataSources()
获取全部 DataSource
实例列表。
1class DataSourceManager {
2 getDataSources(): DataSource[]
3}
1const MyComponent = () => {
2 const dataSourceManager = useDataSourceManager()
3 const dataSources = dataSourceManager.getDataSources()
4
5 return (
6 <div>
7 {dataSources.map(dataSource => (
8 <div key={dataSource.key}>{dataSource.displayName}</div>
9 ))}
10 </div>
11 )
12}
getDataSource(key)
获取 DataSource
实例。
1class DataSourceManager {
2 getDataSource(key: string): DataSource
3}
1const MyComponent = () => {
2 const dataSourceManager = useDataSourceManager()
3 const dataSource = dataSourceManager.getDataSource('my-data-source')
4
5 return <div>{dataSource.displayName}</div>
6}
getAllCollections()
获取所有 DataSource 的所有 Collection 实例。
1class DataSourceManager {
2 getAllCollections(options?: {
3 filterCollection?: (collection: Collection) => boolean
4 filterDataSource?: (dataSource: DataSource) => boolean
5 }): (DataSourceOptions & { collections: Collection[] })[]
6}
1const MyComponent = () => {
2 const dataSourceManager = useDataSourceManager()
3 const collections = dataSourceManager.getAllCollections()
4
5 return (
6 <div>
7 {collections.map(({ key, displayName, collections }) => (
8 <div key={key}>
9 <h3>{displayName}</h3>
10 <ul>
11 {collections.map(collection => (
12 <li key={collection.name}>{collection.name}</li>
13 ))}
14 </ul>
15 </div>
16 ))}
17 </div>
18 )
19}
reload()
重载所有 DataSource
。
1class DataSourceManager {
2 reload(): Promise<void>
3}
1const MyComponent = () => {
2 const dataSourceManager = useDataSourceManager()
3 dataSourceManager.reload()
4}