Alcove CMS
Ru

Part II, Data

Migrations and queries

SQL files applied once, queries that make a page live.

Content that you want to sort, filter or give pages of its own goes into a database. SQLite is built in and the file is data/site.db, next to the content folder. PostgreSQL and MySQL work with a dsn in site.yaml.

Tables and rows come from plain SQL files in migrations/. They run in name order, each one once, and Alcove records which ones ran in alcove_migrations.

An alcove with query: gets its rows from the database. A page that shows such an alcove is not written to a file: it is rendered on request, so it always shows the current rows.

Live result This page is rendered on request.

TitleAuthorYearGenre
Pride and PrejudiceJane Austen1813novel
EmmaJane Austen1815novel
PersuasionJane Austen1817novel
FrankensteinMary Shelley1818gothic
The Last ManMary Shelley1826gothic
A Christmas CarolCharles Dickens1843novella
Bleak HouseCharles Dickens1853novel
Great ExpectationsCharles Dickens1861novel
War and PeaceLeo Tolstoy1869novel
The Death of Ivan IlyichLeo Tolstoy1886novella
Ethan FromeEdith Wharton1911novella
The Age of InnocenceEdith Wharton1920novel

Source the files behind the result above

migrations/001_schema.sql (excerpt)

CREATE TABLE books (
  id INTEGER PRIMARY KEY,
  slug TEXT NOT NULL UNIQUE,
  title TEXT NOT NULL,
  author_id INTEGER NOT NULL REFERENCES authors(id),
  year INTEGER NOT NULL,
  genre TEXT NOT NULL,
  available INTEGER NOT NULL DEFAULT 1,
  summary TEXT NOT NULL
);

migrations/002_library.sql (first lines)

INSERT INTO books (slug, title, author_id, year, genre, available, summary) VALUES
  ('pride-and-prejudice', 'Pride and Prejudice', 1, 1813, 'novel', 1, 'Five sisters, one estate and a first impression that turns out to be wrong.'),
  ('emma', 'Emma', 1, 1815, 'novel', 0, 'A young woman arranges everyone''s marriages except, for a long time, her own.'),
  ('persuasion', 'Persuasion', 1, 1817, 'novel', 1, 'A broken engagement, eight years later, in Bath.'),

content/alcoves/demo_books/alcove.yaml

query: |
  SELECT b.title, a.name AS author, b.year, b.genre
  FROM books b
  JOIN authors a ON a.id = b.author_id
  ORDER BY b.year

content/alcoves/demo_books/blueprint.html

<table class="books">
  <thead><tr><th>Title</th><th>Author</th><th>Year</th><th>Genre</th></tr></thead>
  <tbody>{{ tiles }}</tbody>
</table>

content/alcoves/demo_books/tile.html

<tr><td>{{ title }}</td><td>{{ author }}</td><td>{{ year }}</td><td>{{ genre }}</td></tr>