The backend behind a D2C organic grocery store runs on NestJS, PostgreSQL and Sequelize. Its baseline migration creates 47 tables, tied together by 76 foreign keys and 28 unique constraints. That sounds like over-engineering until you list what a real grocery business has to track. This post walks through the parts that mattered most: stock across warehouses, the reservation ledger, discounts and price history, and the order trail.
Start from the questions the business asks
Before drawing tables, I wrote down the questions operations needed answered without a spreadsheet:
- How much of this product can we sell right now, and from which warehouse?
- What does this item cost today, and who changed the price?
- Where is this order, and what happened to it, in what order?
Most of the schema exists to answer one of those. The rest is plumbing every product needs (sessions, roles, audit logs, join tables), and that's fine.
Stock lives per warehouse, per variant
The same product can sit in several warehouses, and "1 kg" and "500 g" are different stock. So stock is a row per variant per warehouse (inventories), each with an available quantity and a reserved quantity.
Every stock movement goes through a ledger
The important decision was never to change stock silently. An order moves stock through three steps, and each one writes an inventory_transactions row against the exact warehouse row it touched:
- Reserve at checkout. Units move from available to reserved.
- Consume on delivery. The reserved units have left the warehouse.
- Release on cancellation. The reserved units go back to available.
Because each reservation records the row it came from, a cancellation releases stock to that same warehouse, and the ledger can always explain the current numbers.
Reservation is where concurrency bites: two checkouts can both see "3 left". So the reservation reads the inventory row with SELECT … FOR UPDATE inside the order's transaction, re-checks the quantity under that lock, and only then moves units to reserved:
const row = await this.model.findOne({
where: { productId, productVariantId: variantId, availableQuantity: { [Op.gte]: quantity } },
order: [["availableQuantity", "DESC"]], // the warehouse with the most stock
transaction: tx,
lock: tx.LOCK.UPDATE,
});
if (!row) return null; // no single warehouse can fill this line
await row.update(
{ availableQuantity: row.availableQuantity - quantity, reservedQuantity: row.reservedQuantity + quantity },
{ transaction: tx },
);
A second checkout for the same row waits for the first to commit, then sees the real remaining quantity.
Prices and discounts are data, with history
Discounts change weekly, and hard-coding them would mean a deploy per promotion. So discounts are rows scoped to a product or variant, with a type, a value and a validity window. Coupons are separate, and every redemption is recorded in coupon_usages, so limits are counted from data rather than remembered. Every price change writes a price_histories row with the old price, the new price and who changed it, which settles "why was this cheaper last Tuesday?" in one query.
The order keeps its own history
An order's status moves through pending, confirmed, processing, shipped and delivered, with cancelled and refunded on the side. Every change also writes an order_status_histories row, so "where is it, and who touched it last" has an answer, not just a current value.
Integrity: what the database enforces, and what it doesn't
Sequelize makes it tempting to keep every rule in application code. The schema pushes the structural ones into PostgreSQL:
- Foreign keys on relations, so an order line can't point at a product that doesn't exist.
- Unique constraints on natural keys like slugs and codes.
- Soft deletes on the models, so historical orders never point at a missing row.
The database only enforces the invariants you actually encode in it. Quantities never going negative is currently guaranteed by the service, under the row lock above, not by the database. Adding CHECK (available_quantity >= 0) is the next step, so that a bug elsewhere can't break that rule either.
Was 47 too many?
No: each core table answers a question someone actually asks. The ones I'd defend hardest are the inventory ledger, price history and order status history. Current values tell you what is true now; those tables tell you how it got that way, and that's what makes a store's numbers trustworthy to the people running it.
- #Database
- #Backend