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¶
- Org Role
- Used by end users.
- Restricted to their own entities via RLS.
-
Can only mutate edge and flow instances using approved operators.
-
App Role (
app) - Used by the system backend.
- Can access entities across orgs.
- Bypasses RLS.
- 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)¶
- Start Flow
- Initiates one or more flow instances.
- Scoped to an org.
- Creates a flow instance and the first queued edge.
-
Handles rate limiting for flow instances and triggers.
-
Run Edge Instances
- Runs queued edge instances.
- Sets trigger context and performs JSON interpolation.
- Runs as
app(admin function). -
Intended to run periodically as a cron job.
-
Update Edge Instance
- Updates edge instances after execution or when async callbacks return.
- 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 (
completedorfailed), 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.