Module 03 · Lesson 3.2
Repository Structure and Conventions
On this page
- What you will learn
- Why repository structure matters
- The Tidewell folder layout
- Naming conventions that people and assistants follow
- Module boundaries: who may import whom
- Step-by-step workflow: set the layout and teach it
- What the assistant does and what you do
- Conclusion and your next step
- Reusable prompt
- Expected output
- Common errors and fixes
- Validation checklist
- Hands-on exercise
- Knowledge check
- Frequently asked questions
- References
Picture a workshop where every tool has a labelled hook. A new mechanic can find the spanner in seconds. Now picture the same tools thrown in one big box. A repository is the same. When every file has a home, people find things fast, and an AI assistant puts new code in the right place.
In this lesson you set the folder layout for Tidewell's booking system. You agree naming rules, and you set boundaries that say which parts of the code may use which other parts. You also run a small, tested script that catches a boundary break. By the end, a new person could guess where any file lives.
What you will learn
- Lay out folders so that every kind of file has one obvious home.
- Agree naming rules that a person and an assistant can both follow.
- Set module boundaries that stop code from tangling.
- Check a boundary rule with a small script, and teach an assistant the layout.
Before you start
- Completed Generating a Project Skeleton, or have a project skeleton of your own.
- Access to any capable AI assistant.
- Optional: Node.js and the project from earlier lessons, if you want to run the examples.
Why repository structure matters
A repository structure is the plan for where each kind of file lives. A convention is a rule the team agrees to follow, so nobody has to ask the same question twice. For example: 'migrations go in one folder and have numbered names'.
Structure matters more when an assistant writes code. It cannot see your plans. It copies the patterns it can see. If the layout is clear, it follows it. If the layout is vague, it invents one, and you end up with three different ways to do the same thing.
- Feature folder
- A folder that holds everything for one business area, such as bookings. Like one drawer for all the tools of one trade.
- Module boundary
- A rule about which parts of the code may use which other parts. Like a rule that the kitchen staff do not walk through the patient waiting room.
- Import
- A line of code that brings in code from another file so that this file can use it.
- Route handler
- The code that answers one web address, such as a request to list free slots.
The Tidewell folder layout
This is the layout for tidewell-booking. It follows the component diagram from Module 2: pages and route handlers, booking rules, data access and the database. Later modules add files, but never new top-level homes.
tidewell-booking/
docs/ problem-statement.md, stakeholders.md, requirements.md,
acceptance-criteria.md, architecture.md, adr/
db/migrations/ 001_core_tables.sql, 002_bookings_and_changes.sql,
003_waiting_list.sql
src/app/ pages: /book, /my-bookings, ...
src/app/api/ route handlers
src/features/bookings/ rules.ts, validation.ts, service.ts, types.ts
src/lib/ config.ts, db.ts, problem.ts, api-client.ts
src/components/ shared interface pieces
.env.example .gitignore AGENTS.md README.md package.json tsconfig.jsonThe repository layout.
| Folder | What lives here | What does not |
|---|---|---|
| docs/ | Requirements, acceptance criteria, architecture notes and decision records. Written before code. | Code, secrets, meeting notes that nobody maintains. |
| db/migrations/ | Numbered SQL files that build the database, applied in order. | Application code. Edits to a file that was already applied. |
| src/app/ | Pages and the thin route handlers under api/. | Business rules. A handler reads the request, calls the service and returns the answer. |
| src/features/bookings/ | The rules about bookings, input checks, the service that does the work and the shared types. | Anything about how a page looks. |
| src/lib/ | Small helpers used everywhere: settings, database access, error format, API client. | Business rules. A helper that grows rules belongs in a feature. |
| src/components/ | Shared interface pieces used by more than one page. | Calls to the database. |
Where do tests go? Module 7 adds them. Reserve the rule now: a test file sits next to the file it tests and ends in .test.ts, so rules.ts and rules.test.ts are neighbours. Putting a rule in the plan before the first test exists stops the first test from setting a different pattern by accident.
A business rule has one home
The practice manager confirmed that a patient may move or cancel online only if the appointment starts at least 24 hours from now. That number is a business rule. It lives in src/features/bookings/rules.ts as NOTICE_PERIOD_HOURS = 24, with a comment pointing to the decision. It does not live in a page, a handler or an environment variable. One home means one place to change it.
Naming conventions that people and assistants follow
Keep the rules few and easy to check. Each one should answer a question that would otherwise come up in every code review.
| Thing | Rule | Example |
|---|---|---|
| Folders and plain code files | lowercase, words joined by hyphens or a single word | src/features/bookings/rules.ts |
| Components | One component per file, file named after it in PascalCase | SlotList.tsx |
Page and route files in src/app | The names the framework requires. Check its docs. | page.tsx, route.ts |
| Constants for fixed values | UPPER_CASE with underscores | NOTICE_PERIOD_HOURS |
| Migrations | Three-digit number, underscore, short description | 001_core_tables.sql |
| Database tables and columns | lowercase with underscores (snake_case) | booking_changes |
| Requirement references | The ID goes in a comment on the code that serves it | // FR-02 |
The Next.js docs list its special file names and ways to organise folders. Read the project structure guide (opens in a new tab) for the current rules, because they can change.
Module boundaries: who may import whom
Folders only help if code respects them. A boundary rule says which areas may import from which. For Tidewell, the rule is simple. Code flows one way, from pages down to features, and from features down to the library.
| Area | May import from | Why |
|---|---|---|
| src/app | features, lib, components | Pages and handlers sit on top and use everything below. |
| src/components | lib | Interface pieces must not know how bookings work. |
| src/features | lib | Booking rules must not depend on how pages look. |
| src/lib | nothing in src | Helpers stay small and independent. |
Here is a script that checks the rule. It reads each file's import lines and reports any that cross a boundary the table does not allow. It works on text, so it needs no real files.
// Which top-level areas each area may import from.
const allowed: Record<string, string[]> = {
app: ["features", "lib", "components"],
components: ["lib"],
features: ["lib"],
lib: [],
};
function areaOf(path: string): string | undefined {
return path.match(/^src\/([^/]+)\//)?.[1];
}
/** Find imports that cross a boundary the layout does not allow. */
export function findBoundaryBreaks(files: Record<string, string>): string[] {
const breaks: string[] = [];
for (const [path, source] of Object.entries(files)) {
const from = areaOf(path);
if (!from) continue;
for (const match of source.matchAll(/from "@\/([^/"]+)/g)) {
const to = match[1];
if (to !== from && !allowed[from]?.includes(to)) {
breaks.push(`${path} imports from ${to}`);
}
}
}
return breaks;
}
const files = {
"src/app/book/page.tsx": 'import { listSlots } from "@/features/bookings/service";',
"src/features/bookings/service.ts": 'import { db } from "@/lib/db";',
"src/features/bookings/rules.ts": 'import { SlotList } from "@/components/SlotList";',
};
const result = findBoundaryBreaks(files);
console.log(result.length === 0 ? "No boundary breaks." : result.join("\n"));Tested with the TypeScript compiler and run twice. The last example file breaks a rule on purpose.
What each part does
allowedis the table above, written as data. Each area lists what it may import.areaOfreads a file path and returns its area, such asfeatures.findBoundaryBreakslooks at every import that starts with@/. This is the shortcut name forsrc/.- If the import points to another area that is not on the allowed list, it records a break.
- The example has three files. The last one, a booking rule, imports a screen component.
src/features/bookings/rules.ts imports from componentsIn a real project you would use a linting rule or a dependency checker for this, and a later lesson shows how checks run together. The script shows what such a tool does: it turns a boundary from an opinion into something that fails loudly.
Step-by-step workflow: set the layout and teach it
Step 1: Start from the component diagram
Take each box in the Module 2 diagram and give it a folder. Boxes that talk to each other become folders that may import from each other.
What you should see: A first sketch of folders that matches the architecture.
Step 2: Write the layout and the rules down
Put the tree, the naming table and the boundary table in the README or in docs. Keep them to one page.
What you should see: A page a new person can read in five minutes.
Step 3: Create the empty folders and a few real files
Add the folders from the layout. Git does not keep empty folders, so add a short README or a first real file to each.
What you should see: A repository whose shape is visible in the file list.
Step 4: Give the assistant the layout
Paste the layout and rules into the prompt, as in the prompt card. Ask where each new file would go before it writes any code.
What you should see: A list of file locations you can check against your rules.
Step 5: Check the boundaries on every change
Run your boundary check, or a linting rule that does the same. Read any new file in a new place and ask whether it belongs there.
What you should see: A change that breaks a rule is caught before review.
- Component diagram
Module 2
- Folders
One home per kind
- Naming rules
Few and checkable
- Boundaries
Who imports whom
- Assistant briefed
Layout in the prompt
- Checked
On every change
Each step makes the next one checkable.
What the assistant does and what you do
| Task | Assistant's part | Your part |
|---|---|---|
| Proposing a layout | Suggests folders from your diagram and decisions. | Compare it with Module 2. Remove folders you do not need. |
| Placing new files | Says where each new file should go and why. | Reject any placement that breaks a boundary. |
| Naming | Follows the naming table if you give it. | Check names in the review. Fix drift early. |
| Moving files | Can move files and update imports. | Run the checks and the app afterwards. Read the diff. |
| Deciding the boundaries | Not its job. | Decide who may import whom, and why. |
Conclusion and your next step
A good layout gives every kind of file one home. A few naming rules remove daily arguments. A boundary rule stops code from tangling, and a check makes the rule real. An assistant that knows the layout places files where you would.
Your next step: the layout needs settings, and settings include secrets. Continue with Configuration and Secrets to keep keys out of the repository.
To see the same ideas in an assistant workflow, read AI Pair Programming: How to Actually Work with an AI Coding Assistant. Then practise on the Appointment Booking Portal.
Reusable prompt
Where does this file go?
Makes an assistant place new files using your layout and boundary rules, and flag any file that does not fit.
You are helping me keep a repository tidy. Do not write code. Only decide where files go.
Project: tidewell-booking (fictional clinic booking app).
Layout:
- docs/ - written documents
- db/migrations/ - numbered SQL files
- src/app/ - pages; src/app/api/ - route handlers (thin: read the request, call a service, return the answer)
- src/features/bookings/ - rules.ts, validation.ts, service.ts, types.ts
- src/lib/ - config.ts, db.ts, problem.ts, api-client.ts
- src/components/ - shared interface pieces
Import rules: app may import features, lib and components. components may import lib. features may import lib. lib imports nothing from src.
Naming: lowercase file names; components in PascalCase; constants in UPPER_CASE; migrations like 004_short_name.sql.
New files I plan to add:
1. A function that checks whether a booking is inside the notice period.
2. A card that shows one booking.
3. A SQL file that adds a reminders table.
4. A helper that formats dates for display.
5. The handler for cancelling a booking.
Tasks:
1. For each new file, give the full path and one line of reasoning.
2. If a file does not fit the layout, say so and suggest the smallest change to the layout. Do not decide for me.
3. Point out any import that would break the rules.Show the reusable template
Replace each {{placeholder}} with your own details.
You are helping me keep a repository tidy. Do not write code. Only decide where files go.
Project: {{project_name}}.
Layout:
{{folder_layout}}
Import rules: {{import_rules}}
Naming: {{naming_rules}}
New files I plan to add:
{{new_files}}
Tasks:
1. For each new file, give the full path and one line of reasoning.
2. If a file does not fit the layout, say so and suggest the smallest change to the layout. Do not decide for me.
3. Point out any import that would break the rules.Expected output and limitations
A numbered list with a full path and a reason for each new file, any file that does not fit flagged with a small suggested change, and any import that would break a rule named.
1. src/features/bookings/rules.ts - business rule about the notice period; add to the existing rules file.
2. src/components/BookingCard.tsx - interface piece used on more than one page.
3. db/migrations/004_reminders.sql - new numbered migration; do not edit 003.
Note: reminders are not in the requirements list. Confirm the need first.
4. src/lib/format-date.ts - small shared helper, no booking rules.
5. src/app/api/bookings/[bookingId]/cancel/route.ts - thin handler; calls the service in features.
Import check: BookingCard must not import from features. Pass booking data in as props.Limitations to keep in mind
- The assistant can only judge from the rules you gave it. Missing rules mean guessed answers.
- It may place files by habit from other projects. Check each path against your own table.
- It cannot see your repository unless you show it. Paste the current tree when files already exist.
Common errors and how to fix them
Business rules appear in page files and route handlers.
Why it happens: It was quicker to write the check next to the code that needed it.
Fix: Move the rule to src/features/bookings/rules.ts and call it. Add a comment that names the requirement.
A utils folder fills up with unrelated helpers.
Why it happens: No rule says where a helper belongs.
Fix: Name folders after what they hold. Move helpers into a feature or into src/lib with clear file names.
The layout exists on paper but code ignores it.
Why it happens: Nothing checks it, and the assistant was never given it.
Fix: Put the layout in the prompt and the assistant context file, and add a check that fails on a boundary break.
Validation checklist
Tick each item once you have confirmed it.
Hands-on exercise
A repository layout for Ferngate Cycle Repairs
Ferngate's parts tracker records parts taken for a job and flags low stock. Design its repository layout. Include a feature folder, the naming rules and the import rules. Then place eight new files and flag any that break your rules.
You will need
- Ferngate's stack: one Next.js web application, TypeScript, PostgreSQL.
- Ferngate's facts: mechanics share one tablet, and the low-stock threshold is 'to be agreed'.
- Eight files to place: a low-stock check, a part card, a migration for stock movements, a date formatter, a handler for recording a part, a settings file, a requirements document and a login helper.
Acceptance criteria
Your work is done when every one of these is true.
Hints (2)
- Ask whether each file is a rule, a page piece, a database change or a document. The kind decides the folder.
- A threshold that the owner has not agreed is a business decision. It should not be buried in settings.
Show a model answer
A strong answer puts the low-stock check in the parts feature, the part card in components, the migration in db/migrations with the next number, the date formatter in lib, the handler under the api folder and the requirements document in docs. It keeps the threshold out of the environment and marks it as undecided, because it is a business rule that the owner has not agreed. It flags the login helper as out of scope. It also checks one import, for example that the part card does not import from the parts feature. The answer shows that every file has one home and that each rule is stated.
Knowledge check
Choose an answer, then check it. Nothing is recorded — this is for you.
Show the answer and explanation
Answer: B. A business rule needs one home that people can find and review. An environment variable is tempting because it is easy to change, but the rule is then hidden from review and tests. Copies in every page drift apart.
Show the answer and explanation
Answer: B. Rules should be usable without any screen. Working code is a tempting reason to allow it, but the tangle makes later changes risky. Components may import from lib, but the rule is about direction, not about one folder.
Show the answer and explanation
Answer: B. An assistant fills gaps with habit from other projects. Speed is not the reason, and assistants will write code without a layout. That is the risk: the result looks fine but follows nobody's rules.
Frequently asked questions
What is a good folder structure for a Next.js project?
Keep pages and route handlers in the app folder, shared helpers in a lib folder and business code in feature folders. The best structure is one your whole team can predict. Write it down so a new person and an assistant can follow it.
What is a module boundary in simple words?
It is a rule about which parts of the code may use which other parts. It works like a rule that stops kitchen staff from walking through the waiting room. It keeps changes in one place from breaking another.
Should I organise code by feature or by type?
For a small project, grouping by feature keeps related files together. A bookings folder holds its rules, checks and types. Shared pieces still go in a common place. Pick one approach, write it down and use it everywhere.
How do I make an AI assistant follow my project structure?
Give it the layout, the naming rules and the import rules in your prompt, and keep them in a context file it reads first. Ask it to say where each file goes before it writes code. Then check its answers and run a boundary check.
- Written by
- PromptFable Editorial
- Last updated 19 Nov 2026
- Review
- Reviewed by Waseem Nadir, Technical Reviewer
- Last reviewed 27 Nov 2026
References
Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.
- Project structure and organization(opens in a new tab)
Next.js · Official documentation · The framework's special file names and options for organising folders.
- Git - gitignore Documentation(opens in a new tab)
Git · Official documentation · Which files and folders stay out of version control.
- AGENTS.md(opens in a new tab)
AGENTS.md · A shared convention for telling assistants about a project's layout and rules.
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.