Skip to content

Dynamic Expression Engine

JavaScript Expression Engine

By seamlessly integrating JavaScript code, Metal empowers users to create more versatile and adaptive configurations. To embed JS code within your key values, encapsulate it like this:

javascript
${{ /* JS code here */ }}

Usage

Example: Filtering Dates Before Now

Consider the following example, where JS code is applied to filter dates before the current date:

http
POST /schema/my-schema/my-entity
Content-Type: application/json

{
	"filter-expression": "date < '${{ new Date().toLocaleDateString(\"en-US\") }}'"
}

In this instance, the filter-expression incorporates JS code within the curly braces to dynamically calculate the current date in the specified format.

This flexibility enables precise control and customization, showcasing the versatility that JS Expression Engine brings to your Metal implementation.

Context variables

Context variables are placeholders that can be used within your JavaScript Expression Engine. They are denoted by the $ character, followed by the variable name.

$schema v0.4+

The name of the schema being queried.

$entity v0.4+

The name of the entity being queried.

$request v0.4+

The current request being processed (exclusive to web services data providers), containing the following properties:

PropertyTypeDescription
methodstringThe HTTP method used.
urlstringThe URL of the request.
headersobjectThe request headers.
bodyobjectThe request body.
data-pathStringPath of json data to find in response

$response v0.4+

The response object for web services (exclusive to web services data providers), containing the following properties:

PropertyTypeDescription
urlStringRequested URL
hostStringRequested host
bodyObjectReturned response body

$row v0.4+

The current row being processed (exclusive to web services data providers). These object contains returned fields of the row

$utils v0.4+

The $utils object, containing various utility functions and methods, including data manipulation, string operations, and more.

Implemented Utils:

variabledescription
$utils._lodash utility functions
$utils.JSONJSON utility functions
$utils.MathMath utility functions
$utils.newUuid()generates a new UUID

Field Escape Engine v0.4+

To escape a field value and treat it as an expression, prefix it with $>. This is particularly useful when an update relies on existing fields. You can also mix JavaScript Expression Engine and field value escape.

Example:

yaml
plans:
  my-plan:
    my-entity:
      - update:
          filter-expression: "id < 10"
          data:
            display_name: $> first_name + ' ' + last_name # <-- escape field value
            unique_id: $>  last_name + '-${{ $utils.newUuid() }}' # <-- mixed usage

ℹ️ NOTE

This funtionnality is only available for data updates such as Metal REST API PATCH or plan update command.

Released under the GNU v3 License.