Caretaker Express · PostgreSQL · Cedar

Checkpoint · Lesson 2.4

Seed data and reading it with SQL: checkpoint files

Git tag in the reference repository
lesson/m02-l04 (commit c9ee520)
Replay result
All steps replayed from an empty folder
Tests at this point
pass (tests 7, pass 7, fail 0)

Compare your folder with the reference using Git, if you cloned it: git diff lesson/m02-l04 -- src.

Files changed in this lesson

package.json changed

package.json
{  "name": "caretaker",  "version": "1.0.0",  "description": "",  "scripts": {    "test": "NODE_ENV=test node --test --test-reporter=spec",    "start": "node src/server.js",    "dev": "node --watch src/server.js",    "db:migrate": "node --env-file=.env scripts/migrate.js",    "db:seed": "node --env-file=.env scripts/seed.js"  },  "keywords": [],  "author": "",  "license": "ISC",  "type": "module",  "dependencies": {    "ejs": "7.0.2",    "express": "5.3.0",    "pg": "8.23.1"  },  "devDependencies": {    "supertest": "7.3.2"  }}

scripts/seed.js added

scripts/seed.js
import pg from 'pg';import { seed } from '../src/seed.js'; const client = new pg.Client({ connectionString: process.env.DATABASE_URL });await client.connect();try {  await seed(client);  console.log('seeded the Caretaker scenario');} finally {  await client.end();}

src/seed.js added

src/seed.js
export const organisations = [  { id: 1, name: 'Liffey Estates' },  { id: 2, name: 'Harbour Lets' },]; export const buildings = [  { id: 10, organisationId: 1, name: 'Dublin Offices' },  { id: 20, organisationId: 1, name: 'Cork Apartments' },  { id: 30, organisationId: 2, name: 'Galway Studios' },  { id: 40, organisationId: 2, name: 'Limerick House' },]; const namedIssues = [  { id: 101, buildingId: 10, title: 'Lift stuck between floors 2 and 3', priority: 'high', status: 'open', occurredAt: '2026-11-02T09:15:00Z', description: 'The lift stopped between floors with nobody inside. Engineer called.' },  { id: 102, buildingId: 10, title: 'Dripping tap in the second-floor kitchen', priority: 'low', status: 'closed', occurredAt: '2026-11-05T14:40:00Z', description: 'Worn washer. Tap replaced and floor dried.' },  { id: 103, buildingId: 20, title: 'Front door lock sticks at entrance 4', priority: 'medium', status: 'open', occurredAt: '2026-11-08T07:05:00Z', description: 'The key turns only after several tries.' },  { id: 104, buildingId: 20, title: 'Water stain on the ceiling of flat 7', priority: 'high', status: 'investigating', occurredAt: '2026-11-10T11:30:00Z', description: 'Stain about 40 cm wide. Roof drain to be checked.' },  { id: 105, buildingId: 30, title: 'Boiler lost pressure in plant room', priority: 'critical', status: 'open', occurredAt: '2026-11-11T06:50:00Z', description: 'No heating on the ground floor.' },  { id: 106, buildingId: 40, title: 'Car park barrier stuck open', priority: 'low', status: 'open', occurredAt: '2026-11-12T16:20:00Z', description: '' },]; const fillerTitles = [  'Loose handrail on stairs',  'Bins not collected',  'Corridor light not working',  'Radiator cold in room 2',  'Window does not close fully',  'Loose floor tile at entrance',  'Toilet cistern keeps running',  'Blocked sink in kitchen',]; // 24 extra issues (ids 107-130) alternate between buildings 10 and 20,// so that lists have enough rows to page, sort and filter.function fillerIssues() {  const priorities = ['low', 'medium', 'high', 'critical'];  const statuses = ['open', 'investigating', 'closed'];  const issues = [];  for (let i = 0; i < 24; i++) {    issues.push({      id: 107 + i,      buildingId: i % 2 === 0 ? 10 : 20,      title: `${fillerTitles[i % fillerTitles.length]} (#${107 + i})`,      priority: priorities[i % priorities.length],      status: statuses[i % statuses.length],      occurredAt: new Date(Date.UTC(2026, 9, 1 + i, 8 + (i % 9))).toISOString(),      description: '',    });  }  return issues;} export const issues = [...namedIssues, ...fillerIssues()]; export async function seed(client) {  await client.query('BEGIN');  try {    await client.query('TRUNCATE issues, buildings, organisations RESTART IDENTITY CASCADE');    for (const o of organisations) {      await client.query('INSERT INTO organisations (id, name) VALUES ($1, $2)', [o.id, o.name]);    }    for (const s of buildings) {      await client.query('INSERT INTO buildings (id, organisation_id, name) VALUES ($1, $2, $3)', [s.id, s.organisationId, s.name]);    }    for (const e of issues) {      await client.query(        `INSERT INTO issues (id, building_id, title, description, priority, status, occurred_at)         VALUES ($1, $2, $3, $4, $5, $6, $7)`,        [e.id, e.buildingId, e.title, e.description, e.priority, e.status, e.occurredAt],      );    }    for (const table of ['organisations', 'buildings', 'issues']) {      await client.query(`SELECT setval(pg_get_serial_sequence($1, 'id'), (SELECT max(id) FROM ${table}))`, [table]);    }    await client.query('COMMIT');  } catch (err) {    await client.query('ROLLBACK');    throw err;  }}

Project tree at this checkpoint

.env.example
.gitignore
.npmrc
compose.yaml
db/init/01-create-test-database.sql
migrations/001_create_organisations_buildings_issues.sql
package-lock.json
package.json
public/css/style.css
scripts/migrate.js
scripts/seed.js
src/app.js
src/migrate.js
src/seed.js
src/server.js
test/issues.test.js
views/error.ejs
views/issues/index.ejs
views/issues/new.ejs
views/issues/show.ejs
views/not-found.ejs
views/partials/foot.ejs
views/partials/head.ejs
views/partials/nav.ejs

Back to lesson 2.4