In progress2025Designer and engineer

Tradeline

Stock numbers that explain themselves, tenants that cannot see each other, billing that never double-charges.

  • Next.js 16
  • Postgres 18
  • Drizzle
  • pg-boss
  • TypeScript
  • 937commits
  • 329SQL migrations
  • 15modules

01

At a glance

Problem
Distributors run stock across branches on spreadsheets. Quantities drift and expired lots ship.
My role
Design and engineering
Timeline
2025 to now, in progress
Team
Solo

02

Architecture

Web app and PWANext.js 16Modular monolith15 modulesStock engineone posting pathBilling planeplans, invoicespg-boss workersfiscal queuePostgres 18forced RLS, ledger

Web app and PWA → Modular monolith. Modular monolith → Stock engine. Modular monolith → Billing plane. Modular monolith → pg-boss workers. Stock engine → Postgres 18. Billing plane → Postgres 18. pg-boss workers → Postgres 18

03

Hard parts

  1. 01

    Tenant isolation

    Problem
    One slip in an action must not expose another business.
    Decision
    Forced row-level security and composite tenant keys, under app permissions.
    Why it holds
    The database is the last wall, so a bug meets it.
  2. 02

    Stock that adds up

    Problem
    Receipts, transfers, counts and returns all change quantity.
    Decision
    One stock engine posts to an append-only, monthly-partitioned ledger with idempotent keys.
    Why it holds
    Every number traces to documents. A fix is a new document.
  3. 03

    No overselling

    Problem
    Two sales can claim the same lot at once.
    Decision
    Atomic reservations, earliest expiry first, locks taken in a fixed order.
    Why it holds
    Expired lots do not ship and deadlocks stay out.
  4. 04

    Billing plane

    Problem
    Invoices cannot have gaps and webhooks arrive twice.
    Decision
    Versioned plans, gap-free numbers, signed and deduplicated Stripe webhooks, dunning.
    Why it holds
    Money events are safe to replay.
  5. 05

    Support access

    Problem
    Support must see a tenant without a standing key.
    Decision
    Audited, time-boxed impersonation that needs two admins to grant.
    Why it holds
    Access is rare, visible and expires.

04

Results

  • 937commits
  • 329SQL migrations
  • 15modules
  • 40written specs

05

Tech stack

  • Next.js 16
  • Postgres 18
  • Drizzle
  • pg-boss
  • TypeScript

06

The product

Screens stay private. This is a schematic of the product.

07

What I'd do next

  • Finish live fiscal submission.
  • Ship counter sale and platform admin.
  • Load-test reservation contention.
Read the full write-up

The problem

Distributors in Pakistan run stock across two to ten branches on spreadsheets and paper. Quantities drift. Expired lots ship. Fiscal invoicing needs a reliable queue.

I am building IMS to replace that. It is a multi-tenant inventory and digital-invoicing system for pharma and FMCG distributors. It is in progress. Live fiscal submission, counter sale and platform admin are still being finished, and I say so rather than round up.

The constraint

Many businesses share one deployment. A mistake in an action must not expose another business. Stock numbers also have to be explainable, so every quantity on screen links to an Explain view that traces the documents behind it.

My rule for each choice below is to write down the requirement it serves and what it costs. I revisit a choice when measured requirements change, not because another stack is fashionable.

Decisions

One Postgres database and a modular monolith

Stock, reservations and documents must commit atomically. One database gives me that. The cost is discipline: module boundaries need care, and database contention has to be measured instead of assumed away.

Authorization plus forced row-level security and composite tenant keys

Every tenant table has forced row-level security, and foreign keys carry the tenant. An action mistake then meets a wall in the database. The cost is that context, grants and migrations need integration tests that use real roles.

An append-only ledger with one stock engine

Receipts, transfers, counts, shipments and returns all post through one stock engine, inside the caller's transaction. Balance changes, cost layers and ledger entries commit together. I store more rows this way. A correction is a new document, and it must check what depends on the thing it corrects.

Decimal strings and centralised rounding

Quantities and money are decimal strings, and rounding lives in one place. That preserves physical quantity and monetary precision. The cost is that charts and inputs must convert carefully, and a display number is not a posting value.

Atomic reservations, earliest expiry first

Reservations are atomic and conditional, and stock is allocated by earliest expiry first. That prevents overselling. The cost is that backorders and the reservation lifecycle become explicit domain work. Locks are taken in a deterministic order, and that ordering needs adversarial tests.

A queue in Postgres, with pg-boss

I chose pg-boss so jobs are durable and tied to the business transaction, without Redis. The database carries the queue load, and a worker and its schedules have to be running.

A fiscal queue that starts in a sandbox and fails closed

The tax integration runs against a sandbox provider first, and a provider the system does not support is refused. That lets me exercise tax, queue, documents and screens before live credentials exist. The cost: the real provider mapping is unfinished, and credentials alone will not finish it.

A scan PWA with a scoped offline queue

A queue of scan intents lets warehouse work continue through Wi-Fi interruptions. It is not a general offline database. Ownership, expiry and replay failures need to be visible to the user.

Result

The repository stands at 937 commits, 329 SQL migrations, 1,865 source files, 179 pages and endpoints, and 15 modules. Behind it sit 40 specs and 50 plans.

In source, the stock engine, Explain, receiving, transfers, counts, sales orders, reservations, payments and the scan PWA are built. Invoicing and the fiscal queue are built against a sandbox mock only.

The stack is Next.js 16, Postgres 18 and Drizzle, with argon2id, TOTP, OIDC and Turnstile on the auth side.

What is not done: live fiscal submission, counter sale and platform admin. Built in source is not the same as accepted in production, and I make no production claim.

What I would change

This section is opinion. If I rebuilt it, I would write the row-level security integration tests before the first tenant table, not alongside it.

I would also start the live fiscal work earlier. A sandbox proves the queue. It cannot settle the real provider's mapping, and that is the biggest open item.

I would also check specs against source on a schedule, so the documents cannot drift from the code.

Need a system like this?

I build systems like this for founders and teams.

Tell me what you're building