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

Globals

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.setValue()
Next Pagectx.t()

#ctx.sql

ctx.sql provides SQL execution and management. It is commonly used in RunJS (e.g. JSBlock) to access the database directly. It supports temporary SQL execution, executing saved SQL templates by ID, binding parameters, result type control, and saving/deleting SQL templates.

#Permissions

  • Logged-in users: can execute runById (run saved SQL templates by ID).
  • Roles with SQL config permissions: can also execute run, save, destroy (execute temporary SQL, save/update/delete templates).

Therefore, frontend logic for logged-in users can use ctx.sql.runById(uid, options). For dynamic SQL or template management, ensure the current role has SQL config permissions.


#Type definition

sql: FlowSQLRepository;

interface FlowSQLRepository {
  run<T = any>(
    sql: string,
    options?: {
      bind?: Record<string, any> | any[];
      type?: 'select' | 'selectRow' | 'selectVar' | 'raw';
      dataSourceKey?: string;
      filter?: Record<string, any>;
    },
  ): Promise<T>;

  save(options: {
    uid: string;
    sql: string;
    dataSourceKey?: string;
  }): Promise<void>;

  runById<T = any>(
    uid: string,
    options?: {
      bind?: Record<string, any> | any[];
      type?: 'select' | 'selectRow' | 'selectVar' | 'raw';
      dataSourceKey?: string;
      filter?: Record<string, any>;
    },
  ): Promise<T>;

  destroy(uid: string): Promise<void>;
}

Note: implementation lives in FlowSQLRepository. Only commonly used methods/params are listed here.


#Common methods

MethodDescriptionPermission
ctx.sql.run(sql, options?)Execute temporary SQL with bind and result typeRequires SQL config permission
ctx.sql.save({ uid, sql, dataSourceKey? })Save/update SQL template by IDRequires SQL config permission
ctx.sql.runById(uid, options?)Run saved SQL template by ID (options same as run)Logged-in users
ctx.sql.destroy(uid)Delete SQL template by IDRequires SQL config permission

#Parameters

  • Options for run / runById

    • bind: bind variables (object or array)
    • type: result type (e.g. select multiple rows, selectRow single row, selectVar single value, raw)
    • dataSourceKey: data source key
    • filter: filter (if supported by the API)
  • Options for save

    • uid: template unique ID, used by runById(uid, ...)
    • sql: SQL content
    • dataSourceKey: optional data source key

#Example

// Register needed context
ctx.defineProperty('minId', {
  get: () => 1,
});

const sql = 'SELECT * FROM users WHERE id > {{ctx.minId}}';

// Requires SQL config permission: temporary SQL
const rows = await ctx.sql.run(sql, { type: 'select' });

// Requires SQL config permission: save SQL template
await ctx.sql.save({ uid: 'my-report-uid', sql });

// Logged-in users: run saved SQL template
const data = await ctx.sql.runById('my-report-uid', {
  type: 'select',
});

// Requires SQL config permission: delete SQL template
await ctx.sql.destroy('my-report-uid');