Queries

The queries property allows you to define Just-In-Time (JIT) queries that are executed when a function runs. These queries retrieve local data from the SQLite database at the moment the function executes, ensuring that decisions and operations are based on the most current information available.

Purpose and benefits

  • Ensures accuracy by querying local tables at runtime.

  • Reduces stale data by removing the need to store older data in the Command Queue.

  • Supports offline use by relying on local data, not remote API calls.

  • Improves flexibility by allowing conditional logic or table operations to be based on the current state.

How queries work

  • Queries are evaluated at the time the function executes (when it comes off the command queue).

  • They are re-evaluated on continuation unless onContinuation: false is explicitly set.

  • Results of the queries are available in expressions using: [email protected].{query-name}

Result types

The resultType property lets you control the format of the returned data:

Type
Description
Returned value

records (default)

Returns all matching records

Array of objects

record

Returns the first matching record

Single object

scalar

Returns a single value from the first column of the first record

Single value (string, number, etc.)

If no records match the query:

  • records returns an empty array []

  • record and scalar return null

In this example:

  • current-record uses a parameter to fetch a specific country from the local table.

  • existing-countries fetches all records from the local/countries table.

Queries
# In the function definition.
queries:
  current-record:
    statement: SELECT * FROM [local/countries] WHERE [id] = @id
    parameters:
      id: [email protected]
  existing-countries:
    statement: SELECT * FROM [local/countries]

You can access the results in other parts of the function like this:

YAML
records: [email protected]

Queries properties

Properties
Description

dependencies

The queries that this query depends on. They will be executed before this query. Dependencies allow you to stop recursive queries, because queries can be inputs to each other.

jsonColumns

The columns that are automatically parsed as JSON string. Defaults to no JSON columns to parse.

onContinuation

Set to false re-runs the query on each continuation of the function. Defaults to true, so the query only runs initially by default and not again on each continuation.

parameters

Named parameters used in the query statement. They are referenced using the @ prefix (e.g., @id). If not prefixed manually, the @ is automatically added. Defaults to no parameters.

resultType

Defines the expected result format. Can be one of: records (default), record, or scalar.

statement

The SQL query statement to execute.

tables

The tables to use in the query. Defaults to none. These tables are validated to exist before the query is executed.

Last updated

Was this helpful?