sql

Connect to a SQL database, execute a query, and emit each result row as an event. When not wired to any inputs, the query executes once on pipeline start (with {START: true} as the event). When wired to an input, the query executes once per received event.

Supported drivers: postgres, mysql.

Config

Required:

  • driver: Database driver — postgres or mysql
  • connection: Connection URL string (e.g. postgres://user:pass@host/db), or a JS function () => string
  • query: JS function (event) => string — returns the SQL string to execute

Optional: SQL(string) is available as a no-op identity helper inside the query function for compatibility with tagged-template patterns.

Examples

Static query on startup:

pipeline:
  sql:
    config:
      driver: postgres
      connection: "() => 'postgres://' + process.env.DB_URL"
      query: "(event) => 'SELECT * FROM tasks WHERE status = \\'queued\\''"

Query per input event (e.g. from a scheduler):

pipeline:
  scheduler:
    config:
      cron: '*/5 * * * *'
  sql:
    inputs: [scheduler]
    config:
      driver: postgres
      connection: postgres://localhost/mydb
      query: "function(event, db, SQL) { return SQL('SELECT * FROM logs WHERE ts > now() - interval \\'5 minutes\\'') }"

process.env is available inside JS functions for reading environment variables.