166```
167โโโ backend/
168โ โโโ database/
169โ โ โโโ migrations.ts # Schema definitions
170โ โ โโโ queries.ts # DB query functions
228- Handle API calls properly with proper error catching
229
230### Database Patterns
231- Run migrations on startup or comment out for performance
232- Change table names when modifying schemas rather than altering
7 out: "./backend/drizzle",
8 dbCredentials: {
9 url: process.env.DATABASE_URL!,
10 },
11});
9- The **client-side entrypoint** is `/frontend/index.html`, which in turn imports `/frontend/index.tsx`, which in turn imports the React app from `/frontend/components/App.tsx`.
10
11[React Hono Example](https://www.val.town/x/stevekrouse/reactHonoExample) is a fuller featured example project, with a SQLite database table, queries, client-side CSS and a favicon, and some shared code that runs on both client and server.
1305 }
1306
1307 // Removed difficulty/progress tracking and database updates
1308 },
1309
39 } catch (error) {
40 console.error("Failed to save email:", error);
41 // throw new Error("Failed to save email to database");
42 }
43}
407 const openai = new OpenAI(); // Automatically uses process.env.OPENAI_API_KEY
408
409 // --- Database Setup ---
410 // Use a consistent key based on the val's path for table namespacing
411 const valPath = new URL(import.meta.url).pathname.split("/").pop() || "default_learning_app";
422 `);
423 } catch (e) {
424 console.error("Database setup failed:", e);
425 // Don't necessarily stop the whole app, but log the error
426 }
6import { cors } from "https://esm.sh/hono@4.0.9/cors?dts";
7import { ProjectStatus, ApiResponse, CreateProjectRequest, CreateSubscriberRequest } from "../shared/types.ts";
8import { initDatabase } from "../database/migrations.ts";
9import {
10 createProject,
17 getProjectsByEmail,
18 getAllSubscribers
19} from "../database/queries.ts";
20import { sendNewSubscriberNotification, sendWelcomeEmail } from "./email-service.ts";
21import { isValidEmail, logger } from "./utils.ts";
24const api = new Hono();
25
26// Initialize database on startup
27initDatabase().catch(error => {
28 logger.error('Failed to initialize database', error);
29 throw error;
30});
6import { email } from "https://esm.town/v/std/email";
7import { logger } from "./utils.ts";
8import { getAllProjects, getSubscribersByProjectId } from "../database/queries.ts";
9import { sendWelcomeEmail } from "./email-service.ts";
10
1/**
2 * Database queries for the email capture system
3 */
4
1/**
2 * Database migrations for the email capture system
3 */
4
10
11/**
12 * Initialize database tables
13 */
14export async function initDatabase() {
15 console.log('Initializing database...');
16
17 // Create projects table
57 `);
58
59 console.log('Database initialized successfully');
60}
61
62// Only run this file when executed directly
63if (import.meta.main) {
64 await initDatabase();
65}
66