Build the store database on your computer
Create a local PostgreSQL database, define the store tables with Drizzle, run migrations and load safe sample data.
You will have: A local database named shop_local with the tables for categories, products, variants, customers, orders, order items, payments and settings, plus sample products you can see.
Before you start
- Finish Episode 5 so your code is on GitHub and you work on branches
- PostgreSQL running on your computer, with the administrator password saved in your password manager (Episode 2)
- Your PROJECT_PLAN.md with your product list and decisions
- Be ready to type the database password yourself into .env.local when the agent asks. The agent cannot read that file
Make it yours
Change the example (a fashion store) to your own. Your answers are saved in this browser and used in every episode.
Other series keywords (5)
Your prompt
Paste into: {{ai_agent}} running in your project folder.You are my coding agent working inside this project folder. I am a non-programmer. Explain database words briefly the first time you use them.
Context: "Thread & Needle" is a fashion e-commerce store selling women's and men's clothing, shoes and accessories in United States, charging in USD. The code is a Next.js and TypeScript project on GitHub. Read the rules file and PROJECT_PLAN.md first. Local PostgreSQL is running. Production (Neon) comes later, so drive everything from one DATABASE_URL setting.
Goal: a local database called shop_local, managed with Drizzle ORM and migration files, with the store tables and safe sample data.
Plan first: check the official Drizzle documentation for PostgreSQL for the current packages, install commands and config format, and tell me what you found (including whether the recommended release is stable or a release candidate). Show me your table design in plain language and wait for my OK. Work on a branch named feat/local-database.
Tasks:
1. Create the database shop_local and a separate database user for the app with its own strong password, with rights only on shop_local. Do not use the administrator account in app code. You do not know the administrator password, so give me the exact commands to run myself in my terminal, and I will put the new app password in .env.local myself because you cannot read that file. Add the connection name to .env.example with an empty value.
2. Install Drizzle ORM, drizzle-kit and a PostgreSQL driver. Create the database connection in the db folder, and check the DATABASE_URL setting with Zod.
3. Define the schema in db with these tables: categories, products, variants (size, color or similar options, with their own SKU and stock count), customers (email unique, password hash column that stays empty until Episode 11, role of customer or admin, created date), orders, order_items, payments, and settings (key and value pairs such as store name and currency).
4. Design rules: use UUIDs or similar for ids; store money as whole numbers in the smallest currency unit (cents) with a currency code, never decimals; order_items must keep a copy of the product name and price at purchase time; orders have a status (pending, paid, shipped, cancelled, refunded); payments store the provider, a unique provider payment reference, the amount and status; add foreign keys, NOT NULL where required, and indexes on lookups such as slug, email and order status.
5. Generate the first migration with drizzle-kit, show me the SQL, then apply it. Add npm scripts for generate, migrate and seed. Never reset or drop data without asking me.
6. Create a seed script that adds 3 or 4 categories, 8 to 12 clearly sample products suited to women's and men's clothing, shoes and accessories, each with 2 or 3 variants, and basic settings. The script must refuse to run unless the database host is this computer, so it can never touch an online database.
Security rules:
- Never print, log or commit the database URL or any password. No real customer data in seed data.
- Only parameterized queries through Drizzle. No admin account with a password in code; that comes in Episode 11.
Verify (run these yourself and show me): the tables in shop_local, row counts, a query showing products with category and price, lint, typecheck and build.
Commit to git in small steps with clear messages, push the branch, open a pull request, wait for the check to pass, and ask me to approve the merge.
Report back in this format:
1. Done: tables and sample data created
2. Check: how I can look at the data myself (and which tool you suggest)
3. My decisions: anything unsure, such as stock handling or variantsReview the prompt, then copy it. Never paste real passwords or API keys into a chat.
How to check it worked
- The agent shows a list of tables including categories, products, variants, customers, orders, order_items, payments and settings
- A query shows your sample products with category names and prices in the smallest currency unit
- The migration files are saved in the project and committed, and DATABASE_URL does not appear anywhere in git
- Running the seed script a second time does not create duplicate products, or the agent explains how it avoids that
- The pull request is green and merged, and the agent worked on a branch
Sample result
Fashion store exampleExample result for the fashion store: the tables the agent created and the sample data it loaded.
If something goes wrong
- The connection fails with "password authentication failed" or "connection refused".
- Check that PostgreSQL is running and that the app user password in .env.local matches the one you set. Ask the agent to run a connection check that prints only success or failure, never the URL.
- The psql or database commands are not found.
- Open a new terminal window. If it still fails, ask the agent how to add the PostgreSQL tools folder to your path on your system, or to run the commands with the full path to the tool.
- A migration fails halfway or the tables look wrong.
- Do not delete files by hand. Tell the agent: "Explain what state the database is in, then propose the safest fix and wait for my OK." On a local database with only sample data, dropping and recreating shop_local is acceptable, but only after the agent asks you.
- The agent suggests a command that changes the schema without a migration file.
- Ask it to use generated migration files instead, so the same steps can be repeated on the online database later. Quick schema pushes are fine for experiments but leave no history.
More in Foundation
- Episode 5Put the project on GitHub with a safe daily workflow
- Episode 7Give your store a brand, a design system and a home page
This is part of the series Build a secure, no-cost e-commerce website with AI agents. See all 24 episodes, the tools used and answers to common questions on the series page.