DataSourceManager
NocoBase provides DataSourceManager for managing multiple data sources. Each DataSource has its own Database, ResourceManager, and ACL instances, allowing you to flexibly manage and extend different data sources.
Basic Concepts
Each DataSource instance contains the following:
dataSource.collectionManager: Used to manage collections and fields.dataSource.resourceManager: Handles resource-related operations (such as CRUD, etc.).dataSource.acl: Access control (ACL) for resource operations.
For convenient access, NocoBase provides aliases for main data source members:
app.dbis equivalent todataSourceManager.get('main').collectionManager.dbapp.aclis equivalent todataSourceManager.get('main').aclapp.resourceManageris equivalent todataSourceManager.get('main').resourceManager
Common Methods
dataSourceManager.get(dataSourceKey)
Returns the specified DataSource instance.
dataSourceManager.use()
Register middleware for all data sources, which will affect operations on all data sources.
dataSourceManager.beforeAddDataSource()
Executes before data source loading. Typically used for static class registration, such as model classes and field type registration:
dataSourceManager.afterAddDataSource()
Executes after data source loading. Typically used for registering operations, setting access control, etc.
Data Source Extension
For complete data source extension, please refer to the data source extension chapter.
Related Links
- Database - CRUD, Repository, transactions, and database events
- Collections - Define or extend data table structures with code
- ResourceManager - Register custom APIs and resource operations
- ACL - Role permissions, permission snippets, and access control
- Plugin - Plugin class lifecycle, member methods, and the
appobject

