Products · DocFlow app

JSON and CSV output

Preview the final JSON, metadata and CSV before paying.

Version 1 · The app that connects the servicesDesigned, not builtModule 6 of 15

DataSchemaSections

Schema Designed

The SQL for the DocFlow app's tables, in the docflow schema. No service can read them.

-- DocFlow app, version 1. Schema: docflow.
-- Checked with `npm run schema:check`: runs on PGlite, and every sample row passes every constraint.

create schema if not exists docflow;

create table docflow.documents (
  id               uuid        primary key,
  owner_id         uuid        not null,
  file_name        text        not null,
  status           text        not null default 'pending_upload',
  file_id          uuid        not null,
  job_id           uuid,
  review_task_id   uuid,
  order_id         uuid,
  rejection_reason text,
  created_at       timestamptz not null default now(),
  updated_at       timestamptz not null default now(),

  constraint documents_file_name_check check (length(file_name) between 1 and 255),
  constraint documents_status_check check (status in ('pending_upload', 'uploaded', 'rejected',
    'extracting', 'extraction_failed', 'needs_review', 'reviewed', 'paid')),
  constraint documents_parts_check check (
    (status not in ('extracting', 'extraction_failed', 'needs_review', 'reviewed', 'paid') or job_id is not null)
    and (status not in ('needs_review', 'reviewed', 'paid') or review_task_id is not null)
    and (status <> 'paid' or order_id is not null)
  ),
  constraint documents_rejected_check check ((status = 'rejected') = (rejection_reason is not null))
);

create index documents_owner_idx on docflow.documents (owner_id, created_at desc);

create table docflow.inbound_events (
  event_id   uuid        primary key,
  type       text        not null,
  created_at timestamptz not null default now()
);

grant usage on schema docflow to docflow_service;
grant select, insert, update, delete on docflow.documents to docflow_service;
grant select, insert on docflow.inbound_events, docflow.audit_events to docflow_service;

Schema level: enforced by the database, whatever the code does

  • A document's status always has the parts it needs (a job once extracting, a task once in review, an order once paid).

Constraints and indexes by table

docflow.documents
  • documents_pkey: primary key (id)
  • documents_status_check: only the eight statuses
  • documents_parts_check: a job exists from extracting on; a review task from needs_review on; an order when paid
  • documents_rejected_check: rejection_reason exactly when rejected
  • documents_owner_idx: index on (owner_id, created_at desc), for the documents list
docflow.inbound_events
  • inbound_events_pkey: primary key (event_id)

Sample data

docflow.documents

idowner_idfile_namestatusfile_idjob_idreview_task_idorder_idrejection_reasoncreated_atupdated_at
0193b0c4-91aa-7b33-8c44-5d6e7f8a9b0c0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3eacme-invoice-0042.pdfpaid0193b0c4-2a10-7e55-8d31-6c7d8e9f0a1b0193b0d1-4b2c-7d10-9a8b-1c2d3e4f5a6b0193b0f2-3c4d-7e5f-8a6b-7c8d9e0f1a2b0193b120-5a6b-7c7d-8e8f-9a0b1c2d3e4f(empty)2026-11-17 09:14:07+002026-11-17 09:17:03+00
0193b160-aaaa-7bbb-8ccc-dddddddddddd0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3escan.jpgrejected0193b0c9-5f22-7a08-9e44-1a2b3c4d5e6f(empty)(empty)(empty)type_mismatch2026-11-17 09:16:40+002026-11-17 09:16:44+00

docflow.inbound_events

event_idtypecreated_at
0193b121-0000-7000-8000-00000000aaaapayment.order.paid2026-11-17 09:17:03+00

Common queries

The documents list

select * from docflow.documents
where owner_id = $1
order by created_at desc
limit $2;