Products · Delivery V1

Email delivery

Get the files immediately by secure download, with a copy by email.

Version 1 · Email through ResendDesigned, not builtModule 8 of 15

DataSchemaSections

Schema Designed

The SQL for the Delivery service's tables, in the delivery schema. No other service can read them.

-- Delivery service, version 1. Schema: delivery.
-- IDs are UUIDv7 values generated by the service.
-- Checked with `npm run schema:check`: runs on PGlite, and every sample row passes every constraint.

create schema if not exists delivery;

create table delivery.messages (
  id                  uuid        primary key,
  owner_id            uuid        not null,
  app                 text        not null,
  reference           text        not null,
  idempotency_key     text        not null,
  channel             text        not null default 'email',
  template            text        not null,
  status              text        not null default 'queued',
  attempts            smallint    not null default 0,
  provider_message_id text,
  last_error          text,
  sent_at             timestamptz,
  created_at          timestamptz not null default now(),

  constraint messages_idempotency_key unique (app, idempotency_key),
  constraint messages_text_check check (
    length(app) between 1 and 50 and length(reference) between 1 and 200
    and length(idempotency_key) between 1 and 255
  ),
  constraint messages_values_check check (
    channel in ('email') and template in ('order_paid')
    and status in ('queued', 'sent', 'failed', 'cancelled')
    and attempts between 0 and 4
  ),
  constraint messages_sent_check check (
    (status = 'sent') = (sent_at is not null)
    and (sent_at is null) = (provider_message_id is null)
  ),
  constraint messages_failed_check check (status <> 'failed' or last_error is not null)
);

create index messages_owner_idx on delivery.messages (owner_id, created_at desc);
create index messages_queued_idx on delivery.messages (created_at) where status = 'queued';

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

-- The service's database login reaches only this schema.
grant usage on schema delivery to delivery_service;
grant select, insert, update, delete on delivery.messages to delivery_service;
grant select, insert on delivery.inbound_events, delivery.audit_events to delivery_service;

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

  • One message per app and idempotency key.
  • A sent message always has Resend's ID and a time; a failed one always has a reason.
  • Delivery's database login reaches only the delivery schema.

Constraints and indexes by table

delivery.messages
  • messages_pkey: primary key (id)
  • messages_idempotency_key: unique (app, idempotency_key), so the same request never sends twice
  • messages_values_check: channel, template and status only the listed values; attempts 0 to 4
  • messages_sent_check: sent_at and provider_message_id are set exactly when status is sent
  • messages_failed_check: last_error is set when status is failed
  • messages_owner_idx: index on (owner_id, created_at desc), for a person's messages
  • messages_queued_idx: partial index on (created_at) where status = 'queued', for the sender
delivery.inbound_events
  • inbound_events_pkey: primary key (event_id)

Sample data

delivery.messages

idowner_idappreferenceidempotency_keychanneltemplatestatusattemptsprovider_message_idlast_errorsent_atcreated_at
0193b140-2b3c-7d4e-8f50-6172839a0b1c0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3edocflowdocflow:0193b0c4-91aa-7b33-8c44-5d6e7f8a9b0cevt_order_paid_0193b120emailorder_paidsent1re_8f1c2a3b4c5d(empty)2026-11-17 09:17:09+002026-11-17 09:17:04+00
0193b150-0011-7223-8445-566778899aab0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3edocflowdocflow:0193b129-0000-7000-8000-000000000002evt_order_paid_0193b131emailorder_paidfailed4(empty)Mailbox unavailable(empty)2026-11-17 11:02:00+00

delivery.inbound_events

event_idtypecreated_at
0193b2aa-6f01-7c2d-9e3f-4a5b6c7d8e9fidentity.user.deleted2026-11-17 10:00:01+00

Common queries

Send once (`POST /messages`)

insert into delivery.messages (id, owner_id, app, reference, idempotency_key, template)
values ($1, $2, $3, $4, $5, $6)
on conflict (app, idempotency_key) do nothing
returning *;  -- no row: already accepted, return the existing message