Skip to content

Database Schema Requirements

Ahmed edited this page Mar 16, 2026 · 2 revisions

Database Schema Requirements

EventLens expects a read‑only view (or table) within your PostgreSQL database.

Required Conceptual Columns

Your view must project the following conceptual schema:

  • event_id – Globally unique ID, monotonically increasing per stream (often the PK).
  • aggregate_id – String ID of the aggregate / entity.
  • aggregate_type – Domain type (e.g., ORDER, USER).
  • sequence_number – Version within the aggregate’s stream (1, 2, 3, …).
  • event_type – Logical event type (e.g., ORDER_PLACED).
  • payload – JSON body of the domain event.
  • metadata – JSON with headers, correlation IDs, etc. ({} is fine).
  • timestamp – Event creation time (timestamptz or epoch seconds).
  • global_position – Total ordering across all events (often the same as event_id).

Example Postgres View mapping

If your existing event table has a different shape, create a view like this:

CREATE OR REPLACE VIEW eventlens_events AS
SELECT
    e.id                               AS event_id,
    e.aggregate_id::text               AS aggregate_id,
    e.aggregate_type                   AS aggregate_type,
    e.version                          AS sequence_number,
    e.event_type                       AS event_type,
    e.json_data                        AS payload,
    '{}'::jsonb                        AS metadata,
    COALESCE(
        (e.json_data::jsonb->>'createdDate')::timestamptz,
        e.created_at,
        CURRENT_TIMESTAMP
    )                                  AS timestamp,
    e.id                               AS global_position
FROM your_event_table e;

Key Rules:

  • Keep it read‑only (view only, no triggers).
  • Do not change your existing write model – project into this view.

Return Home

Clone this wiki locally