Backend · Skill

Build inventory with Supabase

build-inventory-with-supabase

Tracks shelf counts in Supabase and refuses a sale that would take stock below zero, in one database update. Use this when the user wants inventory, stock levels, to stop overselling, or a warehouse count that stays correct when two checkouts race.

Works with: Any AI Not yet model-tested; testing Jan 2027

Paste it into ChatGPT, Claude, Gemini or any AI chat, then tell it about your project. How it works

Install in Claude Code, Cursor or Codex (optional)

Only for AI coding tools that read skill folders. Run one line in a terminal, in your project folder. It downloads this skill's text files from TheSkillVault and nothing else; you can read them first with “View raw SKILL.md”.

Claude Code (all your projects)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-inventory-with-supabase/raw' -o ~/'.claude/skills/build-inventory-with-supabase/SKILL.md'

Cursor (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-inventory-with-supabase/raw' -o '.cursor/skills/build-inventory-with-supabase/SKILL.md'

Codex and other Agent Skills tools (this project)

curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-inventory-with-supabase/raw' -o '.agents/skills/build-inventory-with-supabase/SKILL.md'

Or connect the whole library over MCP

Add TheSkillVault to Cursor or add this to your app's MCP settings (read-only; help):

{
  "mcpServers": {
    "theskillvault": {
      "url": "https://theskillvault.ai/mcp"
    }
  }
}

Stock has to change in one database statement. Two shoppers can both read "1 left" and both buy it if you subtract in the app.

This skill is the shelf count and the hold. It does not create the payment. Use add-stripe-checkout for that. For tables, indexes and general row policies, use design-supabase-postgres-schema.

When to use

  • You sell a limited quantity and must not oversell it.
  • Two checkouts can run at the same time for the same item.
  • You want a hold while the buyer pays, then a final count when payment lands.

When not to use

  • The goods are made to order and there is no finite shelf count.
  • You only need a product table, not a quantity that can hit zero.

Steps

  1. Add stock_on_hand and stock_reserved as integers, both not null default 0. Add checks so neither can go below zero, and so reserved never exceeds what is on hand.
  2. Add a stock_moves table: product id, order id, kind (reserve, commit or release), and quantity. Put a unique key on (order_id, kind, product_id) so a retry cannot move stock twice.
  3. Turn on row level security. Shoppers may read products. They must not update the counts. Only the server may.
  4. When checkout starts, reserve in one update. Add the quantity to stock_reserved only when stock_on_hand - stock_reserved is still enough. If the update changes no row, stop and show "sold out".
  5. Run that update and the order insert in one transaction. If the insert fails, the reserve rolls back with it.
  6. On the paid webhook, commit once: subtract the quantity from both stock_on_hand and stock_reserved, and write a commit move. Skip if that move already exists.
  7. On cancel, expiry or a failed payment, write a release move and subtract the same quantity from stock_reserved only. A job can release holds older than the window you chose (15 to 30 minutes is typical).
  8. Call these statements from server code with the secret key. Do not expose them as a browser RPC.
  9. Show availability as stock_on_hand - stock_reserved. Do not trust a count you read earlier in the request.
update public.products
set stock_reserved = stock_reserved + $qty
where id = $product
  and stock_on_hand - stock_reserved >= $qty
returning id;

Gotchas

  • A check constraint does not stop a race. Two reads can both pass, then both write. The where clause on the update is what makes one of them lose.
  • Reserving only after payment lets two buyers pay for one item. Hold stock at the start of checkout if overselling is the bug you are fixing.
  • Forgotten releases lock stock forever. Every reserve needs a commit or a release.
  • Editing the count in the dashboard during a sale will fight the holds. Adjust stock_on_hand in the same transaction style, not with a blind overwrite.

Safety

  • The Supabase secret key bypasses row policies. Keep it in server environment variables, never in a NEXT_PUBLIC_ value, the browser, or git.
  • Do not log full order payloads or customer addresses in the stock path.

Portability

Works with any framework that can open a Postgres transaction. If you use Neon, RDS or a hosted Postgres instead of Supabase, keep the same update. If you use Prisma or Drizzle, run this SQL in a transaction rather than a read-then-write in application code.

Example prompt

"Stop two shoppers buying the last unit. Hold stock in Supabase until payment, then commit or release it."

Done when

  • Two parallel reserves for the last unit produce one success and one sold-out result.
  • A retried webhook does not change the counts a second time.
  • A shopper using the public key cannot update stock_on_hand or stock_reserved.

---

House original by TheSkillVault (CC-BY-4.0).

Licence

Licence: CC BY 4.0. Attribution when you reuse it: From TheSkillVault, https://theskillvault.ai, CC BY 4.0