Products · Upload V1

Document upload

Upload a file from the browser straight to a private AWS S3 bucket.

Version 1 · Files to AWS S3Designed, not builtModule 3 of 15

DataSchemaSections

Schema Designed

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

-- Upload service, version 1. Schema: upload.
-- 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 upload;

create table upload.files (
  id               uuid        primary key,
  owner_id         uuid        not null,
  s3_key           text        not null,
  file_name        text        not null,
  mime             text        not null,
  size_bytes       bigint      not null,
  pages            integer,
  accept           jsonb       not null default '{}',
  status           text        not null default 'pending_upload',
  rejection_reason text,
  sha256           text,
  created_at       timestamptz not null default now(),
  completed_at     timestamptz,
  deleted_at       timestamptz,
  expires_at       timestamptz not null,

  constraint files_s3_key_key unique (s3_key),
  constraint files_s3_key_check
    check (s3_key like 'users/' || owner_id::text || '/' || id::text || '/original.%'),
  constraint files_file_name_check
    check (length(file_name) between 1 and 255 and file_name !~ '[/\\[:cntrl:]]'),
  constraint files_mime_check
    check (mime in ('application/pdf', 'image/png', 'image/jpeg', 'application/json', 'text/csv')),
  constraint files_size_bytes_check check (size_bytes between 1 and 10485760),
  constraint files_pages_check check (
    pages is null
    or (mime = 'application/pdf' and pages between 1 and 10)
    or (mime in ('image/png', 'image/jpeg') and pages = 1)
  ),
  constraint files_accept_check
    check (accept - array['mimes', 'max_bytes', 'max_pages'] = '{}'::jsonb),
  constraint files_status_check
    check (status in ('pending_upload', 'ready', 'rejected', 'deleted', 'expired')),
  constraint files_rejection_check check (
    (status = 'rejected') = (rejection_reason is not null)
    and (rejection_reason is null
         or rejection_reason in ('type_mismatch', 'too_many_pages', 'unreadable', 'size_mismatch'))
  ),
  constraint files_ready_check check (
    status <> 'ready'
    or (sha256 is not null and completed_at is not null
        and (pages is not null or mime in ('application/json', 'text/csv')))
  ),
  constraint files_sha256_check check (sha256 ~ '^[0-9a-f]{64}$'),
  constraint files_deleted_check check ((status = 'deleted') = (deleted_at is not null)),
  constraint files_dates_check check (
    (completed_at is null or completed_at >= created_at)
    and expires_at = created_at + interval '30 days'
  )
);

-- A user's files, newest first, page by page.
create index files_owner_id_idx on upload.files (owner_id, created_at desc, id desc);
-- The daily clean-up of uploads that never arrived.
create index files_pending_idx on upload.files (created_at) where status = 'pending_upload';
-- Marking files expired when the bucket removes them.
create index files_expiry_idx on upload.files (expires_at) where status = 'ready';

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

  constraint inbound_events_type_check check (length(type) between 1 and 100)
);

-- The service's database login reaches only this schema. Rows are never
-- deleted (a deleted file keeps its row, without content), and audit events
-- can be added and read but never changed.
grant usage on schema upload to upload_service;
grant select, insert, update on upload.files to upload_service;
grant select, insert on upload.inbound_events to upload_service;
grant select, insert on upload.audit_events to upload_service;

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

  • Primary keys on both tables; s3_key is unique, so two files can never share an object.
  • The storage key must be built from the owner and file IDs.
  • Type, size, page count, status and rejection reason accept only their listed values or ranges.
  • A rejection reason exists exactly when a file is rejected; a deleted date exists exactly when it is deleted.
  • A ready file always has its fingerprint and completion time.
  • expires_at is always exactly 30 days after created_at, matching the bucket's lifecycle rule.
  • Rows are never deleted: the service's login has no DELETE grant. Audit events can be added and read, never changed.
  • Upload's database login can reach only the upload schema. No other service can read these tables.

Constraints and indexes by table

upload.files
  • files_pkey: primary key (id)
  • files_s3_key_key: unique (s3_key)
  • files_s3_key_check: s3_key starts with users/{owner_id}/{id}/original.
  • files_file_name_check: 1 to 255 characters, none of them /, \ or a control character
  • files_mime_check: mime is one of the five allowed types
  • files_size_bytes_check: size_bytes between 1 and 10,485,760
  • files_pages_check: pages empty, or 1 to 10 for a PDF and exactly 1 for an image; always empty for JSON and CSV
  • files_accept_check: accept has no keys other than mimes, max_bytes, max_pages
  • files_status_check: status is one of the five statuses
  • files_rejection_check: rejection_reason is set exactly when status is rejected, and is one of the four reasons
  • files_ready_check: a ready file has sha256, completed_at and, unless it is JSON or CSV, pages
  • files_sha256_check: sha256 is 64 lower-case hex characters
  • files_deleted_check: deleted_at is set exactly when status is deleted
  • files_dates_check: completed_at is not before created_at; expires_at is exactly 30 days after created_at
  • files_owner_id_idx: index on (owner_id, created_at desc, id desc), for listing a user's files page by page
  • files_pending_idx: partial index on (created_at) where status = 'pending_upload', for the daily clean-up
  • files_expiry_idx: partial index on (expires_at) where status = 'ready', for marking files expired
upload.inbound_events
  • inbound_events_pkey: primary key (event_id); inserting a duplicate fails, which is how a repeat is detected
  • inbound_events_type_check: type is 1 to 100 characters

Sample data

upload.files

idowner_ids3_keyfile_namemimesize_bytespagesstatusrejection_reasonsha256created_atcompleted_atexpires_at
0193b0c4-2a10-7e55-8d31-6c7d8e9f0a1b0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3eusers/0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e/0193b0c4-2a10-7e55-8d31-6c7d8e9f0a1b/original.pdfacme-invoice-0042.pdfapplication/pdf2489132ready(empty)3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b2026-11-17 09:14:07+002026-11-17 09:14:11+002026-12-17 09:14:07+00
0193b0c9-5f22-7a08-9e44-1a2b3c4d5e6f0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3eusers/0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e/0193b0c9-5f22-7a08-9e44-1a2b3c4d5e6f/original.jpgscan.jpgimage/jpeg512000(empty)rejectedtype_mismatch(empty)2026-11-17 09:16:40+002026-11-17 09:16:44+002026-12-17 09:16:40+00
0193b1d0-0c3e-7b19-a2f5-7e8f9a0b1c2d0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3eusers/0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e/0193b1d0-0c3e-7b19-a2f5-7e8f9a0b1c2d/original.jpgreceipt-photo.jpgimage/jpeg1843200(empty)pending_upload(empty)(empty)2026-11-17 09:20:30+00(empty)2026-12-17 09:20:30+00

upload.inbound_events

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

Common queries

List a user's files, one page (`GET /files`)

select id, file_name, mime, size_bytes, pages, status, created_at
from upload.files
where owner_id = $1
  and status in ('pending_upload', 'ready', 'rejected')
  and (created_at, id) < ($2, $3)      -- from the cursor; omitted on the first page
order by created_at desc, id desc
limit $4;

Mark a checked file ready (`POST /files/{fileId}/complete`)

update upload.files
set status = 'ready', pages = $3, sha256 = $4, completed_at = now()
where id = $1 and owner_id = $2 and status = 'pending_upload'
returning *;     -- no row: already checked, or not this user's file

Find abandoned uploads (daily clean-up)

select id, s3_key
from upload.files
where status = 'pending_upload'
  and created_at < now() - interval '24 hours';