Save Tickets to a JSON File

Before the parts shelf exercise, Dev asked what happens to a morning’s tickets when the app restarts. You’ve already seen the answer. When you first added POST /tickets, Rosa’s radio vanished after a restart, because new tickets only ever lived in an array in memory, and tickets.json never changed.

In this lesson, you’ll move the tickets into a store module that reads tickets.json on every request and writes changes back to the file. Then you’ll point every route at it, so a ticket the desk logs at ten in the morning is still there after a restart. Start with the module that owns the file.

Give the Tickets a Store

Right now, tickets.ts reads tickets.json once at startup, and every change after that only touches the array in memory. The fix is a store: a module whose functions are the only code that reads or writes the data file. The routes ask the store for tickets and hand it changes, and they never touch the file themselves.

The plan is the one you’d sketch if you’ve used readFile() and writeFile() from node:fs/promises, as in the Timers and Files course. Every store function reads the file, and every change writes the whole array back. Create store.ts next to app.ts:

Language: TypeScript Filename: store.ts
import { readFile, writeFile } from "node:fs/promises";
import type { NewTicket, Ticket } from "./tickets.ts";

export function createTicketStore(file: string) {
  async function load(): Promise<Ticket[]> {
    const text = await readFile(file, "utf8");
    return JSON.parse(text);
  }

  async function save(tickets: Ticket[]): Promise<void> {
    const text = JSON.stringify(tickets, null, 2);
    await writeFile(file, text + "\n");
  }

  async function list(): Promise<Ticket[]> {
    return load();
  }

  async function get(id: string): Promise<Ticket | undefined> {
    const tickets = await load();
    return tickets.find((ticket) => ticket.id === id);
  }

  return { list, get };
}

The load() function reads the file and parses it, trusting it to hold tickets, as tickets.ts did. A missing or broken file comes up later in the course.

The save() function turns the array back into text. The 2 tells JSON.stringify() to indent by two spaces, so the file stays readable, the null before it fills a slot you don’t need, and the "\n" ends the file with a newline, as before.

The store takes the file’s path as an argument instead of writing "tickets.json" inside store.ts, because createTicketStore() doesn’t decide which file the café uses. Its inner functions are closures, so each one remembers file. The app will pass "tickets.json", and a test can pass the path of a temporary file and never touch the café’s real tickets.

Only list and get leave the function for now. The load() and save() helpers stay private, so nothing outside the store can write the file. If you’d like to weigh this design against simpler ones, ask the mentor:

Read Tickets Through the Store

The store can already read, so connect the two read routes first, and check the single-ticket route with one request before anything writes the file.

The GET /tickets route calls findTickets(), which filters the array in tickets.ts. Change its first lines so it filters whatever list it’s given:

Language: TypeScript Filename: tickets.ts
export function findTickets(
  tickets: Ticket[],
  filter: TicketFilter,
): Ticket[] {
  return tickets.filter(

The rest of the function stays the same. In app.ts, remove getTicket from the ./tickets.ts import, import the store, and create it below const PORT = 3000;:

Language: TypeScript Filename: app.ts
import { createTicketStore } from "./store.ts";

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.