Skip to content

Implementation Details

This document explains how Skemata is implemented.

👉 It is recommended to read the Conceptual Overview first.


Design Principles

Skemata is a lightweight wrapper around Postgres functions. The reasons for this design are:

  • Reliability: Postgres is mature and stable, whereas greenfield JavaScript code is often less reliable.
  • Performance: Writing logic in PL/pgSQL reduces network round trips and simplifies code.
  • Portability: PL/pgSQL functions can be called from any backend language or environment.
  • Security: Authorization is offloaded to Postgres via Row-Level Security (RLS) and strategic role-based access.
  • Deployment simplicity: Postgres is easy to deploy, and users can choose any deployment strategy.

Note: We avoid writing triggers that mutate data to prevent cascading effects and complexity.


Implementation Execution

  • Most implementations require HTTP requests or other network calls.
  • For this reason, Skemata runs implementations inside Node.js, not inside Postgres.
  • TypeScript is used to provide bindings for SQL functions.

Database Schema and Roles

All Postgres functions, tables, and objects live inside a single schema (placeholder: __schema__).

Roles

  1. Org Role
  2. Used by end users.
  3. Restricted to their own entities via RLS.
  4. Can only mutate edge and flow instances using approved operators.

  5. App Role (app)

  6. Used by the system backend.
  7. Can access entities across orgs.
  8. Bypasses RLS.
  9. Acts as the system’s "superuser."

Operators

Operators are functions that define how orgs or the system interact with flows and edges.

Core Operators (Main Flow Lifecycle)

  1. Start Flow
  2. Initiates one or more flow instances.
  3. Scoped to an org.
  4. Creates a flow instance and the first queued edge.
  5. Handles rate limiting for flow instances and triggers.

  6. Run Edge Instances

  7. Runs queued edge instances.
  8. Sets trigger context and performs JSON interpolation.
  9. Runs as app (admin function).
  10. Intended to run periodically as a cron job.

  11. Update Edge Instance

  12. Updates edge instances after execution or when async callbacks return.
  13. Queues the next edge instances.

Edge Instance State Diagram

stateDiagram
  direction TB
  state creation_state <<choice>>
  state implementation_failed <<choice>>
  state result_state <<choice>>
  state join_state <<join>>
  [*] --> creation_state:Edege instance created
  creation_state --> QUEUED:Has "queued" status
  creation_state --> [*]:Created with status "failed"
  QUEUED --> RUNNING:Save status to "running"
  RUNNING --> implementation_failed:Run implementation handler
  implementation_failed --> FAILED:Implementation handler failed. Save status to failed
  implementation_failed --> result_state:Implementation handler completed
  result_state --> COMPLETED:Implementation returned status "completed". Save status to "completed"
  result_state --> AWAITING_UPDATES:Implementation returned status "awaiting-updates". Save status to "awaiting-updates"
  COMPLETED --> join_state
  AWAITING_UPDATES --> join_state
  FAILED --> [*]:Create next edge instances with status "failed"
  join_state --> [*]:Create next edge instances with status "queued"

Awaiting Updates Edge Instance State Diagram

stateDiagram
  direction TB
  state update_received <<choice>>
   [*] -->  update_received:Receive update with new status and outputs
   update_received --> AWAITING_UPDATES:Queue new edges corresponding to routing keys in outputs
   update_received --> COMPLETED:Queue new edges corresponding to routing keys in outputs
   update_received --> FAILED:Do not queue next edges
   FAILED --> [*]:Edge Instance lifecycle complete
   AWAITING_UPDATES --> [*]:Wait for more updates
   COMPLETED --> [*]:Edge Instance lifecycle complete

Additional Operators

  • Cancel Flow Instance Allows users to cancel an active flow.

  • Flow Completion Handler Once all edge instances are in terminal states (completed or failed), the flow instance is marked as completed (via a cron job).

  • Edge Instance Summary A cron job maintains summaries of edge instances for easier tracking on flow instances. Also via a cron job.