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.dataSource
Next Pagectx.element
AI翻訳通知

このドキュメントはAIによって翻訳されました。正確な情報については英語版をご参照ください。

#ctx.dataSourceManager

データソースマネージャー(DataSourceManager インスタンス)は、複数のデータソース(メインデータベース main、ログデータベース logging など)を管理およびアクセスするために使用されます。複数のデータソースが存在する場合や、データソースを跨いでメタデータにアクセスする必要がある場合に使用します。

#適用シーン

シーン説明
多データソースすべてのデータソースを列挙する、またはキーを指定して特定のデータソースを取得する。
データソースを跨いだアクセス現在のコンテキストのデータソースが不明な場合に、「データソースキー + コレクション名」の形式でメタデータにアクセスする。
フルパスによるフィールド取得dataSourceKey.collectionName.fieldPath 形式を使用して、異なるデータソース間のフィールド定義を取得する。

注意:現在のデータソースのみを操作する場合は、ctx.dataSource を優先的に使用してください。データソースを列挙したり切り替えたりする必要がある場合にのみ、ctx.dataSourceManager を使用します。

#型定義

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;
}

#ctx.dataSource との関係

目的/ニーズ推奨される使い方
現在のコンテキストに紐付く単一のデータソースctx.dataSource(現在のページやブロックのデータソースなど)
すべてのデータソースへのエントリポイントctx.dataSourceManager
データソースの列挙または切り替えctx.dataSourceManager.getDataSources() / getDataSource(key)
現在のデータソース内でのコレクション取得ctx.dataSource.getCollection(name)
データソースを跨いだコレクション取得ctx.dataSourceManager.getCollection(dataSourceKey, collectionName)
現在のデータソース内でのフィールド取得ctx.dataSource.getCollectionField('users.profile.avatar')
データソースを跨いだフィールド取得ctx.dataSourceManager.getCollectionField('main.users.profile.avatar')

#例文

#指定したデータソースの取得

// 'main' という名前のデータソースを取得
const mainDS = ctx.dataSourceManager.getDataSource('main');

// そのデータソース配下のすべてのコレクションを取得
const collections = mainDS?.getCollections();

#データソースを跨いだコレクションメタデータへのアクセス

// dataSourceKey + collectionName でコレクションを取得
const users = ctx.dataSourceManager.getCollection('main', 'users');
const orders = ctx.dataSourceManager.getCollection('main', 'orders');

// コレクションの主キーを取得
const primaryKey = users?.filterTargetKey ?? 'id';

#フルパスによるフィールド定義の取得

// 形式:dataSourceKey.collectionName.fieldPath
// 「データソースキー.コレクション名.フィールドパス」でフィールド定義を取得
const field = ctx.dataSourceManager.getCollectionField('main.users.profile.avatar');

// 関連フィールドのパスにも対応
const userNameField = ctx.dataSourceManager.getCollectionField('main.orders.createdBy.name');

#すべてのデータソースの反復処理

const dataSources = ctx.dataSourceManager.getDataSources();
for (const ds of dataSources) {
  ctx.logger.info(`データソース: ${ds.key}, 表示名: ${ds.displayName}`);
  const collections = ds.getCollections();
  for (const col of collections) {
    ctx.logger.info(`  - コレクション: ${col.name}`);
  }
}

#変数に基づいたデータソースの動的選択

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();
  // ...
}

#注意事項

  • getCollectionField のパス形式は dataSourceKey.collectionName.fieldPath です。最初のセグメントがデータソースのキーであり、その後にコレクション名とフィールドパスが続きます。
  • getDataSource(key) は、データソースが存在しない場合に undefined を返します。使用前に null チェックを行うことを推奨します。
  • addDataSource はキーが既に存在する場合に例外をスローします。upsertDataSource は既存のものを上書きするか、新しく追加します。

#関連情報

  • ctx.dataSource:現在のデータソースインスタンス
  • ctx.collection:現在のコンテキストに関連付けられたコレクション
  • ctx.collectionField:現在のフィールドのコレクションフィールド定義