logologo
Get Started
Guide
Development
Plugins
API
Home
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
Italiano
Türkçe
Українська
Tiếng Việt
Bahasa Indonesia
ไทย
Polski
Nederlands
Čeština
العربية
עברית
हिन्दी
Svenska
Get Started
Guide
Development
Plugins
API
Home
logologo
RunJS Overview
Import Modules
Render in Container

Global Variables

window
document
navigator

ctx

ctx.blockModel
ctx.collection
ctx.collectionField
ctx.dataSource
ctx.dataSourceManager
ctx.element
ctx.exit()
ctx.exitAll()
ctx.filterManager
ctx.form
ctx.getModel()
ctx.getValue()
ctx.getVar()
ctx.i18n
ctx.importAsync()
ctx.initResource()
ctx.libs
ctx.location
ctx.logger
ctx.makeResource()
ctx.message
ctx.modal
ctx.model
ctx.notification
ctx.off()
ctx.on()
ctx.openView()
ctx.render()
ctx.request()
ctx.requireAsync()
ctx.resource
ctx.route
ctx.router
ctx.setValue()
ctx.sql
ctx.t()
ctx.view
Previous Pagectx.dataSource
Next Pagectx.element

#ctx.dataSourceManager

The data source manager (DataSourceManager instance) for managing and accessing multiple data sources (e.g. main main, logging logging). Use when you have multiple data sources or need cross–data-source metadata access.

#Use Cases

ScenarioDescription
Multiple data sourcesEnumerate all data sources, get one by key
Cross–data-source accessWhen context doesn’t know the data source, access by “data source key + collection name”
Field by full pathGet field definition with path format dataSourceKey.collectionName.fieldPath

Note: If you only work with the current data source, use ctx.dataSource; use ctx.dataSourceManager when you need to enumerate or switch data sources.

#Type

dataSourceManager: DataSourceManager;

class DataSourceManager {
  constructor();

  addDataSource(ds: DataSource | DataSourceOptions): void;
  upsertDataSource(ds: DataSource | DataSourceOptions): void;
  removeDataSource(key: string): void;
  clearDataSources(): void;

  getDataSources(): DataSource[];
  getDataSource(key: string): DataSource | undefined;

  getCollection(dataSourceKey: string, collectionName: string): Collection | undefined;
  getCollectionField(fieldPathWithDataSource: string): CollectionField | undefined;
}

#Relation to ctx.dataSource

NeedRecommended
Single data source for contextctx.dataSource
Entry to all data sourcesctx.dataSourceManager
List or switch data sourcesctx.dataSourceManager.getDataSources() / getDataSource(key)
Collection in current data sourcectx.dataSource.getCollection(name)
Collection in another data sourcectx.dataSourceManager.getCollection(dataSourceKey, collectionName)
Field in current data sourcectx.dataSource.getCollectionField('users.profile.avatar')
Field across data sourcesctx.dataSourceManager.getCollectionField('main.users.profile.avatar')

#Examples

#Get a data source

const mainDS = ctx.dataSourceManager.getDataSource('main');
const collections = mainDS?.getCollections();

#Cross–data-source collection metadata

const users = ctx.dataSourceManager.getCollection('main', 'users');
const orders = ctx.dataSourceManager.getCollection('main', 'orders');

const primaryKey = users?.filterTargetKey ?? 'id';

#Field by full path

// Format: dataSourceKey.collectionName.fieldPath
const field = ctx.dataSourceManager.getCollectionField('main.users.profile.avatar');

const userNameField = ctx.dataSourceManager.getCollectionField('main.orders.createdBy.name');

#Iterate all data sources

const dataSources = ctx.dataSourceManager.getDataSources();
for (const ds of dataSources) {
  ctx.logger.info(`Data source: ${ds.key}, display: ${ds.displayName}`);
  const collections = ds.getCollections();
  for (const col of collections) {
    ctx.logger.info(`  - Collection: ${col.name}`);
  }
}

#Dynamic data source from variable

const dsKey = ctx.getVar('dataSourceKey') ?? 'main';
const collectionName = ctx.getVar('collectionName') ?? 'users';
const col = ctx.dataSourceManager.getCollection(dsKey, collectionName);
if (col) {
  const fields = col.getFields();
  // ...
}

#Notes

  • getCollectionField path format is dataSourceKey.collectionName.fieldPath; first segment is data source key, then collection name and field path.
  • getDataSource(key) returns undefined if the data source doesn’t exist—check before use.
  • addDataSource throws if key already exists; upsertDataSource overwrites or adds.

#Related

  • ctx.dataSource: current data source instance
  • ctx.collection: collection for current context
  • ctx.collectionField: current field’s collection field definition