Form Engine (Fieldwork)
Full-stack SaaS form builder featuring a visual drag-and-drop canvas, atomic two-phase field index reordering, httpOnly cookie sessions, and unified tRPC/OpenAPI surfaces.

Project Overview
Form Engine (shipped in the UI under the product name Fieldwork) is a full-stack form creation and response-collection platform built within a clean Turborepo monorepo.
The product treats the visual builder, publishing gate, respondent experience, and response analytics as a single continuous workflow rather than isolated CRUD screens.
Architected with Next.js 16 (App Router), React 19, Express 5, tRPC, PostgreSQL, and Drizzle ORM, with absolute boundary separation between user interface, services, and database layers.
The Problem & Motivation
- Most form builders separate form configuration, public sharing, and submission inspection across disconnected views, forcing users through jarring context switches.
- Traditional form database schemas often rely on naive sequential integer indexing, which causes expensive re-indexing writes and race conditions during drag-and-drop operations.
- The engineering objective was to build an end-to-end type-safe TypeScript monorepo that delivers an instant keyboard/drag-and-drop editing canvas with strict request-level ownership authorization and zero client-side credential exposure.
Architectural Solution
- Engineered a pnpm/Turborepo workspace where apps/web (Next.js 16) and apps/api (Express 5) share typed packages for database, services, logger, and tRPC contracts without leaking SQL or private secrets to the client.
- Implemented a dual-surface API router: one tRPC router mounted for direct type-safe client RPCs and compiled via trpc-to-openapi into an OpenAPI REST specification with interactive Scalar API documentation at /docs.
- Designed a two-phase transaction-safe field reordering algorithm that parks rows on negative temporary indices to respect unique PostgreSQL constraints during rapid drag reordering.
- Enforced httpOnly cookie sessions, request-level ownership checks (IDOR protection), formula injection-resistant client-side CSV exports, and HMAC-SHA256 password digests with per-user 16-byte random salts (chosen to eliminate native build dependencies, with the explicit disclosure that HMAC-SHA256 is a fast cryptographic digest and NOT a memory-hard password hashing function like Argon2id or bcrypt; in high-threat enterprise deployments, a migration to a memory-hard KDF would be required).
Technology Stack & Evidence
Technologies categorized by source verification and documented implementation scope:
Frontend & UI Architecture
Backend & API Engine
Data Layer & Persistence
Monorepo & Tooling
Architecture & System Flow
Web Application (apps/web)
Responsive dashboard, drag-and-drop form canvas, and lightweight public respondent route (/form/[id]) consuming the shared tRPC client.
API Server & Routing (apps/api & packages/trpc)
Mounts both /trpc and /api OpenAPI endpoints, validates request payloads via Zod, and enforces authentication and IDOR ownership checks.
Data Access & Business Logic (packages/services)
Encapsulated service layer managing form lifecycle, atomic reordering transactions, duplication, and response queries without exposing SQL to the web tier.
Persistence & Monorepo Infrastructure
Normalized relational schema with foreign key integrity, migration tracking, and pnpm workspace isolation.
Core Implemented Features
Visual Drag-and-Drop & Keyboard Builder
Palette of five field types (short text, number, email, yes/no, password) with live canvas previews, optimistic local ordering, and full keyboard navigation (arrows, 1-5 shortcuts, duplicate).
Dual tRPC & OpenAPI REST Surfaces
Single tRPC server router mounted both as an end-to-end typed RPC endpoint for Next.js and as a public REST API with generated OpenAPI JSON and Scalar docs UI.
Two-Phase Atomic Field Reordering
Reorders form fields inside a database transaction by parking rows on negative indices, completely avoiding UNIQUE(form_id, index) constraint collisions.
Secure Response Explorer & Sanitized CSV Export
Master-detail response inspector with full-text answer search, masked password fields, and client-side CSV serialization that neutralizes spreadsheet formula injection (=, +, -, @).
Technical Challenges & Trade-Offs
01.Avoiding Unique Constraint Collisions During Field Reordering
Form fields enforce a strict UNIQUE(form_id, index) database constraint. In-place reordering permutations directly update indices, which throws unique constraint violation errors when swapping adjacent items.
Engineered a two-phase reordering transaction: phase 1 shifts target field indices to temporary negative values (-1, -2, etc.); phase 2 assigns the final zero-based positive sequence.
Reliable in-transaction index swapping without UNIQUE constraint collision errors during rapid drag-and-drop or keyboard reordering.
02.Exposing Dual API Surfaces from a Single Router
Maintaining separate REST and RPC endpoints leads to duplicate validation schemas, documentation drift, and fragmented auth middleware.
Integrated trpc-to-openapi with the root tRPC router in Express, generating an OpenAPI 3.0 specification and interactive Scalar UI at /docs from the exact same schema definitions.
End-to-end type-safe internal RPC calls for the web client while effortlessly providing standard REST documentation for external consumers.
03.Preventing IDOR & Leaking Unpublished Schemas
Draft forms must remain strictly confidential to the creator, while public respondent links (/form/[id]) must allow submission only when explicitly published.
Separated public and authenticated procedure paths. getForm checks creator ownership: unauthenticated requests for draft forms receive only an empty shell, and submitForm rejects any non-PUBLISHED form.
Strict separation between private draft configurations and published respondent experiences, reducing unauthorized field discovery.