All chapters

Chapter 9 · Part 3: Make it yours

Connect your database

Goal
Connect your app to your database from server code only, and keep the password in one private file where nobody else can see it.
Time
About 40 minutes. Go at your own pace.
Before you start
Chapter 8 done, and your Atlas cluster, database user, and IP address from Chapter 5.

Your app and your database are two separate things in two separate places. The app is on your computer, and the database is in Atlas. In this chapter you'll connect them, and you'll do it in a way that keeps the database password safe.

There are two rules, and they matter more than any command in this chapter.

Two rules for secrets

1. A secret stays on the server. Your app has two halves. One half runs in the visitor's browser, and anyone can look at everything sent to it. The other half runs on a server, a computer you control, and nobody sees it. The database password may only ever be used on the server side.

2. A secret lives in one private file. Not in your code, not on GitHub, and not in a chat. The file is called .env.local. Git is told to ignore it, so it never leaves your computer. When the app runs, it reads the password from there.

Why Chapter 5 said never to paste a password into Claude

Anything you paste into a chat is sent to a service, and may be kept. A database password is a key to everything in your database. That's why, in this chapter, you'll paste it into the private file yourself, and Claude Code will check that it's there without ever showing it.

1. Get your connection string

A connection string is one long line of text that tells your app where the database is and how to sign in to it. It has three parts: the database user's name, a placeholder for its password, and the address of your cluster.

  1. Open Connect on your cluster

    Sign in to Atlas and find the cluster you made in Chapter 5. This guide calls it kids-table. Choose Connect next to it.

    The Atlas Clusters page, with the kids-table cluster and the Connect button beside its name.
    The Clusters page in Atlas, captured November 15, 2026.
    The Atlas Clusters page, with the kids-table cluster and the Connect button beside its name.
  2. Choose Drivers

    Atlas asks how you want to connect. Under Connect to your application, choose Drivers and Client Libraries. A driver is the small piece of code that lets an app talk to a database.

    Atlas's Choose a connection method screen, with Drivers and Client Libraries at the top, then Atlas SQL, Compass, Shell, and MongoDB for VS Code.
    Atlas's connection methods, captured November 15, 2026.
    Atlas's Choose a connection method screen, with Drivers and Client Libraries at the top, then Atlas SQL, Compass, Shell, and MongoDB for VS Code.
  3. Copy the string

    The next screen is titled Connect to kids-table. It has JavaScript and Node.js Driver already chosen, which is what we want. Leave the Legacy URI String switch off.

    Your connection string is in the box below that. It starts with mongodb+srv:// and ends with a slash. In the middle, where the password goes, it shows the placeholder <db_password>. That's Atlas telling you to swap in your own. Choose the copy button at the right end of the box. Don't paste it anywhere yet.

    Atlas's Connect to kids-table screen, with the Node.js driver chosen and the connection string in a box, showing a placeholder where the password goes.
    Atlas's connection string screen, captured November 15, 2026. We covered part of the cluster's address. Yours will show its own.
    Atlas's Connect to kids-table screen, with the Node.js driver chosen and the connection string in a box, showing a placeholder where the password goes.

2. Ask Claude Code to build the connection

  1. Save the phase plan

    Save this in your plan folder as 09-connect-database.md, the same way you saved the others. Check that 08-data-model.md is there too.

    plan/09-connect-database.md
    # Phase 9: connect the database
    
    Read `plan/00-app-plan.md` and `plan/08-data-model.md` first. This phase connects the app to the MongoDB Atlas database you made in Chapter 5. It stores nothing yet.
    
    ## Goal
    
    The app can talk to the database from server code only, and the connection string lives in one private file on this computer, `.env.local`, and nowhere else.
    
    ## What to build
    
    - Install the official `mongodb` package and the `server-only` package.
    - One file, `lib/mongodb.ts`, that opens the connection. It runs on the server only, keeps one shared connection, and reads the address from `MONGODB_URI`. The database name comes from `MONGODB_DB` and defaults to `kidstable`.
    - A page at `/status` that says in plain words whether the app can reach the database. It never shows the connection string, the password, or a raw error.
    - An `.env.example` file with an empty `MONGODB_URI=` line, committed to Git, so anyone can see which settings the app needs.
    - Make sure `.env.local` is ignored by Git.
    
    ## Rules for this phase
    
    - Never ask me for the connection string, and never print it, log it, or put it in a message. I will paste it into `.env.local` myself. To check that it's set, look only at whether it exists and how long it is.
    - Create `.env.local` with an empty `MONGODB_URI=` line, then tell me exactly where to paste.
    - Don't put the database code in any file the browser loads. Don't start a secret's name with `NEXT_PUBLIC_`.
    - Tell me what each command does before you run it.
    - Don't create any data yet.
    
    ## Done when
    
    - `/status` says Connected.
    - `git status` doesn't list `.env.local`.
    - `npm run lint` and `npm run build` both pass.
    
    ## Out of scope
    
    Collections and data, sign-in, and photos. Those are later phases.
    
  2. Paste the prompt

    Start Claude Code in the VS Code terminal. Read each request before you approve it, as in Chapter 6.

    If your app is still running from Chapter 6, stop it first. Click in the terminal where it's running and press Ctrl+C. Claude Code will build the app to check its work, and building while the app is running can leave the page in your browser broken for a moment. From here on, start the app again after Claude Code finishes.

    Prompt for Claude Code
    Read plan/00-app-plan.md, plan/08-data-model.md, and plan/09-connect-database.md in this folder. Then do phase 9.
    
    Before you run any command, tell me in plain English what it does. Never ask me for the connection string, and never print it. When it's time, create .env.local with an empty MONGODB_URI line and tell me exactly where to paste the string. I'll tell you when it's in. Then check that it's set without showing it, and open the status page.

    Claude Code installs two packages, writes the code, and creates a file named .env.local with one empty line in it. Then it stops and tells you where to paste.

3. Paste the string yourself

  1. Open the file

    In the VS Code Explorer on the left, click .env.local. It has a line that reads MONGODB_URI= and nothing after the equals sign.

  2. Paste the string

    Click right after the =, and paste the connection string you copied. There are no spaces and no quote marks. It all goes on that one line.

  3. Put in the real password

    Find <db_password> in the line you just pasted. Replace it, including the angle brackets, with your database user's password from your password manager. It's the password for the database user you made in Chapter 5, not your Atlas login. Press Ctrl+S to save.

    It should now look like this, with your own values:

    In .env.local
    MONGODB_URI=mongodb+srv://yourname_db_user:YourRealPassword@kids-table.xxxxx.mongodb.net/

    If your password has symbols in it

    Characters like @ : / ? # have a special meaning inside a connection string, so they have to be swapped for codes. Atlas's generated passwords normally don't have any. If yours does, ask Claude Code to show you how to encode it, and don't paste the password into the chat to do it. Describe it instead, for example “it has an at sign in it.”

  4. Tell Claude Code it's in

    First, make sure nothing in .env.local is highlighted. Click on an empty spot in the file, then close its tab. Claude Code can see what you have selected in your editor and sends it along with your next message, and a highlighted password would go with it. Then type it's in and press Enter. Claude Code checks that the setting exists and how long it is, without showing it. Then it starts your app and opens the status page.

4. Check it

If Claude Code didn't start the app, run it yourself and open http://localhost:3000/status.

PowerShell
npm run dev

What you should see

A page titled Status that says Connected, and names the database. That means your app reached Atlas, signed in, and got an answer. Your connection string doesn't name a database, so the app uses one called kidstable. Atlas creates it the first time something is saved, in Chapter 10.

The Kids' Table status page: a heading that says Status, and a green box with a check mark, the word Connected, and a line saying the app reached the database named kidstable_demo.
The status page, connected, captured November 15, 2026. Ours is a demo database, so it says kidstable_demo. Yours will say kidstable. This is the finished app's look, which Chapter 11 adds. Yours is plainer for now.
The Kids' Table status page: a heading that says Status, and a green box with a check mark, the word Connected, and a line saying the app reached the database named kidstable_demo.

The live site will say Not set up yet

If you save this chapter's work to GitHub, Vercel publishes it, and the live status page says Not set up yet until Chapter 16. That's when you give the live site its own copy of the setting. It's expected, because .env.local never leaves your computer. Anyone can open that page, so Chapter 11 trims what it says on the live site down to Connected or Not connected.

If it doesn't say Connected

The status page says in plain words what went wrong. It never shows your password or the connection string. The details go to the terminal instead.

If it doesn't work

“Not set up yet.” The app can't find MONGODB_URI. Check that you saved .env.local, that the line starts with MONGODB_URI=, and that there's nothing before it. Then stop the app with Ctrl+C and run npm run dev again. The app reads this file when it starts.

The status page saying Not set up yet, and that the app has no MONGODB_URI.
What the page says when the setting is missing. In the finished app's look, which Chapter 11 adds.
The status page saying Not set up yet, and that the app has no MONGODB_URI.

“Couldn't reach the database.” Atlas only talks to addresses on its list, and yours may have changed since Chapter 5. In Atlas, open Network Access, then IP Access List. (Atlas may group these in its sidebar as Database & Network Access.) If your current address isn't there, choose Add IP Address and add your current one. Wait a minute, then reload the page. It also says this when the connection string is cut off, so check that it's all there.

The status page saying it couldn't reach the database, and to check the IP Access List and the connection string.
What the page says when the database can't be reached.
The status page saying it couldn't reach the database, and to check the IP Access List and the connection string.

“The database said no.” The username or the password is wrong. In Atlas, open Database Access, then Database Users, choose Edit next to your user, then Edit Password to set a new one. Put the new password into .env.local, save, and restart the app.

If the string ever lands somewhere it shouldn't

If you paste your connection string into a chat, an email, a document, or GitHub by mistake, treat the password as lost. Reset it in Atlas right away, as above. It costs a minute, and it makes the old one useless.

What Claude made

  • lib/mongodb.ts is the only file that opens a connection. It begins with import "server-only", which makes the build fail if any code that runs in the browser ever tries to use it. That's rule 1, enforced by the tools and not by remembering.
  • app/status/page.tsx is the status page. It asks the database one yes-or-no question and reports the answer.
  • .env.example lists the settings the app needs, with nothing filled in. It's safe to put on GitHub. Someone who copies your project can use it as a checklist.
  • .gitignore now has one extra line. It keeps .env.example and ignores your real .env.local.
  • package.json lists the two new packages: mongodb, the driver, and server-only.

Why the page is built in two pieces

The Status heading appears instantly, and the database answer streams in a moment later. Next.js 16 asks for this when a page reads live data: the part that waits goes inside a Suspense wrapper, so it can't hold up the rest of the page. In the production build, Next.js marks the page as partly prepared ahead of time, and partly filled in on request. If Claude Code writes something else and Next.js complains about data being read outside Suspense, this is the reason.

Checkpoint

0 of 4

Next step: your app can reach the database but it's empty. In Chapter 10, you'll bring in your list from a spreadsheet, and put all the names and categories in one settings file so you can make the app your own.

Last checked November 15, 2026. Software screens and free-plan limits change. If something here doesn't match what you see, the official link in the chapter is the source to trust.