Build the backend that turns customer orders into pick tasks for warehouse workers: grouping orders into waves by carrier cutoff, ordering picks along the aisle path, claiming tasks without duplicates, confirming scans, handling short picks and updating inventory. In TypeScript on Postgres.

A warehouse worker with a handheld scanner wants one thing from the backend: the next item to pick, in an order that does not send them across the building and back. Orders, waves and inventory exist to produce that list correctly.
This guide builds the picking backend in TypeScript on Postgres. It covers how orders become waves, how pick tasks are ordered and claimed, and how scans and short picks update inventory.
An order has lines, each a SKU and quantity, and allocation finds a location with stock for each line and creates a pick task: SKU, location, quantity and the order it serves. Tasks are grouped into a wave, a batch released to the floor together. Warehouses usually cut waves by carrier cutoff, such as everything for the 16:00 pickup released at 13:00, or by zone or priority.
Sort tasks by a location code that follows the walking route, for example aisle-bay-level padded to fixed width, so that sorting the text sorts the path. Snake through aisles by reversing bay order on alternating aisles if your layout calls for it. A simple sorted path gets most of the benefit, and routing algorithms are an optimization once the basics work.
Locations and products are usually scanned as GS1 codes or internal labels, and the system should reject a scan that does not match the task's location or SKU instead of trusting the picker, since the barcode is what confirms the task.
When several pickers ask for work at the same moment, claim the next task atomically with FOR UPDATE SKIP LOCKED, which lets each transaction lock a different row without waiting, as described in the Postgres documentation:
UPDATE pick_tasks SET picker = $1, status = 'claimed'
WHERE id = (
SELECT id FROM pick_tasks
WHERE wave_id = $2 AND status = 'open'
ORDER BY path_code
LIMIT 1
FOR UPDATE SKIP LOCKED)
RETURNING id, sku, location, quantity;
Back the claim query with a partial index on open tasks, as in the schema below, and release claimed tasks back to open if a picker logs out or times out, so work never gets stranded.
Confirming a task records the quantity picked and the scans. If the picker finds fewer units than asked, record the quantity found, mark the task short, correct the location's stock count and raise a cycle-count task for that location, since a short usually means the system's count was wrong. The order line then goes back to allocation, which tries another location or backorders the remainder. Inventory changes in the same transaction as the task confirmation, so counts and picks cannot disagree.
Encore is a backend framework with built-in infrastructure. The picking service declares its Postgres database and a cron job that releases waves in TypeScript, and Encore provisions them on AWS or GCP in your own account on deploy, with encore run starting Postgres locally.
Run in your terminal to get started locally.
-- picking/migrations/1_create_tables.up.sql
CREATE TABLE stock (
sku TEXT NOT NULL,
location TEXT NOT NULL,
units INT NOT NULL CHECK (units >= 0),
PRIMARY KEY (sku, location)
);
CREATE TABLE waves (
id BIGSERIAL PRIMARY KEY,
cutoff TIMESTAMPTZ NOT NULL,
status TEXT NOT NULL DEFAULT 'planned' CHECK (status IN ('planned', 'released', 'done')),
released_at TIMESTAMPTZ
);
CREATE TABLE pick_tasks (
id BIGSERIAL PRIMARY KEY,
wave_id BIGINT NOT NULL REFERENCES waves(id),
order_id BIGINT NOT NULL,
sku TEXT NOT NULL,
location TEXT NOT NULL,
path_code TEXT NOT NULL,
quantity INT NOT NULL CHECK (quantity > 0),
picked INT,
status TEXT NOT NULL DEFAULT 'planned' CHECK (status IN ('planned', 'open', 'claimed', 'done', 'short')),
picker TEXT
);
CREATE INDEX pick_tasks_open ON pick_tasks (wave_id, path_code) WHERE status = 'open';
// picking/picking.ts
import { api, APIError } from "encore.dev/api";
import { SQLDatabase } from "encore.dev/storage/sqldb";
import { CronJob } from "encore.dev/cron";
import log from "encore.dev/log";
const db = new SQLDatabase("picking", { migrations: "./migrations" });
interface Task {
id: number;
sku: string;
location: string;
quantity: number;
}
// Releases planned waves whose carrier cutoff is within the next three hours.
export const releaseWaves = api({ expose: false }, async (): Promise<{ released: number }> => {
await using tx = await db.begin();
const waves: number[] = [];
for await (const w of tx.query<{ id: number }>`
UPDATE waves SET status = 'released', released_at = now()
WHERE status = 'planned' AND cutoff <= now() + interval '3 hours'
RETURNING id`) {
waves.push(w.id);
}
for (const id of waves) {
await tx.exec`UPDATE pick_tasks SET status = 'open' WHERE wave_id = ${id} AND status = 'planned'`;
}
await tx.commit();
log.info("waves released", { count: waves.length });
return { released: waves.length };
});
const _release = new CronJob("release-waves", {
title: "Release waves before cutoff",
every: "10m",
endpoint: releaseWaves,
});
export const next = api(
{ method: "POST", path: "/waves/:waveId/next", expose: true },
async ({ waveId, picker }: { waveId: number; picker: string }): Promise<{ task: Task | null }> => {
const task = await db.queryRow<Task>`
UPDATE pick_tasks SET picker = ${picker}, status = 'claimed'
WHERE id = (
SELECT id FROM pick_tasks
WHERE wave_id = ${waveId} AND status = 'open'
ORDER BY path_code LIMIT 1
FOR UPDATE SKIP LOCKED)
RETURNING id, sku, location, quantity`;
return { task };
},
);
export const confirm = api(
{ method: "POST", path: "/tasks/:taskId/confirm", expose: true },
async ({ taskId, picked, scannedLocation, scannedSku }: {
taskId: number; picked: number; scannedLocation: string; scannedSku: string;
}): Promise<{ status: string }> => {
await using tx = await db.begin();
const t = await tx.queryRow<{ sku: string; location: string; quantity: number }>`
SELECT sku, location, quantity FROM pick_tasks WHERE id = ${taskId} AND status = 'claimed' FOR UPDATE`;
if (!t) throw APIError.failedPrecondition("task is not claimed");
if (scannedLocation !== t.location || scannedSku !== t.sku) {
throw APIError.invalidArgument("scan does not match the task");
}
if (picked < 0 || picked > t.quantity) throw APIError.invalidArgument("invalid quantity");
const status = picked === t.quantity ? "done" : "short";
await tx.exec`UPDATE pick_tasks SET status = ${status}, picked = ${picked} WHERE id = ${taskId}`;
if (status === "done") {
await tx.exec`UPDATE stock SET units = units - ${picked} WHERE sku = ${t.sku} AND location = ${t.location}`;
} else {
// A short pick means the count was wrong: set the location to what was found, then reallocate the line.
await tx.exec`UPDATE stock SET units = 0 WHERE sku = ${t.sku} AND location = ${t.location}`;
}
await tx.commit();
return { status };
},
);
SKIP LOCKED makes each next call take a different task, and the scan check refuses mismatches before any stock changes. Encore cron jobs run in cloud environments and not under encore run or in preview environments, so call releaseWaves from the local development dashboard when testing. The short-pick branch zeroes the location as a simple rule for the example, and a real system raises a cycle-count task and reallocates the order line instead. Each scan request appears in a trace, which helps when a handheld is slow on a particular call.
A coding agent can add a zone filter to the claim query and its test in the same change, then run several simulated pickers against the local database.
Best fit: Logistics teams building warehouse tooling in TypeScript who want waves, tasks and inventory in one Postgres-backed service in their own AWS or GCP account.
Wave picking groups customer orders into batches, called waves, that are released to the warehouse floor together, usually based on carrier cutoff times, order priority or zone. Releasing work in waves lets a warehouse balance workload and make sure orders are ready before the truck leaves.
Claim tasks with an UPDATE that selects the next open task using FOR UPDATE SKIP LOCKED and sets the picker ID, returning the claimed row. Concurrent pickers lock different rows, so each task goes to exactly one person without waiting.
A short pick happens when the picker finds fewer units than the task asks for. Record the quantity picked, mark the task short, adjust the location's inventory count and let the order flow decide whether to reallocate from another location, backorder or cancel the line.
Yes. A TypeScript service declares the Postgres database for orders, tasks and inventory and a cron job that releases waves before carrier cutoffs, and Encore provisions them in your AWS or GCP account. Postgres runs locally under encore run, while cron jobs run only in cloud environments, so call the wave endpoint by hand when testing.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.