{"name":"build-inventory-with-supabase","description":"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.","category":"backend","tags":["inventory","supabase","postgres"],"models":["any"],"proven":false,"passing_families":0,"model_testing":"Not yet model-tested; testing Jan 2027","url":"https://theskillvault.ai/skills/build-inventory-with-supabase","api_url":"https://theskillvault.ai/api/skills/build-inventory-with-supabase","raw_url":"https://theskillvault.ai/api/skills/build-inventory-with-supabase/raw","body":"# Build inventory with Supabase\n\nStock has to change in one database statement. Two shoppers can both read \"1 left\" and both buy it if you subtract in the app.\n\nThis 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.\n\n## When to use\n- You sell a limited quantity and must not oversell it.\n- Two checkouts can run at the same time for the same item.\n- You want a hold while the buyer pays, then a final count when payment lands.\n\n## When not to use\n- The goods are made to order and there is no finite shelf count.\n- You only need a product table, not a quantity that can hit zero.\n\n## Steps\n1. 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.\n2. 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.\n3. Turn on row level security. Shoppers may read products. They must not update the counts. Only the server may.\n4. 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\".\n5. Run that update and the order insert in one transaction. If the insert fails, the reserve rolls back with it.\n6. 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.\n7. 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).\n8. Call these statements from server code with the secret key. Do not expose them as a browser RPC.\n9. Show availability as `stock_on_hand - stock_reserved`. Do not trust a count you read earlier in the request.\n\n```sql\nupdate public.products\nset stock_reserved = stock_reserved + $qty\nwhere id = $product\n  and stock_on_hand - stock_reserved >= $qty\nreturning id;\n```\n\n## Gotchas\n- 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.\n- 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.\n- Forgotten releases lock stock forever. Every reserve needs a commit or a release.\n- 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.\n\n## Safety\n- The Supabase secret key bypasses row policies. Keep it in server environment variables, never in a `NEXT_PUBLIC_` value, the browser, or git.\n- Do not log full order payloads or customer addresses in the stock path.\n\n## Portability\nWorks 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.\n\n## Example prompt\n\"Stop two shoppers buying the last unit. Hold stock in Supabase until payment, then commit or release it.\"\n\n## Done when\n- Two parallel reserves for the last unit produce one success and one sold-out result.\n- A retried webhook does not change the counts a second time.\n- A shopper using the public key cannot update `stock_on_hand` or `stock_reserved`.\n\n---\nHouse original by TheSkillVault (CC-BY-4.0).","files":[],"license":"CC-BY-4.0","license_name":"CC BY 4.0","license_url":"https://creativecommons.org/licenses/by/4.0/","attribution":"From TheSkillVault, https://theskillvault.ai, CC BY 4.0","install":{"claude_code":"curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-inventory-with-supabase/raw' -o ~/'.claude/skills/build-inventory-with-supabase/SKILL.md'","cursor":"curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-inventory-with-supabase/raw' -o '.cursor/skills/build-inventory-with-supabase/SKILL.md'","agents":"curl -fsSL --create-dirs 'https://theskillvault.ai/api/skills/build-inventory-with-supabase/raw' -o '.agents/skills/build-inventory-with-supabase/SKILL.md'","mcp_url":"https://theskillvault.ai/mcp","cursor_mcp_deeplink":"cursor://anysphere.cursor-deeplink/mcp/install?name=theskillvault&config=eyJ1cmwiOiJodHRwczovL3RoZXNraWxsdmF1bHQuYWkvbWNwIn0%3D","mcp_config":{"mcpServers":{"theskillvault":{"url":"https://theskillvault.ai/mcp"}}}},"bundles":[],"tested_on":[{"family":"chatgpt","name":"ChatGPT","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"claude","name":"Claude","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"gemini","name":"Gemini","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"grok","name":"Grok","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"llama","name":"Llama","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"deepseek","name":"DeepSeek","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"mistral","name":"Mistral","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"qwen","name":"Qwen","outcome":null,"date":null,"source":null,"counts":false,"stale":false},{"family":"kimi","name":"Kimi K","outcome":null,"date":null,"source":null,"counts":false,"stale":false}],"proven_rule":{"threshold":5,"window_days":365,"computed_at":"2026-11-29T03:42:30.374Z"}}