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