The Data Source Manager (DataSourceManager instance) is used to manage and access multiple data sources (e.g., the main database main, logging database logging, etc.). It is used when multiple data sources exist or when cross-data source metadata access is required.
| Scenario | Description |
|---|---|
| Multi-data source | Enumerate all data sources, or get a specific data source by key. |
| Cross-data source access | Access metadata using the "data source key + collection name" format when the data source of the current context is unknown. |
| Get fields by full path | Use the dataSourceKey.collectionName.fieldPath format to retrieve field definitions across different data sources. |
Note: If you are only operating on the current data source, prioritize using
ctx.dataSource. Usectx.dataSourceManageronly when you need to enumerate or switch between data sources.
| Requirement | Recommended Usage |
|---|---|
| Single data source bound to the current context | ctx.dataSource (e.g., the data source of the current page/block) |
| Entry point for all data sources | ctx.dataSourceManager |
| List or switch data sources | ctx.dataSourceManager.getDataSources() / getDataSource(key) |
| Get collection within the current data source | ctx.dataSource.getCollection(name) |
| Get collection across data sources | ctx.dataSourceManager.getCollection(dataSourceKey, collectionName) |
| Get field within the current data source | ctx.dataSource.getCollectionField('users.profile.avatar') |
| Get field across data sources | ctx.dataSourceManager.getCollectionField('main.users.profile.avatar') |
getCollectionField is dataSourceKey.collectionName.fieldPath, where the first segment is the data source key, followed by the collection name and the field path.getDataSource(key) returns undefined if the data source does not exist; it is recommended to perform a null check before use.addDataSource will throw an exception if the key already exists; upsertDataSource will either overwrite the existing one or add a new one.