what

Make content durable

Hand your pages from the data file to the database once, so the records keep their IDs and addresses and no file can quietly overwrite them again.

Allow 20–30 minutes. This is an editorial estimate. Continue in the same my-site folder.

Read

Start at L11. Since lesson 6, data/pages.json has been the source of your pages. The [data] section tells the server to read that file and keep its copy in step with it: change the file and the site changes too. That is right while the file is the only place you edit content.

A content system needs the opposite. Later lessons let you create and edit pages in the browser, and those changes are saved in the site's database, the file data/app.db the server manages. If the server still copied data/pages.json over the database, every edit would be lost the next time the file was read, and a page you deleted would come back. So the records need one owner, and from this lesson on that owner is the database.

The handover is an import that happens once. On the first start after the change, the server copies every record from the file into the database, keeping each id, and writes down that the import is done. That note in the database decides everything afterwards: the file is never read again, even if you change it, delete it, or delete every page in the database. Either the import happens completely or nothing changes, so a failed start can simply be tried again.

Two rules come with the new owner. Visitors may read the pages but not change them; a later lesson adds an administrator who can. And no two pages may share a slug: the database itself refuses the second one, however the change arrives.

Change

1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-L12, so you can compare or go back.

2. Change who owns the pages. Replace what.toml. The [data] section becomes [import], and a new [collections.pages] section sets the two rules.

what.toml
# What Project Configuration

[server]
port = 8085
host = "127.0.0.1"

[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"

# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"

# Visitors may read the pages but not change them, and no two pages may share
# a slug: the database itself refuses the second one.
[collections.pages]
create = "none"
update = "none"
delete = "none"
unique = ["slug"]

3. Say where the pages live now. Three pages still tell visitors that the pages come from data/pages.json, and the empty message asks them to add a record there, which no longer works. Replace these files; only those words change.

site/pages/index.html
<what>
title = "Pages | My website"
description = "Everything I have written, in one place."
fetch.pages = "local:pages"
fetch.pages.search = "#query.q#"
fetch.pages.search_fields = "title, summary"
fetch.pages.sort = "#query.sort#"
fetch.pages.sort_options = "id, title, title:desc"
fetch.pages.page = "#query.page#"
fetch.pages.page_size = "3"
</what>

<h1>Pages</h1>
<form class="listing-controls" method="get" action="/pages" role="search">
  <div class="field">
    <label for="q">Search titles and summaries</label>
    <input id="q" name="q" type="search" value="#query.q#">
  </div>
  <div class="field">
    <label for="sort">Order</label>
    <select id="sort" name="sort">
      <if query.sort == "title">
      <option value="id">ID order</option>
      <option value="title" selected>Title A to Z</option>
      <option value="title:desc">Title Z to A</option>
      <elseif query.sort == "title:desc"/>
      <option value="id">ID order</option>
      <option value="title">Title A to Z</option>
      <option value="title:desc" selected>Title Z to A</option>
      <else/>
      <option value="id" selected>ID order</option>
      <option value="title">Title A to Z</option>
      <option value="title:desc">Title Z to A</option>
      </if>
    </select>
  </div>
  <button type="submit">Show pages</button>
</form>
<p class="result-count">Matching pages: #pages_page.total#</p>
<if pages>
<div class="card-grid">
<loop data="#pages#" as="page">
  <what-content-card heading="#page.title#" summary="#page.summary#" href="/pages/#page.slug|urlencode#"/>
</loop>
</div>
<nav class="pager" aria-label="More pages">
  <if pages_page.has_prev><a rel="prev" href="/pages?q=#query.q|urlencode#&amp;sort=#query.sort|urlencode#&amp;page=#pages_page.prev#">Previous page</a><else/><span></span></if>
  <span aria-current="page">Page #pages_page.number# of #pages_page.pages#</span>
  <if pages_page.has_next><a rel="next" href="/pages?q=#query.q|urlencode#&amp;sort=#query.sort|urlencode#&amp;page=#pages_page.next#">Next page</a><else/><span></span></if>
</nav>
<else/>
<if query.q>
<p class="empty-state">No pages match “#query.q#”. <a href="/pages">Show all pages</a></p>
<else/>
<p class="empty-state">No pages yet.</p>
</if>
</if>
site/pages/[slug].html
<what>
title = "Page | My website"
description = "One page of my site."
fetch.page = "local:pages"
fetch.page.filter = "slug=#params.slug#"
fetch.page.single = true
</what>

<what-page-content heading="#page.title#" summary="#page.summary#" body="#page.body#"/>
<p><a href="/pages">All pages</a></p>
site/inventory.html
<what>
title = "Inventory | My website"
description = "Every page on my site, one row each."
fetch.pages = "local:pages"
</what>

<h1>Inventory</h1>
<p>The same records as the Pages list, shown as a table. This is a public list for visitors, not an admin screen: nothing here can change the data.</p>
<if pages>
<div class="table-wrap">
  <table class="inventory">
    <caption>Pages in the site's database</caption>
    <thead>
      <tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th></tr>
    </thead>
    <tbody>
<loop data="#pages#" as="page">
      <tr><td>#page.id#</td><td><a href="/pages/#page.slug|urlencode#">#page.title#</a></td><td><code>#page.slug#</code></td></tr>
</loop>
    </tbody>
  </table>
</div>
<else/>
<p class="empty-state">No pages yet.</p>
</if>

4. Start the server again with run-what dev. Before the address, it prints [import] pages: copied 8 records from data/pages.json into the database, once. The cards, the list, each page and the inventory still read local:pages, so they all switch to the database together.

Check

  1. Open /pages. It shows the same eight pages, “Matching pages: 8” and the same pagination as before. Open a page: its address and text are unchanged.
  2. Stop and start the server again. This time it prints that the database owns these records and the file is not read again. The pages and their IDs are the same.
  3. Change the title of the first record in data/pages.json and save. Reload /pages, then restart the server: the title on the site stays as it was. Undo your edit afterwards.

Exercise

Show that the records no longer depend on the file, without touching your backup. Stop the server and copy the migrated my-site folder to my-site-exercise. In the exercise copy, change a title in data/pages.json, then run run-what dev from that folder: its pages are unchanged, because its database already owns them. Explain in one sentence who owns the records now, and why reading the file again would be harmful. Stop the exercise server and delete my-site-exercise when you are done.

If something goes wrong

  • The server stops with “would refresh `pages` from its source”. what.toml still has a [data] line for pages. The database owns the pages now, so the server refuses to let the file overwrite them. Remove the [data] section.
  • The server stops with “records … have the same slug”. Two records in data/pages.json share a slug. Nothing was imported; give each record its own slug and start again.
  • The server stops with “already holds … records that … differ”. The database holds records that do not match data/pages.json, for example because an older build wrote it. They may be edits you want to keep, and nothing was changed. Stop the server and keep a copy of the whole my-site folder before changing anything; never move the database file on its own. To continue the course from a known state, use the checkpoint below in a new folder.
  • Start again from a known state. Download the L12 checkpoint into a new, empty folder; never unpack it over your project. The checkpoint contains no database: its first start imports data/pages.json once. Never copy a database from a newer lesson into an older folder.

Next: give the site an administrator.