logologo
开始
教程
手册
开发
插件
API
首页
English
简体中文
日本語
한국어
Español
Português
Deutsch
Français
Русский
开始
教程
手册
开发
插件
API
首页
logologo
RunJS 概述
导入模块
容器内渲染

全局变量

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.collectionField
Next Pagectx.dataSourceManager

#ctx.dataSource

当前 RunJS 执行上下文绑定的数据源实例(DataSource),用于在当前数据源内访问数据表、字段元数据及管理数据表配置。通常对应当前页/区块选中的数据源(如主库 main)。

#适用场景

场景说明
单数据源操作在已知当前数据源时,获取数据表、字段元数据
数据表管理获取/添加/更新/删除当前数据源下的数据表
按路径获取字段使用 collectionName.fieldPath 格式获取字段定义(支持关联路径)

注意:ctx.dataSource 表示当前上下文的单一数据源;若要枚举或访问其他数据源,请使用 ctx.dataSourceManager。

#类型定义

dataSource: DataSource;

class DataSource {
  constructor(options?: Record<string, any>);

  // 只读属性
  get flowEngine(): FlowEngine;   // 当前 FlowEngine 实例
  get displayName(): string;      // 显示名称(支持 i18n)
  get key(): string;              // 数据源 key,如 'main'
  get name(): string;             // 同 key

  // 数据表读取
  getCollections(): Collection[];                      // 获取所有数据表
  getCollection(name: string): Collection | undefined; // 按名称获取数据表
  getAssociation(associationName: string): CollectionField | undefined; // 获取关联字段(如 users.roles)

  // 数据表管理
  addCollection(collection: Collection | CollectionOptions): void;
  updateCollection(newOptions: CollectionOptions): void;
  upsertCollection(options: CollectionOptions): Collection | undefined;
  upsertCollections(collections: CollectionOptions[], options?: { clearFields?: boolean }): void;
  removeCollection(name: string): void;
  clearCollections(): void;

  // 字段元数据
  getCollectionField(fieldPath: string): CollectionField | undefined;
}

#常用属性

属性类型说明
keystring数据源 key,如 'main'
namestring同 key
displayNamestring显示名称(支持 i18n)
flowEngineFlowEngine当前 FlowEngine 实例

#常用方法

方法说明
getCollections()获取当前数据源下所有数据表(已排序、过滤隐藏)
getCollection(name)按名称获取数据表;name 可为 collectionName.fieldName 获取关联目标数据表
getAssociation(associationName)按 collectionName.fieldName 获取关联字段定义
getCollectionField(fieldPath)按 collectionName.fieldPath 获取字段定义,支持关联路径如 users.profile.avatar

#与 ctx.dataSourceManager 的关系

需求推荐用法
当前上下文绑定的单一数据源ctx.dataSource
所有数据源入口ctx.dataSourceManager
当前数据源内获取数据表ctx.dataSource.getCollection(name)
跨数据源获取数据表ctx.dataSourceManager.getCollection(dataSourceKey, collectionName)
当前数据源内获取字段ctx.dataSource.getCollectionField('users.profile.avatar')
跨数据源获取字段ctx.dataSourceManager.getCollectionField('main.users.profile.avatar')

#示例

#获取数据表及字段

// 获取所有数据表
const collections = ctx.dataSource.getCollections();

// 按名称获取数据表
const users = ctx.dataSource.getCollection('users');
const primaryKey = users?.filterTargetKey ?? 'id';

// 按「数据表.字段路径」获取字段定义(支持关联)
const field = ctx.dataSource.getCollectionField('users.profile.avatar');
const userNameField = ctx.dataSource.getCollectionField('orders.createdBy.name');

#获取关联字段

// 按 collectionName.fieldName 获取关联字段定义
const rolesField = ctx.dataSource.getAssociation('users.roles');
if (rolesField?.isAssociationField()) {
  const targetCol = rolesField.targetCollection;
  // 按目标数据表结构处理
}

#遍历数据表做动态处理

const collections = ctx.dataSource.getCollections();
for (const col of collections) {
  const fields = col.getFields();
  const requiredFields = fields.filter((f) => f.options?.required);
  // ...
}

#根据字段元数据做校验或动态 UI

const field = ctx.dataSource.getCollectionField('users.status');
if (field) {
  const options = field.enum ?? [];
  const operators = field.getFilterOperators();
  // 根据 interface、enum、validation 等做 UI 或校验
}

#注意事项

  • getCollectionField(fieldPath) 的路径格式为 collectionName.fieldPath,第一段为数据表名,后续为字段路径(支持关联,如 user.name)。
  • getCollection(name) 支持 collectionName.fieldName 形式,返回关联字段的目标数据表。
  • ctx.dataSource 在 RunJS 上下文中通常由当前区块/页面的数据源决定;若上下文无绑定数据源,可能为 undefined,使用前建议做空值判断。

#相关

  • ctx.dataSourceManager:数据源管理器,管理所有数据源
  • ctx.collection:当前上下文关联的数据表
  • ctx.collectionField:当前字段的数据表字段定义