Back to Lowdefy

Operators Plugin Overview

code-docs/plugins/operators/overview.md

5.2.05.4 KB
Original Source

Operators Plugin Overview

Operators are functions that make Lowdefy configuration dynamic. They transform static YAML into reactive applications.

What Are Operators?

Operators are:

  • Functions prefixed with _
  • Evaluated at build time or runtime
  • Used to access state, transform data, add logic
  • The "code" in configuration-driven apps

Operator Evaluation Contexts

ContextWhenPackageExample Operators
Buildlowdefy build@lowdefy/build_ref, _var, _env
ServerRequest execution@lowdefy/api_secret, _user
ClientPage rendering@lowdefy/engine_state, _request

Available Operator Packages

PackagePurposeKey Operators
@lowdefy/operators-jsCore JS operators_if, _get, _state, _array
@lowdefy/operators-mqlMongoDB Query Language_mql_* query operators
@lowdefy/operators-dayjsDate/time with Day.js_dayjs, _date
@lowdefy/operators-nunjucksTemplate strings_nunjucks
@lowdefy/operators-change-caseString case conversion_change_case
@lowdefy/operators-diffObject diffing_diff
@lowdefy/operators-uuidUUID generation_uuid
@lowdefy/operators-yamlYAML parsing_yaml_parse, _yaml_stringify
@lowdefy/operators-jsonataJSONata queries_jsonata

Operator Syntax

Standard Form

yaml
result:
  _operatorName:
    param1: value1
    param2: value2

Shorthand (for getters)

yaml
# Shorthand
value:
  _state: fieldName

# Equivalent to:
value:
  _state:
    key: fieldName

Nested Operators

Operators can contain other operators:

yaml
greeting:
  _if:
    test:
      _eq:
        - _state: userType
        - admin
    then:
      _string:
        - 'Welcome, Admin '
        - _state: userName
    else: Welcome, User

Core Data Access Operators

These are from @lowdefy/operators-js:

OperatorPurposeContext
_statePage state valuesClient
_requestRequest responsesClient
_url_queryURL parametersClient
_inputNavigation inputClient
_globalGlobal stateClient
_userUser sessionServer/Client
_secretEnvironment secretsServer
_argsFunction argumentsAll
_eventEvent payloadClient

Logic Operators

OperatorPurpose
_ifConditional
_andLogical AND
_orLogical OR
_notLogical NOT
_eqEquality
_neNot equal
_gtGreater than
_gteGreater or equal
_ltLess than
_lteLess or equal

Data Transform Operators

OperatorPurpose
_getGet nested value
_arrayCreate array
_objectCreate object
_stringConcatenate strings
_sumSum numbers
_subtractSubtract
_multiplyMultiply
_divideDivide
_mathMath operations

Runtime Schema Validation

Each operator can export a .schema.js file describing its expected params shape (e.g., get.schema.js). These schemas are collected at build time into plugins/operatorSchemas.json.

When an OperatorError occurs at runtime, the server validates the received params against the operator's schema and produces a diagnostic ConfigError:

[ConfigError] Operator "_get" required param "from" is missing.

For method-qualified operators (e.g., _yaml.parse), the display name includes the method and params are extracted from the method-style received key.

This is reactive validation — it only runs when an error is caught, not on every evaluation. See plugin-system.md for schema format details.

Design Decisions

Why Underscore Prefix?

  • Clear visual distinction from data
  • Won't conflict with user property names
  • Easy to identify during parsing
  • Common convention (MongoDB uses $)

Why Multiple Packages?

  • Modularity: only include what you use
  • Bundle size: keep dependencies small
  • Flexibility: swap implementations
  • Independence: version separately

Why Not Just JavaScript?

Operators provide:

  • Declarative configuration
  • Safe evaluation (sandboxed)
  • Build-time analysis
  • Consistent cross-context behavior
  • Easier debugging